Rymbrant
Rymbrant

Reputation: 17

Best way to get Shape from a Transform

Using ls -sl returns a transform. The only way I can find to get the shape of a transform is to use getRelatives but this seems wonky compared to other workflows. Is there a better more standard way to get a Shape from a Transform?

Upvotes: 0

Views: 6340

Answers (3)

Regnareb
Regnareb

Reputation: 144

Even though Pymel is more pythonic and can be more pleasant as a dev to use than maya.cmds, it is not officially supported and brings its share of bugs, breaks and slowness into the pipeline. I would strongly suggest to never import it. It is banned in a lot of big studios.

Here is the solution with the original Maya commands and it's as simple as that:

shapes = cmds.listRelatives(node, shapes=True)

Upvotes: 1

Rive
Rive

Reputation: 1

Be aware, as of 2018, the pymel getShape() is flawed (IMO) in that it assumes there is only one shape per node, and that is not always the case. (like 99% of the time, its the case though, so I'm nitpicking)

However; the getShape() method only works off of a transform nodeType. If you have an unknown node type that you are trying to parse if its a mesh, or a curve for instance, by saying getShape() you'll want to check if you can use the method or not.

if pm.nodeType(yourPyNode) == 'transform': 'shape = yourPyNode.getShape()

If parsing unknowns: listRelatives() command with the shape or s flag set to true

selected_object = pm.ls(sl=True)[0]
shapes = pm.listRelatives(selected_object, s=True)
    if len(shapes) > 0:
    for shape in shapes:
        # Do something with your shapes here
        print('Shapes are: {}'.format(shape))

# or more pymel friendly

shapes = pm.selected_object.listRelatives(s=True)
for shape in shapes: 
    # Do something in here

Upvotes: 0

Priyabrata Biswal
Priyabrata Biswal

Reputation: 22

Very standard way of getting shape from transform in PyMEL:

transform.getShape()

To get shapes from a list of selection, you can do the following which results in list of shapes.

sel_shapes = [s.getShape() for s in pm.ls(sl=1)]

A note that certain transforms do not have shapes. Like a group node, which is basically a empty transform.

Upvotes: -1

Related Questions