Check between 2 lists

22 views
Skip to first unread message

yann19

unread,
Sep 8, 2016, 6:43:40 PM9/8/16
to Python Programming for Autodesk Maya

I am doing a dry-run test of my code and bump into an issue...
Please see my screenshot for my very simple hierarchy as I am trying to do renaming.



This is my code:
import maya.cmds as cmds
all_grp
= []
for child_grp in cmds.listRelatives("group", path=True):
   
print child_grp
    all_grp
.append(child_grp)


all_loc
= cmds.listRelatives(cmds.ls(type='locator'), parent = True)


for grp, loc in zip(all_grp, all_loc):
   
print "grp - {0}, loc - {1}".format(grp, loc)


What I am expecting to see:
# Correct
grp
- sub_group1, loc - locator1
grp
- sub_group3, loc - locator3
grp
- sub_group2, loc - locator2


But my dry-run result says otherwise:
# Wrong
grp
- sub_group1, loc - locator1
grp - sub_group3, loc - locator2
grp
- sub_group2, loc - locator3


Notice that in both sub_group3 and sub_group2, I am expecting the loc to be grabbing locator3 instead of locator2 (for sub_group3)
How do I iterate such that 'loc' will checks if it belongs within the hierarchy of 'grp'?

I am trying to achieve something like this:
If loc belongs in this grp, then do this processA... 
if loc does not belong in that grp, loop thru the grp list to find its 'parent/grp' then do processA..

Justin Israel

unread,
Sep 8, 2016, 7:00:51 PM9/8/16
to python_in...@googlegroups.com
You are seeing this output because there is no correlation between your "all_grp" and "all_loc" lists. But you zip them together as if they are related. One is a list of groups under the named group. The other is a list of all the parents of all the locators in your seen. There is no correlation in the order of these two.

Maybe what you want is to search the hierarchy of each subgroup?

import maya.cmds as cmds

all_grp = cmds.listRelatives("group", path=True)

# Map from group to its locator decendants
mapping = {}

for grp in all_grp:
    shapes = cmds.listRelatives(grp, ad=True, type="locator")
    locs = cmds.listRelatives(shapes, parent=True)
    mapping[grp] = locs

for grp, locs in mapping.iteritems():
    print "grp - {0}, locs - {1}".format(grp, locs)

Justin

--
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/0aa2d366-f162-4962-a509-ab7f787f9ac4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages