colorAtPoint Maya command

Posted by on Apr 8, 2012 in animation, MEL | No Comments

Expressions for transforming nodes via pixel values.

string $cubes2[] = `ls -type transform "newCube*"`;
for( $each in $cubes2) {
float $u = `getAttr($each + ".translateX")`;
float $v = `getAttr($each + ".translateZ")`;
float $capValueF[] = `colorAtPoint -o RGB -u (($u+40)/80) -v (($v+40)/80) file1`;
setAttr ($each + ".scaleY") (($capValueF[0]+$capValueF[1]+$capValueF[2])*10);
setAttr ($each + "Lam.incandescenceR") $capValueF[0];
setAttr ($each + "Lam.incandescenceG") $capValueF[1];
setAttr ($each + "Lam.incandescenceB") $capValueF[2];

Maya particle instancing with real flow rain drops

Posted by on Nov 26, 2010 in code, dynamics, Maya, MEL | One Comment

Create animated instances geometry at a particle collision.

For my raindrop splash, I am using an animated sequence of geometry, created in realflow (70mb). The realflow bin sequence is a single node after it is imported to Maya, it needs to be converted (duplicated) to a sequence of Maya nodes, multiple nodes. Use the following MEL script to convert the bin to multiple nodes.

//Select your realflow imported bin
string $bin[] = `ls-sl`;
//this is set up to to "convert" (duplicate) the bin from frame 1 - 100. Set the frame range that you want to convert below.
for( $index = 1; $index <= 100; $index++ )
currentTime $index;
select $bin[0];
select -cl  ;

Now you should have a sequence of separate meshes, each representing a frame of your instanced animation. These will be loaded into the “particle instancer” to allow for animation at the point where each particle collides.

Create a collision event for your particle
1. Select particle and collision object and go to Menu : Particles > Make Collide
2. Set a particle collision event for your selected particle. Particles > Particle collision event editor. Set: event type to “emit”. Set: inherit to 0. Set: 1 particle emitted per collision. Set: Original Dies.
3.Press the create event button. A new particle object will be created.

Instance the geometry to the newly created particle.
1.Go to Menu>Particles > Instancer options
2. Load your sequence of separate meshes into the “Instanced Objects” section. Make sure that your particleShape is NOT loaded into the create instancer as an instanced object as it will try and instance itself.
3. Set: particle object to instance to : particleShape2 or whatever the particle object that was created by the collision event.
4. Select the newly created particle object shape. In the attribute editor, set: the lifespan of the particle to the duration of you splash object geometry sequence (number of individual nodes). The lifespan is in seconds…so you will need to do the math to get your frame count..

Tracking the position of a particle collision.

Posted by on Nov 23, 2010 in code, dynamics, Maya, MEL | No Comments

Rough guide:

  1. Create planes, one above the other, scale to 10 units square, and place in positive unit space.
  2. Select the top plan. Make it emit particles: Particles > Emit From Object >options (select “surface” as emitter type).
  3. Make particles and ground plane collide. Select the particles, Shift-select the plane, then select Particles > Make Collide.
  4. Select particlesShape, add CollisionU and CollisionV attributes. Set the lifespan attribute to LifespanPP only.
  5. Create a locator called loc.
  6. Create a dynamic expression with the code below.
float $colU = particleShape1.collisionU;
float $colV = particleShape1.collisionV;

if ($colU > 0) {
	loc.translateX = ($colU*10);
	loc.translateZ = ((1 - $colV) * 10);

Scene file.

determining the bounding box of a Maya object

Posted by on Nov 22, 2010 in code, how to, Maya, MEL | No Comments
float $boboArray[] = `polyEvaluate -b pCube1`;

for a default cube, this will return : // Result: -0.5 0.5 -0.5 0.5 -0.5 0.5 //, the units returned correspond to the 6 coordinate locations of the cubes vertices.

float $boboArray[] = `polyEvaluate -b pSphere1`;

for a default sphere, this will return : // Result: -1 1 -1 1 -1 1 // , the units returned correspond to the coordinate locations of the a cube that is “fitted” to the sphere’s bounding box.

Collision Detection in Maya

Posted by on Oct 13, 2010 in code, Maya, MEL | 2 Comments

Based on this tutorial. You will need to download and source this script.

I added attributes to the cubes: .col1(int), .theCol_u(int), and .vel(float) to watch for collisions, “unique” collisions (see the tutorial), and the velocity of the cube, respectively.

Two expressions are created for each cube, each rigidBody. The first is created to watch the dynamic rigid body collisions, and the velocity of the rigid body.

// variables
string $myRigidBody = "rigidBody1";
int $contactCount;
int $uniqueContactCount;

//check velocity
vector $theVelocity1 = `getAttr rigidBody1.velocity`;
float $velCheck1 = ($theVelocity1.x + $theVelocity1.y + $theVelocity1.z);
float $absVel = `abs $velCheck1`;

pCube1.vel = $absVel;

// get $contactCount
$contactCount = `getAttr($myRigidBody+".contactCount")`;

// compute theCol_u
if ($contactCount > 0)
  string $array[] = `rigidBody -q -contactName $myRigidBody`;
  string $flatArray[] = flattenStringArray($array);
  $uniqueContactCount = size($flatArray);
  $uniqueContactCount = 0;

// set interface attributes
pCube1.col1 = $contactCount;
pCube1.theCol_u = $uniqueContactCount;

The second expression affects a shader, based on the collision and velocity values collected by the expression above.

int $theTime = `currentTime -query`;
if ($theTime > 0)
	if (myLam1.incandescenceR >.05)
		myLam1.incandescenceR = myLam1.incandescenceR -.075;
		if (pCube1.vel > 0.2)
			if ( pCube1.theCol_u >=1)
				myLam1.incandescenceR = 2.5;
			myLam1.incandescenceR = 0;

create a grid of cubes

Posted by on Sep 29, 2010 in code, how to, Maya, MEL, python | No Comments

Grid of cubes in Maya.

# create a grid of cubes, their bottom plane set to zero y, and the center point at zero y.
x = -20
count = 0
cubes = []
while x <= 20:
	z = -20
	while z <= 20:
		cube = mc.polyCube( name = ('polyCube_' + str(count) + '_' + str(x) + str(z)) )
		count +=1
		mc.setAttr((cubeName + '.translateY'), .5);
		mc.makeIdentity(apply=True, t =True, r=True, s=True, n= 0)
		mc.move(x, 0, z)
		z += 1
	x +=1

Block of cubes

import maya.cmds as mc
# create a block grid of cubes
# change the mult number below to change the amount of cubes created.
mult =  3;
count = 0
cubes = []
y = mult - mult
while y <= mult *2:
    x = mult * -1
    while x <= mult:
	    z = mult * -1
	    while z <= mult:
		    cube = mc.polyCube( name = ('cube_' + str(count) ) )
		    count +=1
		    mc.move(x, y, z)
		    z += 1
	    x +=1
    y +=1

Make a 3d cube grid, based on a selected object’s bounding box.

proc makeCubeGrid(int $cubeGridDiv)
    string $cube[];
    string $cubes[] = {};
    string $sel[] = `ls -long -sl`;
    float $bBox[] = `polyEvaluate -b -ae $sel[0]`;

    float $w = ($bBox[1] - $bBox[0]) / $cubeGridDiv;
    float $h = ($bBox[3] - $bBox[2]) / $cubeGridDiv;
    float $d = ($bBox[5] - $bBox[4]) / $cubeGridDiv;

    float $p[3];
        $p[0] = $bBox[0] + (($bBox[1] - $bBox[0]) / $cubeGridDiv) * $i;
            $p[1] = $bBox[2] + (($bBox[3] - $bBox[2]) / $cubeGridDiv) * $k;
                $p[2] = $bBox[4] + (($bBox[5] - $bBox[4]) / $cubeGridDiv) * $n;
                $cube = `polyCube -w $w -h $h -d $d -sx 1 -sy 1 -sz 1 -ax 0 0 0 -ch 0`;
                move ($p[0] + $w / 2) ($p[1] + $h / 2) ($p[2] + $d / 2) $cube[0];
                makeIdentity -apply true -t 1 -r 1 -s 1 -n 0 $cube[0];
                $cubes[`size $cubes`] = $cube[0];
    string $group = `group -em -n "cubes"`;
    parent $cubes $group;


Create a Graphic EQ using SoundKeys and Maya

Posted by on Sep 26, 2010 in animation, code, Maya, MEL, python | 10 Comments

You can create this graphic equalizer with Trapcode Soundkeys and Maya. Use my Python script, below, to automatically import keyframe data and create the eq.

Trapcode Soundkeys, an Adobe After Effects plugin, breaks audio into into about 27 frequency blocks, “ranges”, and allows you to drive animation, set keyframes, or export keyframes. To export keys, copy the keyframes from the After Effects timeline, then paste into a text editor to create your data.txt files. Each frequency range should have it’s own text file, named sequentially. Use a code editor like BB Edit or Text Wrangler, as these apps will save your .txt files with UNIX line returns, which are necessary to make my Python script work properly.

Place all the data files in a directory, by themselves, and use the Python script below to automatically build the EQ in Maya.

## make a graphic equalizer, based on keyframe data from After Effects and Sound Keys.
import os
import maya.cmds as mc
## add path to keyframe data files - just the source directory that contains them.
theAttribute = 'data'
count = 0
theCubes = []
for subdir, dirs, files in os.walk(rootdir):
	dataFiles = [each for each in files if each.endswith('.txt')]
	for thisFile in dataFiles:
		## create a cube for each data file, and distibute them across the  X axis
		cube = = True)
		mc.setAttr((cubeName + '.translateY'), .5);
		mc.makeIdentity(apply=True, t =True, r=True, s=True, n= 0)
		mc.move( count -(len(dataFiles)/2), 0, 0)
		count +=1
count = 0
for subdir, dirs, files in os.walk(rootdir):
	dataFiles = [each for each in files if each.endswith('.txt')]
	for thisFile, o in zip(dataFiles, theCubes):
		## prepare each cube for animation by ading an attibute to accept the keys, and creating an expression
		theName = theCubes[count]
		mc.addAttr( theName, shortName='data', longName='data', defaultValue=1.0)
		mc.setAttr ( (theName + '.data'), e=True, keyable=True)
		mc.expression(o=theName, s = (theName + '.scaleY =' +  theName + '.data / 4'),  ae = True, uc = all)
		## make shader for each cube
		myBlinn = mc.shadingNode('blinn', asShader=True, name = ("myBlinn" + str(count)))
		myShader = mc.sets(renderable=True, noSurfaceShader=True, empty=True, name = ('myBlinn' + str(count) + 'SG'))
		mc.connectAttr( (myBlinn + ".outColor"), (myShader + ".surfaceShader"), f=True)
		mc.setAttr((myBlinn + ".color"), 0.1, 0.1, 0.1, type ='double3' )
		mySetRange =  mc.shadingNode('setRange', asUtility=True)
		mySetRange2 = mc.shadingNode('setRange', asUtility=True)
		mc.setAttr((mySetRange + ".minX"),230)
		mc.setAttr((mySetRange + ".maxX") ,240)
		mc.setAttr((mySetRange + ".oldMaxX") ,25)
		mc.setAttr((mySetRange2 + ".maxX") ,3)
		mc.setAttr((mySetRange2 + ".minX") ,.1)
		mc.setAttr((mySetRange2 + ".oldMaxX") ,25)
		myRamp = mc.shadingNode('ramp', asTexture=True)
		my2dPlace = mc.shadingNode('place2dTexture', asUtility=True)
		mc.connectAttr((my2dPlace + ".outUV"), (myRamp + ".uv"))
		mc.connectAttr((my2dPlace + ".outUvFilterSize"), (myRamp + ".uvFilterSize"))
		mc.setAttr((myRamp + ".colorEntryList[2].color"), 0, 0, 0 , type = 'double3')
		mc.removeMultiInstance((myRamp + ".colorEntryList[0]"), b=True )
		mc.removeMultiInstance(myRamp + ".colorEntryList[1]", b=True )
		mc.connectAttr( (theName + ".scaleY"), (mySetRange + ".valueX"), f=True)
		mc.connectAttr( (theName+ ".scaleY"), (mySetRange2 + ".valueX"), f=True)
		mc.connectAttr( (theName + ".scaleY"), (mySetRange2 + ".valueY"), f=True)
		mc.connectAttr( (theName + ".scaleY"), (mySetRange2 + ".valueZ"), f=True)
		myhsvToRgb = mc.createNode('hsvToRgb', ss=True)
		mc.setAttr((myhsvToRgb+".inHsv"), .75, .75, .75)
		mc.connectAttr( (mySetRange + ".outValueX"), (myhsvToRgb + ".inHsvR"), f=True )
		mc.connectAttr( (myRamp  + ".outColor"), (myBlinn + ".incandescence"), f=True )
		mc.connectAttr( (myhsvToRgb + ".outRgb"), (myRamp+ ".colorEntryList[2].color"), f=True )
		mc.connectAttr( (mySetRange2 + ".outValue"), (myRamp + ".colorGain"), f=True )
		mc.sets( e=True, forceElement= myShader)
		count +=1
		## read keyframe data and set keyframes
		dataFile = open((os.path.join(rootdir, thisFile)), 'r')
		lines = dataFile.readlines()
		mylines = lines[10:-4]
		for eachLine in mylines:
			data = eachLine.split('\t')
			theFrame = data[1]
			theValue = data[2]
			mc.setKeyframe( o, v=float(theValue), at=theAttribute, t =float(theFrame))
## the code below will set the playback range to equal the amount of the keyframe data in the files
fileData = open(rootdir + '/' + dataFiles[0])
lines= fileData.readlines()
myLines = lines[10:-4]
countLines = len(myLines)
mc.playbackOptions( minTime='0sec', maxTime=countLines)

Sample keyframe data, exported from After Effects.

Python import/export Maya keyframes script

Posted by on Sep 19, 2010 in code, MEL, python | No Comments

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 =
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

Maya Gamma Node MEL Script

Posted by on Jul 5, 2010 in code, Maya, MEL | 3 Comments

If you are working linear in Maya, you probably add gamma correct nodes between each texture and it’s material.

This MEL script will add a gamma correct node (set to gamma .45) between a selected texture and it’s selected target material, and it will attach the network to the material’s color attribute.

Before you run the script:
first select the material and the texture that you want to attach to it.

string $mySel[] = `ls -sl`;
string $myMaterial[] = `ls -mat $mySel`;
string $myTex[] = `ls -tex $mySel`;
string $myGamma = `shadingNode -asUtility gammaCorrect`;
setAttr ($myGamma + ".gammaX") 0.45;
setAttr ($myGamma + ".gammaY") 0.45;
setAttr ($myGamma + ".gammaZ") 0.45;
connectAttr -force ($myTex[0] + ".outColor") ($myGamma + ".value");
connectAttr -force ($myGamma + ".outValue") ($myMaterial[0] + ".color");

Simple backdrop MEL script

Posted by on Jun 8, 2010 in code, lighting, Maya, MEL, rendering | No Comments

This MEL script will create a simple soft cornered backdrop from a poly plane and bend deformer.

XPM icon for shelf item.

Load More