Selects only the group nodes

94 views
Skip to first unread message

likage

unread,
Aug 29, 2016, 8:56:16 PM8/29/16
to Python Programming for Autodesk Maya
I am trying to grab only the group nodes within the hierarchy as shown in the screenshot..

While I use this code:

sel = cmds.listRelatives(cmds.ls(transforms=True), ap=True, ni=True)
print sel

It is giving me the following results:
[u'Group_1', u'Group_2', u'Group_2', u'Group_2', u'Group_2', u'Group_2', u'Group_3', u'Group_4', u'Group_5']

Though it gives me duplicated result of certain groups, is this the best way to approach this?
I am using Group_ as a generic naming and hence my reluctance to use Group_*, in case you are wondering...



Kurian O.S

unread,
Aug 29, 2016, 9:01:00 PM8/29/16
to python_in...@googlegroups.com
You can use set to filter duplicate's ( although its not duplicate its because you are using all parents = True)

sel = list(set(cmds.listRelatives(cmds.ls(transforms=True), ap=True, ni=True)))
print sel

This should give back you what you expecting 

--
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+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a70ebb57-f68c-493f-b84a-ba5a52334697%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--:: Kurian ::--

Justin Israel

unread,
Aug 29, 2016, 9:26:12 PM8/29/16
to python_in...@googlegroups.com
On Tue, Aug 30, 2016 at 12:56 PM likage <dissid...@gmail.com> wrote:
I am trying to grab only the group nodes within the hierarchy as shown in the screenshot..

Is a group node considered any transform that has a child? That is, would you expect to get pSphere1 or locator1 back in your results if they had child transforms under them?
 

While I use this code:

sel = cmds.listRelatives(cmds.ls(transforms=True), ap=True, ni=True)
print sel

It is giving me the following results:
[u'Group_1', u'Group_2', u'Group_2', u'Group_2', u'Group_2', u'Group_2', u'Group_3', u'Group_4', u'Group_5']

Though it gives me duplicated result of certain groups, is this the best way to approach this?
I am using Group_ as a generic naming and hence my reluctance to use Group_*, in case you are wondering...



--
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.

Geordie Martinez

unread,
Aug 30, 2016, 1:06:15 AM8/30/16
to python_inside_maya
here's a way to do it without getting duplicates.

```python
import maya.cmds as mc

sel = list(set([x for x in mc.listRelatives(mc.ls(assemblies=True), ad=True, type='transform') if not mc.listRelatives(x,type="shape")]))
print sel
```

try this see if it works. 

On Mon, Aug 29, 2016 at 6:25 PM, Justin Israel <justin...@gmail.com> wrote:
On Tue, Aug 30, 2016 at 12:56 PM likage <dissid...@gmail.com> wrote:
I am trying to grab only the group nodes within the hierarchy as shown in the screenshot..

Is a group node considered any transform that has a child? That is, would you expect to get pSphere1 or locator1 back in your results if they had child transforms under them?

While I use this code:

sel = cmds.listRelatives(cmds.ls(transforms=True), ap=True, ni=True)
print sel

It is giving me the following results:
[u'Group_1', u'Group_2', u'Group_2', u'Group_2', u'Group_2', u'Group_2', u'Group_3', u'Group_4', u'Group_5']

Though it gives me duplicated result of certain groups, is this the best way to approach this?
I am using Group_ as a generic naming and hence my reluctance to use Group_*, in case you are wondering...



--
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+unsub...@googlegroups.com.

--
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+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA16FKRem4SLDXvruX-ZOTrY0fQ_Oqt2P%2BZTFozDmUPMAA%40mail.gmail.com.

Geordie Martinez

unread,
Aug 30, 2016, 1:07:01 AM8/30/16
to python_inside_maya

whoops. forgot markdown.

import maya.cmds as mc

sel = list(set([x for x in mc.listRelatives(mc.ls(assemblies=True), ad=True, type='transform') if not mc.listRelatives(x,type="shape")]))
print sel
On Mon, Aug 29, 2016 at 10:06 PM, Geordie Martinez <geordie...@gmail.com> wrote:
here's a way to do it without getting duplicates.

```python
import maya.cmds as mc

sel = list(set([x for x in mc.listRelatives(mc.ls(assemblies=True), ad=True, type='transform') if not mc.listRelatives(x,type="shape")]))
print sel
```

try this see if it works. 
On Mon, Aug 29, 2016 at 6:25 PM, Justin Israel <justin...@gmail.com> wrote:
On Tue, Aug 30, 2016 at 12:56 PM likage <dissid...@gmail.com> wrote:
I am trying to grab only the group nodes within the hierarchy as shown in the screenshot..

Is a group node considered any transform that has a child? That is, would you expect to get pSphere1 or locator1 back in your results if they had child transforms under them?

While I use this code:

sel = cmds.listRelatives(cmds.ls(transforms=True), ap=True, ni=True)
print sel

It is giving me the following results:
[u'Group_1', u'Group_2', u'Group_2', u'Group_2', u'Group_2', u'Group_2', u'Group_3', u'Group_4', u'Group_5']

Though it gives me duplicated result of certain groups, is this the best way to approach this?
I am using Group_ as a generic naming and hence my reluctance to use Group_*, in case you are wondering...



