View Full Version : Path Constraint Script

04 April 2012, 07:35 PM
My contribution to the Maya community.

Path Constraint
version 0.2
Matthew L. Stoehr Objective:
This script will attach objects to a curve.

Simply select the objects, then select the curve last.

Selection order is the order of objects on the path, from 0 to 1 of the path U value.
Works best if the curve is drawn from the top view, but it's not required.


string $mySelection[] = `ls -selection`;
int $size = size($mySelection);
int $objCount = $size - 1;

/*----------------- Find the curve ---------------------*/
//if the node is a curve, don't group it, and use it for the namespace
//last in selection
string $path = $mySelection[$objCount];
//make the offset controller
spaceLocator -absolute -position 0 0 0 -name ($path + "_offset_00");
//collect all offset controllers, locator transforms, and find the most recently created locator
string $myOffsets[] = `ls -tr ($path + "_offset_*")`;
int $offsetsCount = size($myOffsets);
string $recentOffset = $myOffsets[$offsetsCount-1];

/*---------------------- Make a group per object -----------------------*/
//psuedo: make a group for each selection, so it has the motion path, not the artist's objects.
for ($i = 0; $i < $objCount; $i++){
select -r ($mySelection[$i]);

//finds the pivot point of the selection, via query (-q)
vector $v = `xform -q -sp`;

float $x = $v.x;
float $y = $v.y;
float $z = $v.z;

group -n ($path + "_motion_grp");

//set the group's pivot to the object's pivot location
xform -os -piv $x $y $z;
//use the curve name for the namespace

//collect the new groups
string $myMotionGroups[] = `ls ($path + "_motion_grp*")`;

/*---------------------- Path Constraint -----------------------*/
float $offset = 0.0;
float $spacing = 1.0;
//objCount - 1 so the objects distribute along the entire curve.
float $divisions = $spacing/($objCount-1);

for($i=0; $i < $objCount; $i++){

float $uLinearDistribution = $divisions * ($i+$offset);
string $motion = `pathAnimation -f 1 -fm 1 -etu 0 -c $path $myMotionGroups[$i]`;

string $source = $motion + "_uValue.output";
string $destination = $motion + ".uValue";
disconnectAttr $source $destination;

setAttr ($motion + ".uValue") $uLinearDistribution;

expression -s ($motion + ".uValue = "+ $uLinearDistribution +" + "+ $recentOffset +".translateX * 0.1") -o $motion;
//expression -s ($motion + ".uValue = "+ $spacing +"/(" + $objCount + "*("+ ($i+1) +" + "+ $path + ".offset));") -o $motion;


/*----------------------- Group All to Organize the Outliner----------------*/
for ($i = 0; $i < $objCount; $i++){
select -add ($myMotionGroups[$i]);
//rename the motion groups to allow more objects to attach to the same path
rename $myMotionGroups[$i] "_motion_grp_";
group -n ($path + "_grp_00");

select -r $path;

CGTalk Moderation
04 April 2012, 07:35 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.