Home Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson H

Lesson 5 - Maya Functions/Challenges

In this lesson we'll learn a few frequently used Maya functions and use them to solve some problems.

-----------------------------------------------------------------------------------------------------

Maya List Objects:  mc.ls()

Without any arguements like this, this will return all objects in the scene in an array.

So if we run (you'll see this line in most of my scripts): allObjects = mc.ls()

then all objects in the scene will be stored in allObjects.

A useful arguments you can pass to filter the selection is typ. For example  mc.ls(typ='transform') will return only the transform nodes in the scene.

-----------------------------------------------------------------------------------------------------

Select Object:  mc.select(nodeName)

Pretty self explanatory. nodeName should be a string of the object you want to select. You can pass the argument r=True, to force the selection to replace other selections: mc.select(nodeName, r=True)

-----------------------------------------------------------------------------------------------------

Node Type:  mc.nodeType(nodeName)

This one is very useful. It returns the type of the node you pass

-----------------------------------------------------------------------------------------------------

We saw above how we could use mc.ls with the typ argument to return all transform nodes. Using nodeType and ls, we could select achieve the same goal with:

allObjects = mc.ls()
transformObjects = []
for obj in allObjects:
    if mc.nodeType(obj)=='transform':
        transformObjects.append(obj)

This may seem like a bit of a waste (5 lines to do one line's job) but it may give us some extra control over how we filter our list. In fact we could achieve the same thing in one line using list comprehension (which I won't explain here but you can read about here

transformObjects = [obj for obj in mc.ls() if mc.nodeType(obj)=='transform']

-----------------------------------------------------------------------------------------------------

List Relatives:  mc.listRelatives(nodeName)

This lists child/parent related nodes to an input node called nodeName (string). For example if we have a shape node called 'pSphereShape1', we can query it's transform node using the 'p' argument:

mc.listRelatives('pSphereShape1', p=True)

This will return an array of all parent nodes, so if we want to store just the first one in a variable called transformNode, we would execute this:

transformNode = mc.listRelatives('pSphereShape1', p=True)[0]

-----------------------------------------------------------------------------------------------------

List Connections:  mc.listConnections(nodeName)

This lists all input/output connections to the given node. If this function is called without any arguments, it will query connections on selected objects

-----------------------------------------------------------------------------------------------------

Get Attribute:  mc.getAttr("nodeName.attributeName")

This return the value of "nodeName.attributeName". It will error if the attribute doesn't exist:

mc.getAttr("lambert1.color") will return [(R, G, B)] where R G and B are floats of the red, green and blue values from 0-1

-----------------------------------------------------------------------------------------------------

Set Attribute:  mc.setAttr("nodeName.attributeName", value)

This sets the value of "nodeName.attributeName". It will error if the attribute doesn't exist:

mc.setAttr("polySphere2.radius", 27) will set the radius on polySphere2 to 27

-----------------------------------------------------------------------------------------------------

Test if Object Exists:  mc.objExists("nodeName") or mc.objExists("nodeName.attributeName")

This queries maya for a node or an attribute and returns True if it exists and False if it doesn't:

mc.objExists("lambert1.color") returns True

mc.objExists("lambert1.wellclearlythisdoesntexists") returns False

-----------------------------------------------------------------------------------------------------

LESSON 5 TASKS:

  • print all lamberts in your scene.

  • using the argument materials=True within mc.ls, store all materials in your scene in an array called materialArray.

  • For every entry in this array. Print its shading group if it exists, and print 'this has no shading group' if it does not have an SG hint: a shading group is a connection to a material, but what nodeType is it?

  • create an array of unattached shaders

-----------------------------------------------------------------------------------------------------

lesson5_tasks.py