Skip to content

Instantly share code, notes, and snippets.

@jm991
Last active November 7, 2017 04:06
Show Gist options
  • Save jm991/0afb4fefe0a17d4bd368aea28da7d99f to your computer and use it in GitHub Desktop.
Save jm991/0afb4fefe0a17d4bd368aea28da7d99f to your computer and use it in GitHub Desktop.
Constrains the joint heirarchy to the deformation heirarchy, joint by joint
//---------------------------------------------------------------------------------
// Source: https://app.pluralsight.com/library/courses/maya-2017-rigging-introduction/table-of-contents
// Author: Eric Kunzendorf and John Mac
// Date: 11-05-2017
//
// Description:
// Constrains the joint hierarchy to the deformation hierarchy, joint by joint
// Has error handling to skip over anything that isn't a joint and to check joint matches based on name
// Assumes joint naming following this convention:
// _c_ center
// _rt_ right
// _lf_ left
// _DEF deformation skeleton
// _JNT joint skeleton
// All of these tokens will be ignored in joint name matching.
// Installation:
// 1. Copy constrainJntToDefSkeleton.mel to C:\Users\[USER]\Documents\maya\[MAYAVERSION]\prefs\scripts\jm991
// 2. Restart maya
// Instructions:
// 1. Select the jnt heirarchy
// 2. CTRL select the def heirarchy
// 3. Run this script
// Dependencies:
// printextensions.mel https://gist.github.com/jm991/29cc46b5b8e5fa4709ee4be0a7911095
global proc constrainJntToDefSkeleton()
{
// set up tokenizer filter with all ignored joint name keywords
string $ignore[] = stringToStringArray("_c_ _rt_ _lf_ _DEF _JNT", " ");
// parse the two joint selections into an array 0 = JNT 1 = DEF skeletons.
string $s[] = `ls -sl`;
// select the JNT heirarchy
select -hi $s[0];
// put the list of JNT joint names into an array
string $jntSel[] = `ls -sl`;
// select the DEF heirarchy
select -hi $s[1];
// put the list of DEF joint names into an array
string $defSel[] = `ls -sl`;
// Verify that the arrays are the same size
println("jntSel: " + size($jntSel));
printarray($jntSel);
println("defSel: " + size($defSel));
printarray($defSel);
if (size($jntSel) == size($defSel))
{
// initialize the for-loop counter
int $i = 0;
// get the number of names in the array using the size command
int $arSize = size($jntSel);
// constrain each DEF joint to the matching JNT name. NOTE constrained object
// is always last. Size is the number of names to get accurate for-loop, you must stop
// at size -1.
for ($i = 0; $i <= ($arSize - 1); $i++)
{
$jntType = objectType($jntSel[$i]);
$defType = objectType($defSel[$i]);
// println($jntType);
// println($defType);
if ($jntType == "joint" && $defType == "joint")
{
$curJntNameFiltered = filterString($jntSel[$i], $ignore);
$curDefNameFiltered = filterString($defSel[$i], $ignore);
if ($curJntNameFiltered == $curDefNameFiltered)
{
orientConstraint -offset 0 0 0 -weight 1 $jntSel[$i] $defSel[$i];
}
else
{
string $errorStr = "The joint and deformation skeletons don't match (found mismatched joint names " + $curJntNameFiltered + " and " + $curDefNameFiltered + ".";
error $errorStr;
break;
}
}
}
}
else
{
error "The joint and deformation skeletons don't match (different number of children).";
}
}
global proc string filterString(string $str, string $filter[])
{
int $j = 0;
$filteredStr = $str;
// println("Before filter: " + $filteredStr);
for ($j = 0; $j < size($filter); $j++)
{
$filteredStr = substitute($filter[$j], $filteredStr, "");
// println("Applying filter " + $filter[$j] + ": " + $filteredStr);
}
// println("After filter: " + $filteredStr);
return $filteredStr;
}
@jm991
Copy link
Author

jm991 commented Nov 5, 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