Showing posts with label Mel. Show all posts
Showing posts with label Mel. Show all posts

Friday, May 11, 2012

MEL: Maya file texture node initializing for Mental ray.


The default filter type in Maya's file texture node is set to Quadratic and this is not desireable for Menta ray since Mental ray uses Mipmap filter.
And the default filter value is set to 1.0 which is a little too high for most of the case.
So you need to manually tweak these settings for every single file texture node and that's tiresome.

Instead, I'd like to automate this process using a MEL script.
To make this possible, you need to execute two tasks.
One, select all the file texture nodes in the scene.
Two, for each of these selected file texture nodes, change attribute's values.

Here's the complete script.
This script switches the current Filter Type to Mipmap and set the Filter value to a modest value of 0.5.

/* select all the file texture nodes in the scene and put them in the string array*/
string $selected[] = `ls -type "file" -long`;
/* declare two strings for file node's filterType and filter */
string $filterType, $filter;
/* loop through all the file nodes */
for ($member in $selected) {
    /* get the current attributes */
    $filterType = `getAttr ($member+".filterType")`;
    $filter = `getAttr ($member+".filter")`;
    /* set new values for the attributes */
    setAttr ($member+".filterType") 1;
    setAttr ($member+".filter") .5;
}

Monday, April 30, 2012

MEL: How to apply commands to all the objects selected.

Here's how to apply a command or commands to each and every objects that are being selected.

string $selected[] = `ls -sl`;
     for ($member in $selected)
     {
          commands...
     }

The first line is to list all the selected objects, and put the list into a string array variable called $selected. The second line is a for-loop which will execute the commands for all the selected objects.

Notice that $member variable is for-loop internal variable which doesn't need to be declared.
It is only used inside of the for-loop.

For example, you can rename all the selected objects at one shot.

string $selected[] = `ls -sl`;
     for ($member in $selected)
     {
          rename $member "myObject#";
     }

If you execute this script, your selected objects will become myObject1, myObject2, myObejct3....


Here's another example.
You can add a new attribute to all the objects you select.
Let's add miLabel attribute.

string $selected[] = `ls -sl`;
     for ($member in $selected)
     {
          addAttr -ln miLabel -at long -min 0 -max 10 -dv 1 $member;
          setAttr -e keyable true ($member +".miLabel");
     }

This script is to add "miLabel" attribute to all the selected objects, and set its initial value to 1.

This is one of the most frequently used mel scripts. It can be used in many different ways.

Saturday, April 28, 2012

How to save a final gather map file for each frame


When you use the final gathering for animation, you'd probably have to refresh final gather map and rebuild it from scratch at every frame unless it's a camera fly-thru only animation.
In that case, you'd have to use very high settings for the final gathering and each frame would take a good amount of rendering time. What if you have re-render the entire sequence for some reason.
All the final gather maps that you previously calculated has been over-written and only the final gather map for the last frame remains. What a waste.
So, I'll show you a simple way to save all the final gather maps that are created during the rendering.

 
You can access and control the final gather map's name by the attribute called
"miDefaultOptions.finalGatherFilename"

You can simply set this attribute to a file name of your choice using setAttr command like below.
The filename goes between the parenthesis. 


setAttr -type "string" miDefaultOptions.finalGatherFilename ("FGmap_save."+`currentTime -q`+".fgmap");

Here, my scene file name is" FGmap_save.mb", so I typed "FGmap_save.".
You can use your scene file name or any name.
Each fgmap name will be unique by quoting the current frame number using `currentTime -q` command and appending that to the file name.


This one-line script goes to Pre render frame MEL slot under Render Options  in Common tab.


This mel script will be executed before every render begins at each frame.
Now you can find that for every test render,  the Primary Final Gather File name updates for the current frame.

Now render your sequence with FG rebuild option ON and your FGmaps will be saved for each and every frame. 

When you need to re-render any frames or the entire sequence, just switch the Rebuild option to Freeze and render again. The renderer will locate the corresponding saved FGmap for each frame.




This can be a big time saver especially when you find that you need to adjust the Point Interpolation attribute only after the sequence is once rendered.