Skip to content

Instantly share code, notes, and snippets.

@jm991
Last active November 6, 2017 05:05
Show Gist options
  • Save jm991/b558687598c6a5fa404be4ce3862201f to your computer and use it in GitHub Desktop.
Save jm991/b558687598c6a5fa404be4ce3862201f to your computer and use it in GitHub Desktop.
Creates a symmetry constraint between two selected joints (useful for arms, legs, eyes, etc.)
//---------------------------------------------------------------------------------
// Source: https://gist.github.com/jm991/
// http://griffinanimation.blogspot.com/2014/11/maya-symmetry-constraint.html
// https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/Maya-Tech-Docs/Nodes/symmetryConstraint-html.html
// Author: John Mac
// Date: 11-05-2017
//
// Description:
// Creates a symmetry constraint between two selected joints (useful for arms, legs, eyes, etc.) using the same settings as Maya default setup as Joint Tool > Symmetry > _-Axis
// Maya has an option to create symmetrical joints using the joint tool, but the constraint doesn't have an option to apply it to existing joints
// Until now!!! (with this script)
// Thanks to Griffin Animation who published a python version for transforms - this script is a .mel adaptation that works on joints
// Another advantage this script has over using Joint Tool > Symmetry is that this script allows for Orientation (non-flipped local rotation axes) joint mirroring which is useful for legs or eyes - Joint Tool > Symmetry only creates Behavior (flipped local rotation axes) joint mirroring
// Commands:
// createJointSymmetry(0),createJointSymmetry(1)
// Installation:
// 1. Copy createJointSymmetry.mel to C:\Users\[USER]\Documents\maya\[MAYAVERSION]\prefs\scripts\jm991
// 2. Restart maya
// Instructions:
// createJointSymmetry(0): applying symmetry to two existing mirrored joints
// 1. In the Outliner, select the joint you want to use as the source/master in the symmetry relationship
// 2. CTRL select the joint you want to use as the target to recieve the symmetry
// 3. Run this script
// createJointSymmetry(1): mirror a joint chain, and apply symmetry to the resulting joints
// 1. In the Outliner, select the root of the joint chain you want to mirror
// 2. Run the script
// Dependencies:
// printextensions.mel https://gist.github.com/jm991/29cc46b5b8e5fa4709ee4be0a7911095
global proc createJointSymmetry(int $option)
{
// parse the joint selections into an array
string $s[] = `ls -sl`;
if ($option == 0)
{
// first selected/0 = source joint, second selected/1 = target joint to apply symmetry
setSymmetryConstraint($s[0], $s[1]);
}
else if ($option == 1)
{
// select the joint heirarchy
select -hi $s[0];
string $originalJnts[] = `ls -sl`;
// Return selection back to first selected joint since you can't mirror a full hierarchy
select $s[0];
string $search = "_lf";
string $replace = "_rt";
string $mirrorJnts[] = `mirrorJoint -mirrorYZ -searchReplace $search $replace`; // -mirrorBehavior
if (size($originalJnts) == size($mirrorJnts))
{
int $i = 0;
for ($i = 0; $i < size($originalJnts); $i++)
{
setSymmetryConstraint($originalJnts[$i], $mirrorJnts[$i]);
}
}
}
}
global proc setSymmetryConstraint(string $jntSrc, string $jntTgt)
{
string $symConstName = $jntTgt + "_symmetryConstraint";
// Create the symmetryConstraint and parent it to the target joint
string $symNode = `createNode "symmetryConstraint" -n $symConstName -p $jntTgt`;
println(objectType($symNode));
println($symNode);
// Set up the constraint
connectAttr(($jntSrc + ".translate"), ($symNode + ".targetTranslate"));
connectAttr(($jntSrc + ".rotate"), ($symNode + ".targetRotate"));
connectAttr(($jntSrc + ".scale"), ($symNode + ".targetScale"));
connectAttr(($jntSrc + ".parentMatrix[0]"), ($symNode + ".targetParentMatrix"));
connectAttr(($jntSrc + ".worldMatrix[0]"), ($symNode + ".targetWorldMatrix"));
connectAttr(($jntSrc + ".rotateOrder"), ($symNode + ".targetRotateOrder"));
connectAttr(($jntSrc + ".jointOrient"), ($symNode + ".targetJointOrient"));
connectAttr(($symNode + ".constraintTranslate"), ($jntTgt + ".translate"));
connectAttr(($symNode + ".constraintRotate"), ($jntTgt + ".rotate"));
connectAttr(($symNode + ".constraintScale"), ($jntTgt + ".scale"));
connectAttr(($symNode + ".constraintRotateOrder"), ($jntTgt + ".rotateOrder"));
connectAttr(($symNode + ".targetJointOrient"), ($jntTgt + ".jointOrient"));
connectAttr(($jntTgt + ".parentInverseMatrix[0]"), ($symNode + ".constraintInverseParentWorldMatrix"));
}
@jm991
Copy link
Author

jm991 commented Nov 6, 2017

Remove the ".c" from the filename when you download this (git doesn't support .mel syntax highlighting)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment