def set_poleLocator(**kwargs): """ Select 3 joints parent to child in order to which pole vector has to be placed Select 3 joints and ik handle in order, to create the pole vector object and constraint """ distance_scale = kwargs.get("distance_scale", 1) pole_vector_locator = kwargs.get("pole_vector", "poleLocator") selected_joint = pm.ls(selection=True) if len(selected_joint) < 3: print("please select 3 joints") return None joint1_position = pm.xform(selected_joint[0], query=True, worldSpace=True, translation=True) joint2_position = pm.xform(selected_joint[1], query=True, worldSpace=True, translation=True) joint3_position = pm.xform(selected_joint[2], query=True, worldSpace=True, translation=True) joint1_vector = openmaya.MVector(joint1_position[0], joint1_position[1], joint1_position[2]) joint2_vector = openmaya.MVector(joint2_position[0], joint2_position[1], joint2_position[2]) joint3_vector = openmaya.MVector(joint3_position[0], joint3_position[1], joint3_position[2]) joint1_joint2_vector = joint2_vector - joint1_vector joint1_joint2_vector.normalize() joint2_joint3_vector = joint3_vector - joint2_vector joint2_joint3_vector.normalize() plane_normal = joint1_joint2_vector ^ joint2_joint3_vector plane_normal.normalize() joint1_joint3_vector = joint3_vector - joint1_vector joint1_joint3_vector.normalize pole_vector = joint1_joint3_vector ^ plane_normal mag_vec = pole_vector * distance_scale pole_position = joint2_vector + mag_vec loc_pos = [pole_position.x, pole_position.y, pole_position.z] pole_locator = pm.spaceLocator(name=pole_vector_locator) locator_group = pm.group(pole_locator, name=pole_vector_locator + "_group") pm.xform(locator_group, translation=loc_pos) print("Pole object placed at ", str(loc_pos)) if len(selected_joint) == 4: ik_handle = selected_joint[3] pm.poleVectorConstraint(pole_locator, ik_handle) print("Pole Vector Constraint applied") return None
def makePoleVector(self, constrain=True): iks = pm.ls(selection = True ) originalSelection = iks locators = [] for ik in iks: loc = pm.spaceLocator() pm.rename( loc, 'poleVectorTarget' ) pm.parent( loc, ik ) ik.poleVectorX >> loc.translateX ik.poleVectorY >> loc.translateY ik.poleVectorZ >> loc.translateZ loc.translateX.disconnect() loc.translateY.disconnect() loc.translateZ.disconnect() pm.parent( loc, world=True ) if constrain==True: pm.poleVectorConstraint( loc, ik, weight=1 )
## at the one exact "magic" ## place where the pole vector ## is for the ik handle ## it's magic, because it's the only ## place that won't "pop" out of the pose ## ## connect the ik's poleVector attribute ## to the locators translation ## which is a way of recording the data ik.poleVector >> loc.translate ## ## now that's it is in the right ## world space location ## we can disconnect it, and unparent it ## as long as its world space location ## stays put, it works, and marks that ## location for later ## ## disconnect loc.translate.disconnect() ## unparent (via parent to world) ## this is how you unparent in code pm.parent( loc, world=True ) ## now finally, we can constrain the ## the ik to the locator, ## using a pole vector constraint #pm.poleVectorConstraint( targetGoal, thingBeingConstrained ) #pm.poleVectorConstraint( master, slave ) pm.poleVectorConstraint( loc, ik )
## at the one exact "magic" ## place where the pole vector ## is for the ik handle ## it's magic, because it's the only ## place that won't "pop" out of the pose ## ## connect the ik's poleVector attribute ## to the locators translation ## which is a way of recording the data ik.poleVector >> loc.translate ## ## now that's it is in the right ## world space location ## we can disconnect it, and unparent it ## as long as its world space location ## stays put, it works, and marks that ## location for later ## ## disconnect loc.translate.disconnect() ## unparent (via parent to world) ## this is how you unparent in code pm.parent(loc, world=True) ## now finally, we can constrain the ## the ik to the locator, ## using a pole vector constraint #pm.poleVectorConstraint( targetGoal, thingBeingConstrained ) #pm.poleVectorConstraint( master, slave ) pm.poleVectorConstraint(loc, ik)