Skip to content

Instantly share code, notes, and snippets.

@Meatplowz
Last active November 29, 2019 08:08
Show Gist options
  • Save Meatplowz/856befbbb9dfdb36a7c327e092cd60b5 to your computer and use it in GitHub Desktop.
Save Meatplowz/856befbbb9dfdb36a7c327e092cd60b5 to your computer and use it in GitHub Desktop.
Brute force method to adjust the poleVector of one ik chain to line up the elbow joint of a similar arm chain
'''
Brute force method to adjust the poleVector of one ik chain
to line up the elbow joint of a similar arm chain
'''
import pymel.core as pymel
import maya.OpenMaya as OpenMaya
def adjust_pv_to_elbow(poleVector, ik_elbow, elbow):
"""
Move the poleVector of an ik chain until the elbow joint of that
ik chain, lines up in worldspace to the target elbow worldspace position
*Arguments:*
* ``poleVector``
* ``ik_elbow`` elbow joint driven by the poleVector
* ``elbow`` elbow joint we are trying to get the ik_elbow positioned at
*Keyword Arguments:*
* ``None``
*Returns:*
* ``None``
*Author:*
* randall.hess
"""
pymel.select(poleVector, r=True)
# this is the base elbow joint target
elbow_pos = pymel.xform(elbow, ws=1, q=1, t=1)
elbow_vec = OpenMaya.MVector(elbow_pos[0], elbow_pos[1], elbow_pos[2])
# if we get this close we are probably good
tolerance = 0.00001
# iterate the poleVector moving to decrease the distance between the two elbow joints
for index in range(0, 100):
# this is the elbow joint that will move when the pv is adjusted
ik_elbow_pos = pymel.xform(ik_elbow, ws=1, q=1, t=1)
ik_elbow_vec = OpenMaya.MVector(ik_elbow_pos[0], ik_elbow_pos[1], ik_elbow_pos[2])
length = OpenMaya.MVector(ik_elbow_vec - elbow_vec).length()
# go through each component and subtract
diff_x = ik_elbow_pos[0] - elbow_pos[0]
diff_y = ik_elbow_pos[1] - elbow_pos[1]
diff_z = ik_elbow_pos[2] - elbow_pos[2]
# move the poleVector
if length == 0:
break
elif length < tolerance:
print 'length is under the tolerance!'
break
if length > 0:
pymel.move(-diff_x, -diff_y, -diff_z, ws=True, wd=True, r=True)
else:
pymel.move(diff_x, diff_y, diff_z, ws=True, wd=True,r=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment