Friday, March 11, 2016

Simple Maya Script Snippets

 I sort of started doing the rigging dojo challenge of putting out a super simple script snippet a day, that not only does "something" but also displays something interesting in the world of scripting for Maya . None of this is mind bending and I'm sure not new to a lot of people, but maybe it will help someone with an intro


day one:
so this is a super simple script to just get the number of objects you have selected.. I have gotten so use to writing this script that I tend to just pop it into the script editor without thinking
the nice thing here is I'm not creating any variables so they wont be global or overwrite anything in my current Maya session.
mel:
size `ls -sl`;
python:
len(cmds.ls(sl=True))

also since the "select" command does not have a type flag. I use this same sort of method for quickly selecting all of a given node type
mel:
select `ls -type "mesh"`;
python:
cmds.select(cmds.ls(type='mesh'))


day two:
ok, so this one is not so different from the previous, so I'll just call it "super simple script" that can be helpful ( i just ran out of time today)
This one just takes your current object's type and makes a set of every object of that type in your scene . Since I'm dropping this on the shelf or running in the script editor, I scope it inside curly braces to keep the variables from going global. I highly recommend scoping whenever you are prototyping in the script editor with mel.. or you will bang your head against an illegal re-declaration , or an array of the wrong items from time to time.

string $s[] =`ls -sl ` ;
string $c[] =`listRelatives -c -s $s[0]`;
string $type = `nodeType $c[0]` ; 
sets -n ($type + "s") `ls -type $type`;
}
for the Python version I was able to get rid of all the variables as you can nest functions better ( with Mel you only can do it once)
(the following should read as just 2 lines, the first being the import of the maya commands module )
import maya.cmds as cmds
cmds.sets(cmds.ls(type=cmds.nodeType(cmds.listRelatives(cmds.ls(sl=True)[0],c=True,s=True))),n='SET')


day three: 
this is an experiment in fixing up an old script and streamlining it a little bit. the top this time is in Python, and I added the nope variable & print statement just to demonstrate that nope is actually list of booleans and I have to A) pull out the first item using [0] and then use str() to force it into a string for printing
sel=cmds.ls(sl=True,dag=True,shapes=True,noIntermediate=True)
for e in sel:
nope =not(cmds.displaySurface(e,query=True,xRay=True))[0]
print ("the ! issue is : " + str(nope) ) 
cmds.displaySurface(xRay=not(cmds.displaySurface(e,query=True,xRay=True))[0])
pretty much the same thing, but I don't have the same power in MEL to pull the first item out of the int array, and so I have to first make an int array variable and then use that in the action part of the script,, note that in MEL you can use ! for ( opposite or not) while in Python it's "not" to do the same thing
{
string $sel[] = `ls -sl -dag -s -noIntermediate`;
for ($each in $sel)
{
int $x[] =`displaySurface -q -xRay $each`; 
displaySurface -xRay ( !$x[0] ) $each;
}
}

No comments:

Post a Comment