finding the root joint with child joint selected

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
Old 05 May 2013   #1
finding the root joint with child joint selected

tempSel = =True) # Result: [u'joint9'] #

dupParentJntLr = cmds.listRelatives(ap = True, f = True )

# Result: [u'|joint1|joint2|joint3|joint4|joint5|joint6|joint 7|joint8'] #


dupeMainJnt = dupParentJntLr[0]

# Result: |joint1|joint2|joint3|joint4|joint5|joint6|joint7| joint8 #<<== why it is giving the same list again rather than giving 'joint1'?
Old 05 May 2013   #2
Because you are storing the list as a string in dupParentJntLr[0]

You need a loop for the effect you are looking for.
Old 05 May 2013   #3
urgh... cmds.listRelatives returns ugliness to be sure.

If you want the root joint from a single selection try this

import pymel.core as pm
root = pm.selected()[0].root()

Old 05 May 2013   #4
david, please tell me how to do it in python. as i cant convert entire code to pyMel.
thank you!
Old 05 May 2013   #5
In your OP you just went up one level to joint8, and you only see its full name. You need to continue going up.
Old 05 May 2013   #6
i can see mel and pyMel examples in that link that you provided.but how is it possible with python. and why python storing list as a single string? is this a feature or a bug?
Old 05 May 2013   #7
cmds.listRelatives() returns a list.
Your selected joint has just one parent, so the list contains just one element.
Most maya.cmds function return strings or list of strings.
You used the fullPath keyword, so your string is the full path name to your selected joint's parent, which in this case happens to be all the other joints, upto the top of the heirarchy, which maya or may not be the root joint, because the root joint may actually be parented to something else.
In maya a path seperator is the | character.
You can use the path seperator to split the string into a list.
You can iterate through the list to find the first joint, and this would be your root joint.

selectedJointName =, type='joint')
parentList = cmds.listRelatives(selectedJointName, ap=True, f=True)[0].split('|') if selectedJointName else []
jointList = [p for p in parentList if p and cmds.nodeType(p) == 'joint']
rootJoint = jointList[0] if jointList else None
print rootJoint


Last edited by djx : 05 May 2013 at 01:31 PM.
Old 05 May 2013   #8
Just a side note/question...I have never needed to convert a whole code to pymel if I wanted to use a few pymel commands. Since I don't know all the amazing aspects of pymel, I still rely on python commands for a good chunk of my python scripts, but I haven't had any problem using both maya.cmds and pymel.core. I think as long as you import them with an alias you are okay using both.
I may be totally wrong here...but so many great pymel commands (like root!) are just too sweet to pass up if you can't write the whole script in pymel.
Just saying.
Old 05 May 2013   #9
Originally Posted by theJackson: i can see mel and pyMel examples in that link that you provided.but how is it possible with python. and why python storing list as a single string? is this a feature or a bug?

There's nothing specific to pymel in the pymel example. Just use cmds instead of pm. And concerning @animatedfox comment, what do you have against pymel anyway? (and please don't tell me that it's a 'different' language that you don't have time to learn.)
Old 05 May 2013   #10
Originally Posted by zoharl: There's nothing specific to pymel in the pymel example. Just use cmds instead of pm
While most (not all) of the maya.cmds functions have a pymel.core equivalent, the opposite is not always true. I believe my first example was actually fairly pymel specific. In this example literally replacing pm with cmds would result in error. There is no cmds.selected() and since .root() is an object method it would not work on the return from a cmds function. My second example was an attempt to achieve the same result using straight cmds (although I also added some sanity checks).

In some ways I think I can see what you are getting at zoharl, but I always try to encourage people to look deeper and see pymel as being different to cmds. The pymel dev's claim is it "Python in maya done right". I tend to agree. Its not just a replacement for cmds, its a different way of doing it.

Old 05 May 2013   #11
David, you gave me this lecture before, and I have no arguments. I even refrained from suggesting to import cmds as pm. The guy said that my link didn't contain a python example (i.e. I'm stupid), and I just remarked that he can use the second example on the link without pymel as well (or translate the first mel code line-by-line to python).
I didn't even express my opinion that while I liked the tricks you used, a newbie should also understand the conventional process of navigating a DAG.
Old 05 May 2013   #12
I'm sorry if you thought that was all directed at you. Definitely no lecture intended. However often I see that the differences and similarities between cmds and pymel are difficult to understand for people new to scripting in maya. I usually try to give only pymel examples in my answers and let others provide the cmds comparison (since I hardly use it, I figure others could give better cmds examples than me - case in point... I had not seen the rootOf() function before following the link you provided). But in this case I felt it would be helpful to respond to the op's request for a cmds equivalent.


Last edited by djx : 05 May 2013 at 02:37 PM.
Old 05 May 2013   #13
Ha, ha, zoharl!
You always love to defend your scripting languages.
I am sorry if it sounded as though I was dissing pymel...My intention was actually to encourage the original poster to use pymel commands without worrying about converting his whole code into pymel. I actually love pymel though I still don't know all the ins and outs of using it exclusively as it isn't just a 1 to 1 swap from using maya.cmds.
Anyway, just wanted to clarify.
Old 05 May 2013   #14
I get the feeling that this whole thread is a big confusion, at least on my part where I misunderstand or misunderstand.

@fox, I think we hold the same perspective (which I think is different than David, although what he says makes sense to me, and if I was more than a casual scripter, I might have taken is words more seriously). When I asked what have you got against pymel it was directed towards the OP (isn't there an implication when I quote someone?).

I think I explained my opinion on a previous thread. I use the best tool for me to get the job done the easiest (something is redundant here using these two superlatives in the same sentence). I hardly know pymel, and it's really don't make any difference to me if I use pm.getAttr, cmds.getAttr, or obj.getSomeProperty. When I use OOP (C++), it's usually for a much productive reason (usually a necessity) than to impress my friends that I write OOP python. So currently, since maya python is for me just a bunch of procedures that do something, and it's easier for me to navigate autodesk docs, or translate mel from the script editor or examples, I didn't make the effort to learn pymel properly. I mean what's the use if it would take me longer to locate commands; why am I using python anyway?

But as you (@fox the previous poster) said, I really like these sweet shortcuts if I find them, and I blend pymel and cmds with no compunctions, i.e. I'm not investing more time to find how to do it in pymel, only for the sake of having the whole script written in pymel. So that's why I was curious why the OP requested no pymel. I mean, you have one simple command that gets you what you want, what's the problem?
Old 05 May 2013   #15
Originally Posted by zoharl: And concerning @animatedfox comment, what do you have against pymel anyway?

Glad to get the clarification...this is what I found confusing. It sure read as though you were asking me what I had against pymel. Glad to know we are still friends!
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 04:55 AM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.