Skip to content

Instantly share code, notes, and snippets.

@coxevan
Last active August 29, 2015 14:02
Show Gist options
  • Save coxevan/dca78a106d01dbca4b37 to your computer and use it in GitHub Desktop.
Save coxevan/dca78a106d01dbca4b37 to your computer and use it in GitHub Desktop.
r"""
Module contains methods for setting up Dot Product relationships and vectorProduct nodes in Autodesk Maya.
Future features:
Cross Product implementation.
"""
import pymel.core as py
def create_dot_relationship(**kwargs):
"""
Script creates creates locator's and relationships between specific joints (driver and reference locators) and determines
the dot product between the two corresponding locator's positions. Runs real time with the use of a VectorProduct node.
Kwargs -
name = name of the relationship ("rt_arm_bicep_flex")
jointOne = name of the top joint ("rt_shoulder")
jointTwo = name of hinge joint ("rt_elbow")
jointThree = name of tip joint ("rt_wrist")
output1 = name of connection for a specific channel ("rt_locator.translateX")
output2 = name of connection for a group of channels ("rt_locator.translate")
normalizeOutput = boolean for normalizing output
"""
selection = py.ls(sl=True)
if len(selection) < 3:
selection = ["", "", ""]
joint_1 = kwargs.setdefault("jointOne", selection[0])
joint_2 = kwargs.setdefault("jointTwo", selection[1])
joint_3 = kwargs.setdefault("jointThree", selection[2])
output_connection_1 = kwargs.setdefault("output1")
output_connection_3 = kwargs.setdefault("output3")
normalize_bool = kwargs.setdefault("normalizeOutput", True)
name = kwargs.setdefault("name")
if isinstance(name, str):
locator_space_group = py.group(em=True, n="{0}_{1}".format(name, "locator_space"))
driver_locator = py.spaceLocator(n="%s_%s" % (name, "locator_driver"))
reference_locator = py.spaceLocator(n="%s_%s" % (name, "locator_reference"))
py.parentConstraint(joint_1, locator_space_group, mo=False)
py.parent(driver_locator, reference_locator, locator_space_group)
temp_constraints = [py.parentConstraint(joint_3, reference_locator),
py.parentConstraint(joint_3, driver_locator)]
py.delete(temp_constraints)
py.parentConstraint(joint_2, driver_locator, mo=True)
vector_product = create_vec_product_node(name=name,
input1=driver_locator,
input2=reference_locator,
normalize=normalize_bool,
operation="dot")
if isinstance(output_connection_1, str):
py.connectAttr(vector_product + ".outputX", output_connection_1)
if isinstance(output_connection_3, str):
py.connectAttr(vector_product + ".output", output_connection_3)
py.select(vector_product)
else:
py.warning("Name parameter must be given a string value")
def create_cross_relationship(**kwargs):
pass
def create_vec_product_node(**kwargs):
"""
Creates a vector product node and sets up input connections
Kwargs -
name = name of vector product node
input 1 = input 1 of vector product node
input 2 = input 2 of vector product node
normalize = boolean
operation = str "dot" or "cross" for different operations
Returns new vector_product node
"""
name = kwargs.setdefault('name')
input1 = kwargs.setdefault('input1')
input2 = kwargs.setdefault('input2')
normalize = kwargs.setdefault('normalize')
operation = kwargs.setdefault('operation')
operation_dict = {"dot": 1, "cross": 2}
vector_product = py.createNode("vectorProduct")
py.rename(vector_product, "%s_%s" % (name, "vector_product"))
py.connectAttr(input1 + ".translate", vector_product + ".input1")
py.connectAttr(input2 + ".translate", vector_product + ".input2")
py.setAttr(vector_product + ".normalizeOutput", normalize)
py.setAttr(vector_product + ".operation", operation_dict[operation])
return vector_product
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment