How to store an object by its type on creation?

Hello,
I’m missing something fundamental about retrieving objects by their node type and I could really use some guidance.

When I instantiate an object, I’ll create the object and assign it a variable like so:

my_curve = mc.curve(name="my_curve",d=1,p=[(0,0,0),(1,1,1),(2,2,2)])

#print my_curve.getCVs(space="world") <-----my_curve is seen as a Transform, so getCVs doesn't work
curveList = pm.ls("my_curve")
print curveList[0].getCVs(space="world")

For some reason, the variable my_curve isn’t seen as a NurbsCurve node (it’s seen as a Transform), but when I use the PyMel list command, it returns an indexed object that is recognized as a NurbsCurve.

What am I missing? This would clear up a lot of my thinking about PyMel scripting.

Thank you! :):

What version of Maya are you on?



import pymel.core as pmc

my_curve = pmc.curve(name="my_curve",d=1,p=[(0,0,0),(1,1,1),(2,2,2)])
print my_curve.getCVs(space="world")


This works just fine for me in 2016 ext1. Furthermore, pmc.ls(‘my_curve’)[0] yields the same object returned by the call to pmc.curve above. While both of these are the transforms and not the shape nodes getCVs continues to work, I’m guessing it’s similar to the fact that you can access cvs and verts using getattr on a transform in python and mel.

So when I print out the pm.ls(‘my_curve’) as above, it also returns a transform. However the reason that getCVs works, is that there is some trickery going on with how PyNodes work, basically if the node is a transform, it will first check to see if any method or attribute exists on it, if it doesn’t exist, it then also checks its shape node. getCVs is a method on the NurbsCurve type, which happens to be the shape node under the my_curve transform.

One thing to note though, the cmds version will only ever return a string, not a PyNode, so none of the methods will work on them at all.
Its usually best to either stick entirely with cmds, or pymel, and not mix and match them like that.

Thank you both. My error was due to using Maya’s commands and not Pymel commands. Thanks!

I didn’t catch that this was a mix of maya.cmds and pymel. It’s worth noting that you should stick with the canonical way of doing things wherever possible. In this case use “from maya import cmds” or “import maya.cmds as cmds”, this way people don’t have to make a leap when looking at your code, and you don’t have to make a leap while looking at others.