how do you select all nurbs curves in a scene?

5,040 views
Skip to first unread message

e955...@gmail.com

unread,
Mar 8, 2015, 5:53:00 PM3/8/15
to python_in...@googlegroups.com
heyyy,

i need to select all the curves present in my scene. But when i use this command i only get the shape node which isn't too useful.

curves=cmds.ls(type='nurbsCurve')

i need it to return the scene name of the curve that i have given it before.


thanks alot,
Sam

Gerard v

unread,
Mar 8, 2015, 6:45:00 PM3/8/15
to python_in...@googlegroups.com
Hi.

import maya.cmds as cmds
curve_transforms = [cmds.listRelatives(i, p=1, type='transform')[0] for i in cmds.ls(type='nurbsCurve', o=1, r=1, ni=1)]
cmds.select(curve_transforms)

List comprehension is probably not the best way to explain it, and there may be a one command way to do it instead but:
you just need an additional step: after you generate a list of nurbs curves in your scene pass that list through another command that finds the transform for each of the curves.
If you really want to do it right you could also make sure that there are no duplicate names (ie the same transform name in the resulting list (perhaps there's a transform with more than one curve shape under it) and you could also filter any resulting transform to exclude say.. joints that have curve shapes as handles..




--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/5c511357-544d-4530-bbde-c5e59b17441a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gerard v

unread,
Mar 8, 2015, 6:52:56 PM3/8/15
to python_in...@googlegroups.com
import maya.cmds as cmds
curve_transforms = [cmds.listRelatives(i, p=1, type='transform')[0] for i in cmds.ls(type='nurbsCurve', ni=1, o=1, r=1)]
curve_transforms = list(set(curve_transforms))
cmds.select(curve_transforms)

line 3 removes duplicates. Happy to get feedback from real programmers on the use of converting a list to set and back to remove duplicates...

Cesar Saez

unread,
Mar 9, 2015, 5:45:55 AM3/9/15
to python_in...@googlegroups.com
Hi there,

You don't really need to remove duplicates or get the parent in a list comprehension, maya cmds will deal with it anyways.
The following code is basically the same you suggested, but should be much faster (way less maya calls).

from maya import cmds
crvs = cmds.ls(typ='nurbsCurve', ni=True, o=True, r=True)
xfos = cmds.listRelatives(crvs, p=True, typ="transform")
cmds.select(xfos)


Cheers!


e955...@gmail.com

unread,
Mar 9, 2015, 4:04:06 PM3/9/15
to python_in...@googlegroups.com
wow, simple as that. thanks all for your help here!

Sam

Gerard v

unread,
Mar 9, 2015, 9:01:58 PM3/9/15
to python_in...@googlegroups.com
Thanks for the tip Cesar. Iterating is bad habit from my Mel days I think. 
However the removal of duplicates is probably a good idea if Sam is going to extend this code to beyond just selection. Unless I am mistaken my tests show that if there are more than one nurbsCurve shape under a transform then the transform will be listed more than once when listing relatives, which makes sense. No big deal if just selecting, but probably good to remove for other purposes perhaps?

On Tue, Mar 10, 2015 at 7:04 AM, <e955...@gmail.com> wrote:
wow, simple as that. thanks all for your help here!

Sam

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

AK Eric

unread,
Mar 9, 2015, 11:30:26 PM3/9/15
to python_in...@googlegroups.com
How about one more way? :)

import pymel.core as pm
curves = map(lambda x:x.firstParent().nodeName(), pm.ls(type='nurbsCurve'))

This returns the string names of the parent transforms.  If you want the actual PyNodes, just get rid of the nodeName() method.

Justin Israel

unread,
Mar 10, 2015, 3:36:20 AM3/10/15
to python_in...@googlegroups.com

Just one thing to note about the cmds vs the pymel approach. If you have at least a few curves, the performance difference is pretty big:

* in Maya 2015 on a Macbook Pro

# curves   cmds    pymel
  500      0.02s   0.12s
 1000      0.03s   0.25s
 5000      0.16s   1.33s
10000      0.33s   2.48s
20000      0.69s   5.32s

A lot more overhead on creating all the PyNodes in a loop. Especially if you don't need the PyNodes and just the string names. 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Marcus Ottosson

unread,
Mar 10, 2015, 6:18:36 AM3/10/15
to python_in...@googlegroups.com

You may need to remove duplicates, when there are more than one shape under a transform.

from maya import cmds

transform = cmds.createNode("transform")
shape1 = cmds.createNode("nurbsCurve", parent=transform)
shape2 = cmds.createNode("nurbsCurve", parent=transform)

shapes = cmds.ls(type="nurbsCurve")
transforms = cmds.listRelatives(shapes, parent=True)
assert len(transforms) == 2  # Should be 1

Cesar Saez

unread,
Mar 10, 2015, 6:27:21 AM3/10/15
to python_in...@googlegroups.com
Absolutely, my post was about passing the list to a select command... but yeah, you should remove duplicates if you want to use them for anything else.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Marcus Ottosson

unread,
Mar 10, 2015, 7:24:53 AM3/10/15
to python_in...@googlegroups.com
Ah, sorry about that, Cesar, I wasn't looking too closely at the original question.​
Reply all
Reply to author
Forward
0 new messages