--
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+unsubscribe@googlegroups.com.

--
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+unsubscribe@googlegroups.com.

likage

unread,
Aug 30, 2016, 6:16:28 PM8/30/16
to Python Programming for Autodesk Maya
@justin
I am only expecting groups, and hence I am not expecting any pSphere1 or locator1 in this case.

@Kurian
That works great! Just wanted to ask is using list and set necessary?
I tried removing them and they worked too

Justin Israel

unread,
Aug 30, 2016, 6:23:36 PM8/30/16
to python_in...@googlegroups.com
On Wed, Aug 31, 2016 at 10:16 AM likage <dissid...@gmail.com> wrote:
@justin
I am only expecting groups, and hence I am not expecting any pSphere1 or locator1 in this case.

Right. So your original solution would not work correctly in this case, because it would return transforms for other objects as well
 

@Kurian
That works great! Just wanted to ask is using list and set necessary?
I tried removing them and they worked too

While there would not be duplicate items in this approach, there could be duplicate names at different parts of the hierarchy (depending on whether you are asking for full paths or not)
 

--
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,
Aug 30, 2016, 6:24:08 PM8/30/16
to python_in...@googlegroups.com

I am only expecting groups

I think the reason Justin was asking this, was because in Maya, there is no concept of “group”, only “transforms without shape”. You can prove this to yourself, by deleting the shape from any transform, such as a sphere or locator and witness it become what you refer to as “group”.


--
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+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e7323b1f-84f1-40cc-859b-25b4d5737ff5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Aug 30, 2016, 6:27:59 PM8/30/16
to python_in...@googlegroups.com
On Wed, Aug 31, 2016 at 10:24 AM Marcus Ottosson <konstr...@gmail.com> wrote:

I am only expecting groups

I think the reason Justin was asking this, was because in Maya, there is no concept of “group”, only “transforms without shape”. You can prove this to yourself, by deleting the shape from any transform, such as a sphere or locator and witness it become what you refer to as “group”.


Yep, that was my intent. Thanks Marcus!
 

On 30 August 2016 at 23:16, likage <dissid...@gmail.com> wrote:
@justin
I am only expecting groups, and hence I am not expecting any pSphere1 or locator1 in this case.

@Kurian
That works great! Just wanted to ask is using list and set necessary?
I tried removing them and they worked too

--
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
konstr...@gmail.com

--
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/CAFRtmOA%2B2q%2B-k%3D_Ta%2B9JvW_z6kX3VfzZX87z07m1a2_kdoQihw%40mail.gmail.com.

likage

unread,
Aug 31, 2016, 3:39:39 PM8/31/16
to Python Programming for Autodesk Maya
I have another related question...
Suppose in my above hierarchy, this time round, I have a pCube1 which is a parent of pCube2 and it is in the same 'level' as Group_1..

While running the code, it selects all the group nodes but pCube1 too though it should not be...
I did not think that far ahead... Then I guess it may be impossible?

Justin Israel

unread,
Aug 31, 2016, 4:13:12 PM8/31/16
to Python Programming for Autodesk Maya
This is the point that myself and Marcus were making. Maya has no 1st Clas concept of a group type. It is a display concept depending on its children. So technically pCube1 can be a group. You may need to extend your test. 

--
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.

likage

unread,
Aug 31, 2016, 5:13:51 PM8/31/16
to Python Programming for Autodesk Maya
@Justin
I see, now I do get what you mean.. I apologize that I have missed out on your point..

I was using that screenshot as a small testing ground of what I was doing, too generic as I did not forecast what you meant... Now that sort of sucks..

likage

unread,
Aug 31, 2016, 5:26:02 PM8/31/16
to Python Programming for Autodesk Maya
@Geordie
Forgotten to mention but it does not seems to be working, fyi

likage

unread,
Aug 31, 2016, 6:29:36 PM8/31/16
to Python Programming for Autodesk Maya
Also, why is it that sometimes I got this error:
# Error: 'list' object is not callable
# Traceback (most recent call last):
#   File "<maya console>", line 14, in <module>
#   File "<maya console>", line 3, in test
# TypeError: 'list' object is not callable #

that is stemming from the list(set(...))?

There are items within my scene and the error does not exists only after I remove the list..

Justin Israel

unread,
Aug 31, 2016, 6:32:32 PM8/31/16
to python_in...@googlegroups.com
Seems like a typo in your code. It implies that you are doing something like:

aList()
 

--
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/f6fbfc4a-6c40-482e-ba48-58014bb9ba38%40googlegroups.com.

likage

unread,
Aug 31, 2016, 8:29:26 PM8/31/16
to Python Programming for Autodesk Maya
I will keep a lookout again. Perhaps it is a mistake on my part..

But in case if anyone chance upon this thread, I managed to get groups' selection by using 2 sets..
transform_set = set(cmds.listRelatives(cmds.ls(transforms=True), ap=True, ni=True))
mesh_set
= set(cmds.listRelatives(cmds.ls(typ='mesh'), parent=True))
group_selection
= list(trans_set.difference(mesh_set))

Not in the most elegant way of doing, but since my scene seems to be dealing mainly with meshes and groups, I think it work in my cause.

Reply all
Reply to author
Forward
0 new messages