List groups and geometry inside each one

List groups and geometry inside each one

abelvargas
Explorer Explorer
1,623 Views
2 Replies
Message 1 of 3

List groups and geometry inside each one

abelvargas
Explorer
Explorer

Hello,

 

I'm trying to create two separate lists.

 

1 - List of all top level groups

2 - List of all geometry inside all of them

 

My outliner looks like this:

 

+building01_grp

    building01_geo01

    building01_geo02

    building01_geo03

+building02_grp

    building02_geo01

    building02_geo02

    building02_geo03

+building03_grp

    building03_geo01

    building03_geo02

    building03_geo03

 

So ideally the lists would look like

buildingList = ["building01_grp", "building02_grp", "building03_grp"]

geometryList =["building01_geo01", "building01_geo02", "building01_geo03", ... , "building03_geo03"]

 

I'm ok with using selected nodes since there will always be a top group, like so:

 

import maya.cmds as cmds

buildingList = cmds.ls(long = True, selection = True, type = 'dagNode')

 

I can't figure out how to iterate based on the selected group names (or just straight up list all geometry) into another list.

 

How would you go about this?

 

Thank you

0 Likes
Accepted solutions (1)
1,624 Views
2 Replies
Replies (2)
Message 2 of 3

osidedan
Advocate
Advocate
Accepted solution

Hello! you probably need to use a combination of the maya command listRelatives, and Python's append, which can append items to a list. Here's an example that should work for what you need:

import maya.cmds as cmds

building_list = cmds.ls(selection=True)
geometry_list = []
for this_group in building_list:
    group_items = cmds.listRelatives(this_group)
    if group_items == None:
        pass
    else:
        for item in group_items:
            geometry_list.append(item)
print building_list
print geometry_list
    

 

That should work, and let me know if you need more info on that!

0 Likes
Message 3 of 3

abelvargas
Explorer
Explorer

Thanks a lot for the help! That makes total sense 😄