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 demonstrate, I've added planes that correspond with the different axis.
Start / Mid / End guides aimed with aim constraint In 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.
Mid joint with X and Z-axis zero-ed out To 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).
Mid guide Y-axis rotation is calculated 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.
start guide x-axis is calculated And then the (parent) start guide's X-axis rotation would be calculated, to match up the guides once again with image 1.
Desired end result 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.
The reason I ask is because I'm not very good at math myself, let alone calculating rotations in 3D space through script and not via maya.
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 example 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 [X,Y,Z] rotations would be great.
Aside from this there were a couple of other scripts I tried, but don't remember which ones and looked around on the internet for relating questions / answers that could help me, but didn't find a helpfull answer.