Maya | Python: How to get parent and child distributed rotation after zero-ing out 2 child rotation axis?

Maya | Python: How to get parent and child distributed rotation after zero-ing out 2 child rotation axis?

Radiant-Sky
Observer Observer
783 Views
0 Replies
Message 1 of 1

Maya | Python: How to get parent and child distributed rotation after zero-ing out 2 child rotation axis?

Radiant-Sky
Observer
Observer
Hello,

I'm trying to write a script that automatically 'aims' a set of 3 guides / controls (that were placed in the scene at random) towards each other [start to mid, mid to end]. These guides are also parented under each other (as you would a normal arm / leg) [Start -> mid -> End]. So far I've been using maya's built in aim constraint, which works just fine. However, to get the best result with that, the aim constraint aims the middle guide on all 3 rotation axis to the end guide, which for rigs (that use IK chains) isn't always desired. So my question is how to find the same aim rotation, but have this distribute between the parent (start guide) and the child (mid guide), so that the child is only rotated around the Y-axis?
To clarify, the axis that I've mentioned are just an example. So any combination of axis would work. The only thing that needs to stay is that the child is only rotated around 1 axis.

 

To demonstrate, I've added planes that correspond with the different axis.

Image 1Image 1In this image the start / mid / end guide are aimed towards each other with the aim constraint from Maya. Which I have scripted. Aka, the start guide and mid guide have rotations values on all axis.

Image 2Image 2To build further on my existings script, I was planning on zero-ing out the X and Z-axis rotation for the mid guide, and from this altered version calculate the difference from the first image. Which might be a redundent step if the altered rotation can be gotten from the aimed version (image 1).

marietyrions_0-1693556074339.png

After this the (child) mid guide's Y-axis rotation would be calculated, to rotate the end guide to allign with the end guide from image 1.

marietyrions_1-1693556100063.png

And then the (parent) start guide's X-axis rotation would be calculated, to match up the guides once again with image 1.

marietyrions_2-1693556140555.png

Essentially ending up with guides that are position on the exact same place, but are rotated much cleaner than Maya's built in aim constraint.

Hope that makes a bit of sense. It's possible that the answer is very easy and I'm just making it hard on myself, but just wondering if someone has any ideas or insights that I'm overlooking.

What I've tried: I used the a script on the following thread given by 'Dominic' [https://stackoverflow.com/a/42390363/22477916] (although translated to python for use in maya).

import math 

def normaliseToInteriorAngle(angle):
    if (angle < 0):
        angle += (2*math.pi)
        
    if (angle > math.pi):
        angle = 2*math.pi - angle
    
    return angle

def angle(startJnt, midJnt, endJnt):
    p1 = cmds.xform(startJnt, q=True, t=True, ws=True)
    center = cmds.xform(midJnt, q=True, t=True, ws=True)
    p2 = cmds.xform(endJnt, q=True, t=True, ws=True)
    
    P1 = [(p1[0] - center[0]), (p1[1] - center[1])]
    P2 = [(p2[0] - center[0]), (p2[1] - center[1])]

    angleToP1XY = math.atan2(P1[1], P1[0])
    angleToP2XY = math.atan2(P2[1], P2[0])

    return normaliseToInteriorAngle(angleToP2XY - angleToP1XY)

def toDegrees(radians):
    deg = 360 * radians / (2 * math.pi)
    
    return deg
    
angleA_XY = angle('GuideA1', 'GuideA2', 'GuideA3')
angleDegA_XY = toDegrees(angleA_XY)

angleB_XY = angle('GuideB1', 'GuideB2', 'GuideB3')
angleDegB_XY = toDegrees(angleB_XY)

angleAB = angleDegA_XY - angleDegB_XY

Where the idea behind using it would be this:
Tryout ideaTryout idea

 

Where 1 refers to the position of the guides when aimed with maya's aim constraint (image 1 in explanation). 2 refers to the same guides but where the X and Z rotation axis on the mid guide are zero-d out (image 2 in explanation). The positions of A1, A2 and A3 were used to calculate angle A. Positions B1, B2 and B3 were used to calculate angle B. After which B was substracted from A to become the desired angle C. This works in a 2D environment (aka, if the guides were hardly rotated), but not in a 3D environment.

So something like this that works with 3D rotations / translations.

 

0 Likes
Replies (0)