Thanks Andres! That worked great. Now, regardless of the primary axis, it
returns the correct length.
By the way, I'm not too familiar with pymel (yet). Is there an equivalent
to the code in Python proper? Specifically the getTranslation & getChildren
commands?
I've tried this, but I get errors:
from math import *
def dist (objA, objB):
Ax,Ay,Az=mc.xform(objA,q=1,t=1)
Bx,By,Bz=mc.xform(objB,q=1,t=1)
return sqrt(pow((Ax-Bx),2)+pow((Ay-By),2)+pow((Az-Bz),2))
def lenJointChain(obj,len=0):
print obj
if mc.listRelatives(obj,children=1)==[]:
return len
else:
child=mc.listRelatives(obj,children=1,type='transform')[0]
return lenJointChain (child, len=len+dist(obj,child))
lenJointChain(mc.ls(sl=True)[0])
Thanks.
On Wednesday, May 28, 2014 5:15:12 PM UTC-4, Andres Weber wrote:
>
> I'm not really sure why you're doing it the way you're doing it...
> This is how I solved it quickly (There's better ways but I'm lazy and I
> didn't want to keep working on it.) Just a simple recursive function.
>
> def dist( objA, objB ):
>
> Ax, Ay, Az = objA.getTranslation(space="world")
>
> Bx, By, Bz = objB.getTranslation(space="world")
>
> return ( (Ax-Bx)**2 + (Ay-By)**2 + (Az-Bz)**2 )**0.5
>
> def lenJointChain(obj,len=0):
>
> if obj.getChildren()==[]:
>
> return len
>
> else:
>
> child=obj.getChildren(type='transform')[0]
>
> return lenJointChain(child, len=len+dist(obj,child))
>
> lenJointChain(pm.ls(sl=True)[0])
>
>
>
> On Wednesday, May 28, 2014 3:18:13 PM UTC-4, flaye wrote:
>>
>>
>>
>> # Find the full length of the chain
>> sel=[]
>> sel=mc.ls(sl=True)
>> length = 0
>> for i in range(1, len(sel)):
>> length += abs(mc.getAttr(sel[i]+'.tx'))
>>
>> print length
>>
>>
>> The above code will provide the correct answer, on the condition that the
>> primary axis of the joints (in this case, X) is known.
>>
>> I've also tried using the MVector function, but unfortunately the numbers
>> are off
>>
>> import maya.api.OpenMaya as om #for api 2.0
>> sel=[]
>> sel=mc.ls(sl=True)
>>
>> startjnt=sel[0]
>> numjnts=len(sel)
>> length=0
>> for i in range(0,numjnts-1):
>>
>> currentjnt=sel[i]
>>
>> nextjnt=mc.listRelatives(currentjnt,children=1)[0]
>>
>> print currentjnt,nextjnt
>>
>> t1=mc.xform(currentjnt,q=1,t=1)
>> t2=mc.xform(nextjnt,q=1,t=1)
>>
>> v1=om.MVector(t1)
>> v2=om.MVector(t2)
>>
>> dist=om.MVector(v2-v1).length() #equivalent to MEL's mag?
>> length+=dist
>> print length
>>
>>
>> any ideas? Thanks.
>>
>>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/e27036af-4d88-4a91-bd0c-3fb492dfeb9e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.