Maybe I am missing something, but it doesn't seem like the sortByHierarchy()
function is doing anything.
It takes in a list, then loops over it, creating a new list with the depth
count + @ + name. Then it just loops back over the new list, and replaces
it with the name again. But no sorting is happening at all.

If that function is supposed to simply sort by the depth value, couldn't
you just do this?

    dagList.sort(key=lambda x: x.count('|'))
    # dagList.sort(key=lambda x: x.count('|'), reverse=True)

What is the logic of concatenating the depth + @ + name?


On Fri, Aug 24, 2012 at 5:54 AM, Nicolas Combecave <[email protected]
> wrote:

> I've tested this and it seems to work as you need:
>
> import maya.cmds as cmds
> import random
>
> # CREATING RANDOM LOCATORS AND JOINTS TO TEST SNAPPING HIERARCHIES
> max = 10
> prevLoc = None
> prevJoint = None
>
> for i in range(max):
>
>     loc = cmds.spaceLocator(name = "Gizmo_"+str(i))
>     cmds.xform(worldSpace = True, t = [random.random()*10,
> random.random()*10, random.random()*10])
>     cmds.select(cl = True)
>     joint = cmds.joint(name = "joint_"+str(i), position =
> [random.random()*10, random.random()*10, random.random()*10])
>
>     if prevLoc != None:
>         cmds.parent (loc[0], prevLoc[0])
>     if prevJoint != None:
>         cmds.parent (joint, prevJoint)
>
>     prevLoc = loc
>     prevJoint = joint
>
> # HERE YOU CAN REPARENT SOME LOCATORS AND JOINTS IN THE SAME WAY IN ORDER
> TO TEST HIERARCHIES WITH BRANCHES
>
> # NOW SNAPPING JOINTS HIEARCHY TO LOCATORS HIERARCHY
> # WE ASSUME YOU DON'T HAVE DUPLICATE SHORT NAMES IN JOINTS HIERARCHY
> liste = cmds.ls('Gizmo_*', type = "transform", dag = True, l = True)
>
> for gizmo in liste:
>     gizmo_id = gizmo.split('|')[-1].split('_')[-1]
>     joint_id = "joint_" + gizmo_id
>     gizmoPos = cmds.xform(gizmo, q = True, worldSpace = True, t = True)
>     cmds.xform(joint_id, worldSpace= True, absolute = True, t =
> (gizmoPos[0],gizmoPos[1],gizmoPos[2]))
>
>
>
> Anyway, it's always a good thing to have a little utility function to
> order list by hierarchy
> Here is one quickly hacked:
>
> def sortByHierarchy(dagList):
>     # we'll use '@' as it can't never be found in maya node names
>
>     sortedByHierarchy = []
>
>     for elem in dagList:
>         depth = elem.count('|')
>         sortedByHierarchy.append (str(depth) + "@" + elem)
>     for i in range(sortedByHierarchy.__len__()):
>         sortedByHierarchy[i] = sortedByHierarchy[i].split('@')[1]
>
>     return sortedByHierarchy
>
> print sortByHierarchy(liste)
>
> Nicolas
>
> --
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings:
> http://groups.google.com/group/python_inside_maya/subscribe
>

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to