Discussion:
[Maya-Python] how do you select all nurbs curves in a scene?
e***@gmail.com
2015-03-08 21:52:58 UTC
Permalink
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
--
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_maya+***@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
2015-03-08 22:44:57 UTC
Permalink
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..
Post by e***@gmail.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
--
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
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.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CADKA4CqTDOq_F9ExKV%2B%2Bd5aEngo7CN7T%2BeG5ccxAF2mBbvfm1w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Gerard v
2015-03-08 22:52:51 UTC
Permalink
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...
Post by Gerard v
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
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..
Post by e***@gmail.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
--
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
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.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CADKA4CrUfp0HedNetTQ-jrZoyOps5Tdqf3FfOg%3DdU6RZZoAVUA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Cesar Saez
2015-03-09 09:45:17 UTC
Permalink
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!
Post by Gerard v
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...
Post by Gerard v
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
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..
Post by e***@gmail.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
--
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
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.
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CADKA4CrUfp0HedNetTQ-jrZoyOps5Tdqf3FfOg%3DdU6RZZoAVUA%40mail.gmail.com
<https://groups.google.com/d/msgid/python_inside_maya/CADKA4CrUfp0HedNetTQ-jrZoyOps5Tdqf3FfOg%3DdU6RZZoAVUA%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPamJi-YJ%3D2Lm53Zbsu4Cc48YpBazz3-mOviV9pN0UXZ96p8Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
e***@gmail.com
2015-03-09 20:04:03 UTC
Permalink
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a3bf69f4-2a24-443f-a548-f3fc19405eda%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Gerard v
2015-03-10 01:01:56 UTC
Permalink
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?
Post by e***@gmail.com
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/a3bf69f4-2a24-443f-a548-f3fc19405eda%40googlegroups.com
.
For more options, visit https://groups.google.com/d/optout.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CADKA4Cq7Oj0jjVEDHpwfCToGP_jYmHf%2BWx_NRVXLqF3vv3QN%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
AK Eric
2015-03-10 03:30:26 UTC
Permalink
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.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a218df4d-4ae5-4937-9a18-bd84596fd8c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2015-03-10 07:35:55 UTC
Permalink
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.


On Tue, Mar 10, 2015 at 4:30 PM AK Eric ***@sbcglobal.net
<http://mailto:***@sbcglobal.net> wrote:

How about one more way? :)
Post by AK Eric
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.
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/a218df4d-4ae5-4937-9a18-bd84596fd8c0%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/a218df4d-4ae5-4937-9a18-bd84596fd8c0%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
​
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3NtyqC1_FiFqDEwD1GYqkEokjKZ_ZkDV72ntUrhsoKVQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Marcus Ottosson
2015-03-10 10:18:33 UTC
Permalink
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

​
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBADKuRRWxYxNrmb4xYsEzqMbSbbSGGzW57PG0oW0v9WQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Cesar Saez
2015-03-10 10:26:44 UTC
Permalink
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.
Post by Marcus Ottosson
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
​
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBADKuRRWxYxNrmb4xYsEzqMbSbbSGGzW57PG0oW0v9WQ%40mail.gmail.com
<https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBADKuRRWxYxNrmb4xYsEzqMbSbbSGGzW57PG0oW0v9WQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPamJi8wtgXqkM2MnoPTnJdUUG5FmA%3DinONf%2B0Bi0oiMbKoe3Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Marcus Ottosson
2015-03-10 11:24:51 UTC
Permalink
Ah, sorry about that, Cesar, I wasn't looking too closely at the original
question.​
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOB_nwez5uJPi%3DXiT6gYnenm4cS3GC7bRAkHHnWNpOWstQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...