Correct way to get shape node from transform node?


#1

What is the correct way to get the shape node from the transform node?


import maya.cmds as cmds

theCam = (cmds.ls(selection=True))
if len(theCam) == 1:
    theCam = theCam[0]
    result = cmds.objectType( (theCam + "Shape"), isType='camera' )
    print(result)

Thanks

Dave


#2

I think you’re looking for the listRelatives command:

selected_items = cmds.ls(selection=True)
if selected_items:
    shapes = cmds.listRelatives(selected_items[0], shapes=True)
    if shapes:
        print shapes[0]

#3

With PyMel, just use node.getShape. I do strongly recommend using PyMel rather than cmds.


#4

I’m still so confused as to why there is two… PyMel sounds better but I keep getting told it’s slower.


#5

I’ve only found PyMel notably slower for bulk things. If I’m reading bulk vertex data from a mesh I’ll drop to cmds or OpenMaya. PyMel scales to complex code much better. It doesn’t lose track of nodes if they get renamed (it tracks by object) so you can keep object references around without them becoming invalid all the time. It also catches errors earlier, eg. if you call node.attr(‘foo’) you get the “attribute not found” error, where with “cmds” you’ll only get an error when you try to use it (by which point it might be hard to tell where the attribute came from). It’s a lot nicer to retrieve attributes like that, too, compared to stuff like setAttr(’%s.foo’ % node).


#6

“I’m still so confused as to why there is two… PyMel sounds better but I keep getting told it’s slower.”
PyMel casts into PyNode objects so that you get tons of utility methods attached to anything cast in Maya. It is incredibly powerful and most often you will not “feel” the casting.

If you are going to iterate through a ton of vertices, you will start noticing the casting and it might be time to turn to OpenMaya.

There are 2 options because AD didn’t make a very pythonic implementation in Maya. maya.cmds is not object oriented. Chad Dombrova spearheaded PyMel and tons of people adopted it. AD recognized the adoption and started bundling it in with Maya.

import pymel.core.general

selectedTransform = pymel.core.general.selected()[0]
for vert in selectedTransform.getShape().verts:
    if vert.isOnBoundary():
        doSomething()

Simple example, but it illustrates how fast you are able to do things when using PyMel and how readable it is.

This is an outdated article, but scroll down to the “The Languages Of Maya” to give you a better overview…
http://christianakesson.com/python-art-pipelines/ - search for “The Languages Of Maya”

Hope that helps somewhat,
/Christian Akesson


#7

Thanks very much Christian, that’s very interesting. Coming from 3dsmax I’m far more used to an object-orientated system for dealing with nodes. Makes a lot more sense, thanks!


#8

You are welcome and I am glad to hear it! :slight_smile:

Cheers
/Christian Akesson