Category Archives: MEL

Convert Python list in to MEL String Array – [Maya]

Hi there,

So this is a handy little script if you want to pass a python list into a mel command in your code, say you have a list of objects that you now wat to pass in eg:

[u'pCube1', u'pCube2', u'pCube3', u'pCube4', u'pCube5', u'pCube6', u'pCube7', u'pCube8', u'pCube9']

, you’re going to need to convert it to a type that MEL understands, which looks like this:

{"pCube1", "pCube2", "pCube3", "pCube4", "pCube5", "pCube6", "pCube7", "pCube8", "pCube9", "pCube10"}

and the command below will do all this for you, it turns the whole thing in to a string, and then replaces the relevant characters with the ones that MEL understands.

def convertListToMelStringArr(pyList):
    pyList = str([str(x) for x in pyList])
    pyList = pyList.replace("'", "\"")
    pyList = pyList.replace("[", "{")
    pyList = pyList.replace("]", "}")
    return pyList


Randomise Vertex Position – [Maya]

So I found a great little script here that will randomise the vertex positions in all the axis depending on how much you want it to, check it out below!

$mySelection = `ls- sl`;
$myVerts = `getVerts`; // this command grabs all the vertices of selected objects and put into a string array

for ($vert in $myVerts)
    float $min = -50;
    float $max  = 50; 
    float $randNumX = rand( $min , $max );
    float $randNumY = rand( $min , $max );
    float $randNumZ = rand( $min , $max );

    select -r $vert ;
    move -r $randNumX $randNumY $randNumZ ;
select -r $mySelection; // return to previously selected objects


Find the colour at the closest point to a locator

So it’s a bit of an odd one, but I needed to be able to calculate the RGB value of the colour at certain points on the mesh, and at first this seemed a bit tricky, but using the cloestPointOnMesh node and the colorAtPoint command I was able to use a locator and easily connect it up to give me the result I needed, hopefully it will help you out too.

// Select mesh
$mesh = `ls -sl`;

// Select locator
$locator = `ls -sl`;

//Name of the texture
string $texture = "noise1";

//Calculates the position of the locator.
float $position[3] = `xform -worldSpace -query -translation $loc`;

//Calcualtes the closest point on the mesh to the locator.
float $uv [] = `closestPointOnMesh -ip $position[0] $position[1] $position[2] -q -u -v $mesh`;

//Calculates the RGB colour of the texture at the point
float $rgb [] = `colorAtPoint -o RGB -u $uv[0] -v $uv[1] $texture`;

Maya Curves

Here is a little script I found from here, that allows you to query the amount of points (or cv’s) on a curve, where curveShape1 is the name of the curve you have created.

string $curve = "curveShape1";

int $numSpans = `getAttr ( $curve + ".spans" )`;
int $degree   = `getAttr ( $curve + ".degree" )`;
int $form     = `getAttr ( $curve + ".form" )`;

int $numCVs   = $numSpans + $degree;

// Adjust for periodic curve:
if ( $form == 2 ) $numCVs -= $degree;

I then used this to be able to spread the cv’s along the y axis, where ‘curve1’ is the name of your curve.

for ($i = 0; $i < $numCVs; ++$i ) 
    vector $low = `pointPosition -w[0]`;
    vector $high = `pointPosition -w[$numCVs-1]`;
    float $gap = ($high.y - $low.y)/($numCVs-1);
    vector $pos = `pointPosition -w[$i]`;
    $var = $lowNew.y + $i*$gap;  
    move -y $var;