All posts by Gary Nisbet

Visualise a Field [Maya]

Hey,

So I wanted to visualise a field in maya, as I needed to know what it was looking like, and I found a pretty simple way to do it.

Create a 3D fluid container:
Dynamics > Fluid Effects > Create 3D Container

Connect your field to the container:
Window > Relationship Editors > Dynamic Relationships

In the fluid container, turn on velocity draw:
Display > Velocity Draw

Feel free to change the options, increase the resolution of the container if you need more fidelity.

Enjoy.

Gary

Add notes to a node in maya with python

So sometimes you want to add some notes to your objects in maya, the quick python script below will add a string attribute to the node you input

from maya import cmds

def add_notes(node = None, default_text = "Input note here"):
    
    if not node:
        sel = cmds.ls(sl=1)
        if sel:
            node = sel[0]
        else:
            cmds.warning("Please select a node to apply the notes field too")

    if not cmds.attributeQuery("notes", n = node, ex = True):
        cmds.addAttr(node, ln = "notes", sn="nts", dt="string")
        cmds.setAttr("%s.notes"%node, default_text, type="string")
    else:
        cmds.warning("%s already has a notes attribute"%node)

add_notes("my_node_name", "Remember to rewind to 0 when opening this file")

notes_val = cmds.getAttr("%s.notes"%node)

Cheers,
Gary

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

Enjoy

Using setKeyframe on only the transform and not the shape [Maya]

So I ended up with a few issues when attempting to set the keyframe on an object using the layout specified in the python command, I was trying to do something like:

cmds.setKeyframe("pCube1", value=True, attribute="visibility")

However as I was using instancing, this would also keyframe the visibility on the pCubeShape1 which I didn’t want, even if I explicitly passed in only the name of the transform, to avoid this you need to pass in the transform name and the attribute in the same go, so something along the lines of:

cmds.setKeyframe("pCube1.visibility", value=True)

and this should only change it on the transform.

Good Luck!

Gary

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

Gary

Use an image sequence with texture emission – [Maya]

One of the great bugs in maya is that if you want to use an image sequence to use within any of its’ nodes, it manages to completely negate the effect of anything other than the first frame.

To fix this just create an expression in your scene and enter this code in to it, replacing the correct attributes as shown by the comments.

// Enter the name of the existing file node you want to drive here. "file1", "weightDrive_file", etc.
string $fileNode="fileIn";</code>

// Enter the directory and first part of the image name, before the numbers, here
// If your image sequence is called "animMap.0001.tif" and its in the images directory of your project
// then you would leave the $imagePrefix variable as it is.
string $imagePrefix="images/animMap";

// This is self explantory with the previous explanation in mind..
string $imageSuffix="tif";

// This is for you to offset the sequence if you need to
int $offset=0;

// Check the image name of the file node to make sure the name string is correct. Adjust previous entries to fix.
string $pythonCmd="import maya.cmds as cmds\noffset="+$offset+"\nframe=int(cmds.currentTime(q=1))+offset\ncmds.setAttr('"+$fileNode+".fileTextureName',('"+$imagePrefix+".'+str(frame).zfill(4)+'."+$imageSuffix+"'),type='string')";
python $pythonCmd;

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`;

Draw a curve/line between two objects [Maya]

So I found this to be pretty useful, I used it to help visualise the direction of the wind I was pumping in to another variable, but it could be used for countless other things, just select the two objects you want to draw a curve between and run this MEL script.

{
string $sel[] = `ls -sl -tr`;
string $locA = $sel[0];
string $locB = $sel[1];
string $crv = `curve -d 1 -p 0 0 0 -p 0 0 0`;
connectAttr -f ($locA+".t") ($crv+".cv[0]");
connectAttr -f ($locB+".t") ($crv+".cv[1]");
}

Laters!

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 &lt; $numCVs; ++$i ) 
{
    vector $low = `pointPosition -w  curve1.cv[0]`;
    vector $high = `pointPosition -w curve1.cv[$numCVs-1]`;
    float $gap = ($high.y - $low.y)/($numCVs-1);
    vector $pos = `pointPosition -w curve1.cv[$i]`;
    select curve1.cv[$i];
    $var = $lowNew.y + $i*$gap;  
    move -y $var;
};

Gary