Lets save an object into a variable:
Creates a new cube object.
Let’s figure out how to store this same value in a variable, so that our code relates directly to our object name in the outliner.
Firstly lets delete the existing pCube1 in the outliner. Now lets store the new object in a variable:
cube = cmds.polyCube()
The cube is stored in the variable “cube” outlined above. Our code will now look like this:
from maya import cmds cube = cmds.polyCube()
Lets run this code. You’ll see that a new cube has been created, and again in the outliner (windows > outliner) we can see that the object once again appears as ‘pCube1’.
To see how Maya is referencing this object, we add an additional line of code to the script to ‘print’ the variable attributes:
from maya import cmds cube = cmds.polyCube() print cube
Now if we run the code again you’ll see that in the output window some additional information about the object we stored in the variable ‘cube’:
But what does this mean?
[…]= Square brackets mean that this is a ‘list’ containing one or more strings.
u= Unicode string, we can ignore this, its something that Maya does.
pCube1= The name of the cube object itself.
polyCube1= The name of the Maya node that created the cube.
So by saving the object in a variable we are saving the string “[u’pCube1′, u’polyCube1′]”, which contains a definition for both the object and the Maya node. Lets say we want to extract the object only, how would we do this?:
from maya import cmds cube = cmds.polyCube() cubeShape = cube
Stop! Delete the previous cube from the outliner
Lets update our code to print the new variable:
from maya import cmds cube = cmds.polyCube() cubeShape = cube print cubeShape
If we run this new code, we’ll get the following output for the cubeShape variable:
Notice the absence of square brackets, this is no longer a list and is a single object. It is the first object in the list stored in the variable ‘cube’.