Tagged: MEL

Python import/export Maya keyframes script

These are fairly rudimentary python scripts, to export keys from Maya and import them back into Maya. Creates a key at every frame. May be interesting to people learning Python.

Export Maya Keys
import maya.cmds as mc
# Select all nodes with keys to export
# Customize the path below so that it points to, or creates, the file where you want to store the keyframe data
myFileObject=open(‘/mydataDir/data.txt’, ‘w’)
obs = mc.ls(sl=True)
theData = []
minTime = mc.playbackOptions(query=True, minTime=True)
maxTime = mc.playbackOptions(query=True, maxTime=True)
attributes = [‘translateX’, ‘translateY’, ‘translateZ’, ‘rotateX’, ‘rotateY’, ‘rotateZ’, ‘scaleX’, ‘scaleY’, ‘scaleZ’, ‘visibility’]
for time in range(minTime -1, maxTime +1):
count = 0
for selection in obs:
name = obs[count]
count +=1
for theAttribute in attributes:
myAtF = mc.getAttr(selection + ‘.’ + theAttribute)
myAt = str(myAtF)
myTime = str(time)
theData.append(myAt + ‘ ‘ + myTime + ‘ ‘ + theAttribute + ‘ ‘ + name + ‘ \n’)
for lines in theData:

Import Maya Keys
This will import keys to nodes which are named identically to the nodes that were selected when the keys were exported.
import maya.cmds as mc
# Customize the path below so that it points to the file where you have exported the keyframe data
myFileObject=open(‘/mydataDir/data.txt’, ‘r’)
theLines = myFileObject.readlines()
count = 0
for line in theLines:
theLine = theLines[count]
theSplit = str.split(theLine)
theValue = theSplit[0]
theFrame = theSplit[1]
theAttribute = theSplit[2]
theName = theSplit[3]
mc.setKeyframe( theName, v=float(theValue), at=theAttribute, t =float(theFrame))
count +=1

Studio Light HDR images

Studio Light HDR images, photographed from real studio lights. Download the images below – and PyMEL scripts to automatically attach them to a lighting rig.

These HDRIs can be used as image based lights and reflection cards.

Soft Box

Get the soft box HDR. 1 MB

Umbrella Light

Get the umbrella light HDR. 8 MB


Newly revised PyMEL Scripts

Recently updated the light rig scripts here with PyMEL. These should work better for people using later versions of Maya. Please comment or contact me if you have any problems with these scripts. You can locate the old MEL scripts here.

Get a PyMEL(Python) script that will rig the Soft Box HDR to a reflection card with spotlight, shadows, and other controls. Note: for this script to work properly, you will need to download the soft box HDR folder and place it in your Maya project’s sourceimages directory. XPM icon for shelf item.

Get a PyMEL(Python) script that will rig the Umbrella HDR to a reflection card with spotlight, shadows, and other controls. Note: for this script to work properly, you will need to download the umbrella HDR folder and place it in your Maya project’s sourceimages directory. XPM icon for shelf item.

Installation tip: Make sure that the HDR images are placed in the currently set Maya project. These images should also be within their respective folder just as they are when you download them. The folder containing the HDR images should therefore be named either “softBox” or “umbrellaLight”, depending on which one you downloaded.

Usage tip: Select the locator on the rig, called “softBoxLightControl”, to find attributes to control Color, Shadows, and light Intensity.

note: The easiest way to position the light, without affecting the “roll” orientation of the reflector card, is to position (translate) the locator in one plane (ex: the X Y plane) to set the distance from the subject and light angle, then rotate the top node (pick-walk up once) in the Y axis to set the “horizontal” orientation.

If you are using another 3D animation package, download an OBJ, with UVs set up to use with the Umbrella HDR.

Also see my procedural softbox MEL script here. My most useful script, IMO.

Many thanks to Daniel Vasquez of heylight.com for translating the MEL script into PyMEL.

Also please check out my iOS apps.

Create Maya shaders that react to key frames

