Showing posts with label mel scripts. Show all posts
Showing posts with label mel scripts. Show all posts

Sunday, June 17, 2012

Gamma Correction or Why Do You Not Have A Magic Linear Button Mental Ray?!

    I had some down time about a week ago and decided to rewrite my gamma correction script with some new ideas in mind. This time I focused not on my own workflow, but tried to make a tool that would work for many different gamma correction workflows while also simplifying the interface.

    I had to come up with a simpler and quicker way of writing UI's in mel because the formLayout is really too complex and tedious to work with (at least for me!). FormLayouts require you to attach and position items in relation to each other which ends up getting annoying if you have to make modifications or add new elements to your UI. So instead I've started to nest gridLayouts inside of columnLayouts. This gives 100% predictable results, while not having to fuss with the complexity of the formLayout. Check out the new layout, with example code below.
 
string $win = `window -title "columnGridExample" columnGridLayout`;
        
        columnLayout -width 300 -cal "center" -adj 1;

        gridLayout -nc 2 -cwh 150 20;
        text -l "list title";
        text -l "list title";
        setParent..;
        gridLayout -nc 2 -cwh 150 300;
        textScrollList -ams 1;
        textScrollList -ams 1;
        setParent..;
        gridLayout -nc 2 -cwh 150 25;
        button -label "refresh";
        button -label "refresh";
        setParent..;

        separator -h 10 -style "none";
        checkBoxGrp -ncb 3 -l1 "cBox1" -l2 "cBox2" -l3 "cBox3";
        checkBoxGrp -ncb 3 -l1 "cBox4" -l2 "cBox5" -l3 "cBox6";

        separator -h 10 -style "none";
        checkBox -label "use custom options?" -v 0;
        textField -w 225;
        separator -h 20 -style "none";
        button -label "GO!" -bgc 0.5 0.2 0.2;

showWindow $win;


    As you can see I tried to give users extra control and allow them to gamma correct whatever they want. The check boxes mark attributes among maya and mental ray shaders for gamma correction, while the textures check box adds a gamma correction node in between the textures outColor and destination. Also users can choose to use mip_gamma_gain or the standard maya gammaCorrect node. If you want to gamma correct an attribute that is not included in the checkboxes, check off correct custom attributes?

The format for the custom attribute text box is as follows:

color, outColor, diffuse

Download dbrGammaCorrect

Wednesday, April 11, 2012

Mel eval() and an undetermined amount of flags

Every once in awhile I run into a mel command that requires an undetermined amount of flags to be passed for evaluation. Here's a quick example of how to build and pass an array of flags to eval.


string $curvePoints;
string $myPointArray[];
int $tx;
int $ty;
int $tz;

for($i = 0; $i < 10; $i++)
{
    $tx = $i;
    $ty = $i;
    $tz = $i;
    $myPointArray[$i] = ("-p " + $tx + " " + $ty + " "+ $tz);
}

$curvePoints = stringArrayToString($myPointArray, " ");
print $curvePoints;
eval("curve -d 3" + $curvePoints)

Monday, March 12, 2012

Matte Passes Quick and Clean

There are a bunch of scripts to create matte passes for mental ray, but these tend to be on the more complicated end of the spectrum. So to speed up my workflow a little I wrote this script. Select some geo, go to the render layer you'd like to add the matte pass to, and type 'dbrMattePass("myName")' in the command line.



global proc dbrMattePass(string $name){
    float $mayaVersion = `getApplicationVersionAsFloat` ; 
    //get selected objects
    string $sel[] = `ls -sl`;
    
    if (`size($sel)`){
        //get current render layer and create new pass contribution map
        string $curLayer = `editRenderLayerGlobals -q -currentRenderLayer`;
        string $contMap = `createNode -ss -name ($name + "PCM") passContributionMap`;
        connectAttr -na ($curLayer + ".passContributionMap") ($contMap + ".owner");

        //create new 1 channel matte render pass and associate with current render layer and new PCM
        string $mattePass;
        if ($mayaVersion == 2012)
            $mattePass = `shadingNode -ss -name ($name + "Matte") -asRendering renderPass`;
        else
            $mattePass = `createNode renderPass -name ($name + "Matte")`;
        
        setRenderPassType -type "MATTE" ($name + "Matte");
        setAttr -type "string" ($mattePass + ".passGroupName") ($name + "MatteGRP");
        setAttr ($mattePass + ".numChannels") 1;
        setAttr ($mattePass + ".frameBufferType") 256;
        setAttr ($mattePass + ".useTransparency") 1;
        setAttr ($mattePass + ".holdout") 1;
        connectAttr -na ($curLayer + ".renderPass") ($mattePass + ".owner");
        connectAttr -na ($mattePass + ".message")($contMap + ".renderPass");
      
        //add selected objects to pass contribution map
        for ($each in $sel){
            connectAttr -na ($each + ".message") ($contMap + ".dagObjects");
        }
    }
    else
    {
        print "Select some objects to create a matte.";
    }
} 



**Update: Didn't realize this initially because I hadn't reached post-production yet. Slipped right through my fingers, if you don't specify the values of hold-out and use transparency when creating pass contribution maps via mel, they will default to OFF. A huge error, hopefully nobody was relying on the previous code. The above represents working code.

Tuesday, February 14, 2012

Wiggle Your Keys!

dbrWiggleKeys

