Ah, got it! Just a missing line. Makes sense. I think thats called a Shwartzian 
Transform? 
http://en.m.wikipedia.org/wiki/Schwartzian_transform , where you map the data 
to something with key priority, then sort, then map it back. 
Interestingly, I believe that is what the key argument of the sort function 
does under the hood, in the C side. 
But the sort function could also be used on a list of tuples. Another manual 
way to do your version would be to do:
1)  [ (depth, path), (depth, path) ]
2)  sort()
3)  [ item[1], item[1] ]

All of this depends on how expensive each option would be. 


On Aug 24, 2012, at 5:36 PM, Nicolas Combecave <[email protected]> wrote:

> Yeah,
> My first post missed an essential statement, which unfortunaetly, was the 
> core aspect of the function, the sort part of the outputed list.
> I replied with the corrected version (I set it in bold but I don't know if 
> everyone sees it in bold), and it then works as expected
> 
> 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)
> 
>     # this was the essential part of the function
>     sortedByHierarchy.sort()   
> 
>    for i in range(sortedByHierarchy.__len__()):
>         sortedByHierarchy[i] = sortedByHierarchy[i].split('@')[1]
>     
>     return sortedByHierarchy
> 
> dagList = ['|a|b|c', '|a', '|a|b', '|a']
> 
> print sortByHierarchy(dagList)
> 
> # ['|a', '|a', '|a|b', '|a|b|c']
> 
> Anyway, it's useless to say that your version is MUCH more sexy, and after 
> reading python doc, MUCH more efficient on big lists.
> 
> Nicolas
> 
> 2012/8/24 Justin Israel <[email protected]>
> Example:
> 
> dagList = ['|a|b|c', '|a', '|a|b', '|a']
> 
> print sortByHierarchy(dagList)
> # ['|a|b|c', '|a', '|a|b', '|a']
> 
> 
> On Fri, Aug 24, 2012 at 10:37 AM, Justin Israel <[email protected]> 
> wrote:
> > Haha, no don't get me wrong. I am totally NOT making a statement that
> > it is lame. I was saying that I don't see where the sort aspect is
> > occurring in your function. All i see is that you append them to the
> > list and then go back over that same list and strip away the bit you
> > put in front again. How does it sort?
> >
> >
> > On Fri, Aug 24, 2012 at 10:33 AM, Nicolas Combecave
> > <[email protected]> wrote:
> >> It does the same as yours but it's okay to say it's lame, obfuscated and 
> >> not
> >> leveraging python power ^^
> >>
> >>
> >> 2012/8/24 Justin Israel <[email protected]>
> >>>
> >>> Well ya, but in your code it wasn't sorting at all. What was it trying
> >>> to do actually?
> >>>
> >>> From a python interp you can do:
> >>> >>> help([].sort)
> >>> sort(...)
> >>>     L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
> >>>     cmp(x, y) -> -1, 0, 1
> >>>
> >>>
> >>>
> >>> On Fri, Aug 24, 2012 at 10:21 AM, Nicolas Combecave
> >>> <[email protected]> wrote:
> >>> > Actually, looking at the python doc for the list.sort() method, it
> >>> > doesn't
> >>> > mention optional arguments...
> >>> > http://docs.python.org/tutorial/datastructures.html
> >>> >
> >>> > I've had to search in here to find the info:
> >>> > http://docs.python.org/library/stdtypes.html#mutable-sequence-types
> >>> >
> >>> >
> >>> > 2012/8/24 Nicolas Combecave <[email protected]>
> >>> >>
> >>> >> Damn, your version is QUITE shorter!
> >>> >>
> >>> >> As my python is very weak, I tried to do a conversion of how I'd do it
> >>> >> in
> >>> >> mel!
> >>> >> I sure do need to deepen my python seriously...
> >>> >> Although I don't fully understand the lambda usage, I sure will look
> >>> >> into
> >>> >> it right now ^^
> >>> >>
> >>> >> So, about the (lame and lenghty) function I sent before, that was doing
> >>> >> nothing, it's because in the process of cleaning for pasting into
> >>> >> gmail, I
> >>> >> forgot the sort command...
> >>> >>
> >>> >> It should have been like this...
> >>> >>
> >>> >> 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)
> >>> >>
> >>> >>     sortedByHierarchy.sort()
> >>> >>
> >>> >>     for i in range(sortedByHierarchy.__len__()):
> >>> >>         sortedByHierarchy[i] = sortedByHierarchy[i].split('@')[1]
> >>> >>
> >>> >>     return sortedByHierarchy
> >>> >>
> >>> >>
> >>> >> The idea was to prefix each long name with it's depth info, sort that,
> >>> >> and
> >>> >> strip back this depth info to return a proper list...
> >>> >>
> >>> >> Nicolas
> >>> >>
> >>> >>
> >>> >>
> >>> >> 2012/8/24 Justin Israel <[email protected]>
> >>> >>>
> >>> >>> 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
> >>> >>
> >>> >>
> >>> >
> >>> > --
> >>> > 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
> >>
> >>
> >> --
> >> 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
> 
> -- 
> 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