This MEL script was written specifically to automatically generate shaders for the equalizer in the animation below.

The script is set up to generate and assign shaders and attach the .translateY attribute of selected objects to the incandescence of the newly generated shader. Also creates a network that cycles the colors a bit at the same time.

Here is a snapshot of the network:

Get the script.

MEL renaming

Maya has a built in re-naming tool, Rename Selected Objects, on the right side of the Status Line. Select Rename from the menu of line input operations, to the left of the input box.

image of Maya's Rename selected objects dialog

Basic rename script using Python:

#rename selected Maya nodes. Adds 4 digit padding to numbered name.
import maya.cmds as mc
mySel = mc.ls(sl=True)
count = 0
for obj in mySel:
count +=1
#customize name below
mc.rename( obj, ‘myObj2_’ + str(‘%04d’ % count))

Below are various methods to rename using MEL:

Simple approach

int $i; //Select the object(s) that you want to rename string $rename[] = `ls -sl`; int $selSize = `size $rename`; for ($i =0; $i <$selSize ; $i++){ //Change newName_, below, to the name that you want to replace. rename ($rename[$i]) ("newName_" + ($i +1)); }

Search and replace.
Designed to find and replace part of the name of similarly named nodes.

searchReplaceNames "myNode" "newNode" "selected";

This will change names in a selection. For example you can change 100 selected nodes names from “myNode000 – myNode100” to “newNode000 – etc”. You can replace “selected” with “hierarchy” to rename all of the nodes in a selected hierarchy, or “all” to rename all nodes in the scene.

Search and replace 2

string $search = "myNode"; string $replace = "newNode"; string $Objs[] = `ls -sl`; for($obj in $Objs) { if(`startsWith $obj $search`) { rename $obj (`substitute $search $obj $replace`); } }

Rename and add custom number

//by pappapierre
//catch the object(s) current names, and clear your selection
string $sel[] = `ls -sl`;
select -cl;

//declare a string variable which will become your new name prefix

string $newNamePrefix = "myNewName";

//now run a loop to rename each elemnt in your selection list in the order
//they were selected
for($i=0 ; $i<size($sel); $i++){

//and rename your objects accordingly
rename $sel[$i] ($newNamePrefix + ($i+1));

//you can start your numbers anywhere by replacing the "1" in ($i+1) with
//your desired starting number.

Rename shape nodes:

//script 1. rename shapes unique names
string $rename[] = `ls -sl -dag -shapes`;

for ($i = 0; $i<size ($rename); $i++) {

rename($rename[$i]) ("myNewShapeName_" + ($i +1));


//script 2. remnameShapes (all the same name)

for ($i in `ls -sl -dag -shapes`) {

rename $i "myShape";


//script 3. rename types

for ($i in `ls -sl -dag -type “nCloth”`) {

rename $i "myShape";

MEL change attribute on multiple objects

Using the channel box or the attribute spreadsheet are good ways to manually change attributes on multiple selected objects.

In MEL, this script is set up to change attributes on selected nodes in the scene.

string $mySel[] = `ls-sl`; string $name; int $arraySize = `size $mySel`; for ($i = 0; $i < $arraySize; $i++) { $name = $mySel[$i]; $myNode = ($name + ".rotateX"); setAttr $myNode 100; }

If you want to make changes to the shape node, you will want to use ls -sl -dag -leaf:

string $mySel[] = `ls -sl -dag -leaf`; for ($each in $mySel) { setAttr ($each +".attribute") 1; } print $mySel;

Make Studio Lights MEL Script

Maya MEL script that some may find use useful. It’s called Make Studio Lights, and it does just that. It creates lights featuring procedurally textured reflection cards that simulate softbox and umbrella reflectors. These can work well in combination with indirect illumination in Mental Ray. I want to improve this, and will upgrade the script as I can.

Get the script.

A sample render scene.


See this link for a new version of the softbox light, the control is somewhat easier to use in this version.

See this link for a newer HDR image based version of this script. May have improved results and easier to control custom manipulator.