This guy allows you to create a series of keyframes in a specified time range to any number of attributes using the random, sine, and cosine functions of mel. It works with multiple object/controller selections so you can do cool stuff like apply a sine curve with overlap to a tentacle. I got tired of manually creating sine curves in the graph editor for my jelly creatures tentacles so this is a pretty huge time saver, gives me a nice base curve to go in and modify.



  • Amplitude: the maximum deviation from the current value of the attribute. If translateY is 10 and you use an amplitude 5, the keys will range from 5-15.
  •  
  • Step Size: keyframe interval.
  •  
  • Overlap Step: offsets sine curves when multiple controls or objects are selected. Selection order determines the order of overlap.

  • Attributes: Use long or short names separated by commas. No spaces following the commas.
  • ie: tx,ty,rz,visibility,translateZ

  • Wavelength of sine and cosine is: 4 * step size

I'll update this a bit in the near future to add a falloff option so that the keys fall off to a value of 0 + initial value over the time range.

download

Camera Animation Broken on Batch Render

A few people at school have had issues with their cameras not animating when rendering on our farm. I think this happens when complex camera rigs are used, sometimes even one group above a camera with keys will not work. These rigs are a little bit more difficult to deal with but here is a quick script that bakes the overall camera animation to a new camera.

global proc dbr_bakeCamera(string $cam)
{
string $bakeCam[] = `camera -name ($cam + "_BAKE")`;
string $oCST[] = `orientConstraint $cam $bakeCam[0]`;
string $pCST[] = `pointConstraint $cam $bakeCam[0]`;
bakeResults -t "1:68" -at "tx" -at "ty" -at "tz" -at "rx" -at "ry" -at "rz" $bakeCam[0];
delete $oCST[0];
delete $pCST[0];
}

//usage: dbr_bakeCamera("camName") where camName is the name of the camera you'd like to bake.


** Python

import maya.cmds as cmds
def dbr_bakeCamera(cameraName, frameRange):
     bakeCamera = cmds.camera(name = '%s_BAKE' % cameraName)
     orientCNSTR = cmds.orientConstraint(cameraName, bakeCamera[0])
     pointCNSTR = cmds.pointConstraint(cameraName, bakeCamera[0])
     cmds.bakeResults(bakeCamera[0], t = frameRange, at = ['tx', 'ty', 'tz', 'rx', 'ry', 'rz', 'focalLength'])
     cmds.delete(orientCNSTR)
     cmds.delete(pointCNSTR)

#Bake selected camera from frame 1 to 62
dbr_bakeCamera(cmds.ls(sl = True)[0], (1,62))

Monday, January 9, 2012

dbrMeshTools (conform verts and mesh constraints)

dbrMeshTools has 2 main functions:
Conform one mesh to another mesh

  • Good for retopology
  • This function is based on shrink wrapping scripts like xyShrinkWrap, I simply wanted a better interface for shrinkwrapping.

Constraining and Duplicating an object on another mesh

  • Good for placing screws or bolts on the exterior of a piece of machinery








Download dbrMeshTools

dbrRenderTool (Linear Workflow)


A utility created to help maintain a Linear workflow rendering with Mental Ray.
Pulls textures and material color/outcolor/diffuse down to 0.454 gamma using mip_gamma_gain nodes.
Toggles gamma 2.2 on all perspective cameras in a scene via mia_exposure_simple.



Instructions:

1. Load Textures or Materials into the textScrollList using the buttons all textures or all materials.
2. Select materials and textures you would like to gamma correct.
3. Press selected materials or selected textures under Gamma Correct to pull down texture and material colors down to 0.454 gamma.

OPTIONAL: Press Toggle Gamma 2.2 to bump gamma on all perspective cameras to 2.2. This allows for a preview of final output from a compositing program.

4. Batch Render. (Toggle Gamma 2.2 off before batch rendering)

Download dbrRenderTool

dbrToon

This is a brief script I wrote for Zach and Sang to streamline the process of working with multiple toon lines in maya. It is basically just a toon line management script that presents you with the most useful toon line attributes and inputs in a compact interface. Eliminating the need to go through the attribute editor in most cases.



Instructions
Simply input a name for a toon outline, select an object, and click "add new toon"

To vary line width, add a noise texture to line width map.
A useful expression to vary width over time: noise(frame)/x where x is a scale factor for the expression. This can be added to the implode attribute of a noise texture.
In addition if you want to wiggle geometry over time, you can use the node "polyMoveVertex". Select your geo, and type polyMoveVertex in the mel command line.
This expression can be used for the local translation attributes of the polyMoveVertex node: noise(n)/x. Where n is a distance of translation and x is a scale factor.
--Ideas for varying toon line loosely based on Keith Lango's NPR blog posts.

*Works with and incorporates scenes with existing toon lines

Download dbrToon

Sunday, January 8, 2012

It's Parti Time!

There are a bunch of us working on thesis right now who want to use parti volumes for generating volumetric light fog. Unfortunately parti volumes require a bit too much setup; creating transmats, making connections to shading groups, connecting lights etc etc. It all adds up to quite a bit of setup when you're doing it across multiple scene files. So I decided to write a little interface for quickly creating and modifying parti volumes.

The interface exposes the most important mental ray render settings, allows you to select lights and create a parti volume that has those lights already attached, and allows you to add lights to existing parti volumes.

  • "Auto Volume" has to be checked on and "Shadow Ray Depth" have to be set to at least 5 in order to cast volume shadows.
  • The most important quality settings are the min and max step length attributes of the parti volume. Setting this too low will cause render times to shoot through the roof.



Normalizing Cluster Weights

For some reason maya doesn't have an option to normalize cluster weights. I guess it's pretty uncommon to have cluster weights exceed 1. Anyway a friend of mine asked me if it was possible to write a script to normalize cluster weights for a cluster making script he was working on. It was a fun little challenge!

curve2parent

I wrote this for a friend who frequently uses this technique for his FK joints.
It add a nurbs shape as a parent to a bone.
It iterates over any number of joints you have selected.



Download curve2parent