PDA

View Full Version : snap an object to another object with MEL?


Leffler
08-07-2008, 10:21 AM
Hello

Lets say I want to snap "locator1" to "joint1", is there a fast and easy way to do it?

I donīt want to rotate anything, just snap it ... like holding the V-key in and then snap

When I look in the Script editor after doing that, all i see is coordinates and a move-command

// Otto

kojala
08-07-2008, 10:51 AM
hey try this:

{
string $node[] = `pointConstraint -offset 0 0 0 -weight 1`;
delete $node;
}

first select the object that is in the correct place and then select the object that you want to move.

edit:
here is an even simpler version:

delete `pointConstraint -offset 0 0 0 -weight 1`;

NaughtyNathan
08-07-2008, 12:09 PM
that delete constraint trick is very clever and cool.. but if you want to move/snap multiple objects onto the same spot this little snippet should do..

{
string $sel[] = `ls -sl -tr`;
float $t[] = `getAttr ($sel[size($sel)]+".t")`;
for ($obj in $sel) setAttr ($obj+".t") $t[0] $t[1] $t[2];
}

it'll move every selected object (transform) onto the same spot as the focus (green) object.
it's not bulletproof though, but it's good for 99% of tasks for a quick 30 second hack...

:nathaN

Impus
08-07-2008, 12:25 PM
that delete constraint trick is very clever and cool.. but if you want to move/snap multiple objects onto the same spot this little snippet should do..

{
string $sel[] = `ls -sl -tr`;
float $t[] = `getAttr ($sel[size($sel)]+".t")`;
for ($obj in $sel) setAttr ($obj+".t") $t[0] $t[1] $t[2];
}

it'll move every selected object (transform) onto the same spot as the focus (green) object.
it's not bulletproof though, but it's good for 99% of tasks for a quick 30 second hack...

:nathaN

That method doesn't work so well with objects that aren't under the same parent in a hierarchy. This is primarily because by setting the objects ".t" value you're setting the object space translation and not the world space translation.


However by using 'xform' you can fix that issue:

{
string $sel[] = `ls -sl -tr -l`;
float $t[] = `xform -q -ws -t $sel[size($sel) - 1]`;
for ($obj in $sel) xform -ws -t $t[0] $t[1] $t[2];
}

Leffler
08-07-2008, 12:49 PM
Thanks alot guys, the pointConstrain was clever :)

This is my final version:

select -r L_ankleBuffer ;
select -add L_heelLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r L_heelLoc_pointConstraint1 ;
doDelete;


//Otto

Impus
08-07-2008, 12:54 PM
Can I suggest a slight improvement to this?


select -r L_ankleBuffer ;
select -add L_heelLoc ;
delete `pointConstraint -offset 0 0 0 -weight 1`;


The reason I suggest this is because the 'pointConstraint' operation doesn't necessarily return 'L_heelLoc_pointConstraint1'.

I'm actually pretty curious what you're using this for? It seems to be part of a larger script? Some kind of pose matching script?

Leffler
08-07-2008, 01:19 PM
Hey Rod!

Thanks alot, that worked fine also so Iīll use that rather than my version if the naming isnīt sure to be pointConstrain1

Iīm not working on a way to automaticly create a footSetup with MEL. Iīd love to share my code, improvements can probably be made!

Itīs ALOT OF CODE :) This is the first step


//draw some joints

//script to rename the joints
rename "joint1" "L_hipGroin";
rename "joint2" "L_kneeA";
rename "joint3" "L_kneeB";
rename "joint4" "L_ankle";
rename "joint5" "L_ankleBuffer";
rename "joint6" "L_ball";
rename "joint7" "L_toeBuffer";
rename "joint8" "L_toe";

//check the Local Rotation Axis

//mirror joints
mirrorJoint -mirrorYZ -searchReplace "L" "R";


//script to ADD all the attributes needed for the IK footControllers
//select both footControllers and add these attributes
select -r R_footCtrl_IK L_footCtrl_IK ;
//freeze transform & delete history
makeIdentity -apply true -t 1 -r 1 -s 1 -n 0 -jointOrient;
DeleteHistory;
//lock and hide scale and visibility
setAttr -lock true -keyable false "R_footCtrl_IK.sx";
setAttr -lock true -keyable false "L_footCtrl_IK.sx";
setAttr -lock true -keyable false "R_footCtrl_IK.sy";
setAttr -lock true -keyable false "L_footCtrl_IK.sy";
setAttr -lock true -keyable false "R_footCtrl_IK.sz";
setAttr -lock true -keyable false "L_footCtrl_IK.sz";
setAttr -lock true -keyable false "R_footCtrl_IK.v";
setAttr -lock true -keyable false "L_footCtrl_IK.v";

//add extras as displayable
addAttr -ln "Extras" -at double |R_footCtrl_IK;
setAttr -e-channelBox true |R_footCtrl_IK.Extras;
addAttr -ln "Extras" -at double |L_footCtrl_IK;
setAttr -e-channelBox true |L_footCtrl_IK.Extras;
//add roll attribute
addAttr -ln "roll" -at double -max 145 |R_footCtrl_IK;
setAttr -e-keyable true |R_footCtrl_IK.roll;
addAttr -ln "roll" -at double -max 145 |L_footCtrl_IK;
setAttr -e-keyable true |L_footCtrl_IK.roll;
//add Toe Lift with default value 45
addAttr -ln "toeLift" -at double -dv 45 |R_footCtrl_IK;
setAttr -e-keyable true |R_footCtrl_IK.toeLift;
addAttr -ln "toeLift" -at double -dv 45 |L_footCtrl_IK;
setAttr -e-keyable true |L_footCtrl_IK.toeLift;
//add Toe Straight with default value 90
addAttr -ln "toeStraight" -at double -dv 90 |R_footCtrl_IK;
setAttr -e-keyable true |R_footCtrl_IK.toeStraight;
addAttr -ln "toeStraight" -at double -dv 90 |L_footCtrl_IK;
setAttr -e-keyable true |L_footCtrl_IK.toeStraight;
//add lean
addAttr -ln "lean" -at double |R_footCtrl_IK;
setAttr -e-keyable true |R_footCtrl_IK.lean;
addAttr -ln "lean" -at double |L_footCtrl_IK;
setAttr -e-keyable true |L_footCtrl_IK.lean;
//add side
addAttr -ln "side" -at double |R_footCtrl_IK;
setAttr -e-keyable true |R_footCtrl_IK.side;
addAttr -ln "side" -at double |L_footCtrl_IK;
setAttr -e-keyable true |L_footCtrl_IK.side;
//add Toe spin
addAttr -ln "toeSpin" -at double |R_footCtrl_IK;
setAttr -e-keyable true |R_footCtrl_IK.toeSpin;
addAttr -ln "toeSpin" -at double |L_footCtrl_IK;
setAttr -e-keyable true |L_footCtrl_IK.toeSpin;
//add Toe Wiggle
addAttr -ln "toeWiggle" -at double |R_footCtrl_IK;
setAttr -e-keyable true |R_footCtrl_IK.toeWiggle;
addAttr -ln "toeWiggle" -at double |L_footCtrl_IK;
setAttr -e-keyable true |L_footCtrl_IK.toeWiggle;
//add Knee Spin
addAttr -ln "kneeSpin" -at double |R_footCtrl_IK;
setAttr -e-keyable true |R_footCtrl_IK.kneeSpin;
addAttr -ln "kneeSpin" -at double |L_footCtrl_IK;
setAttr -e-keyable true |L_footCtrl_IK.kneeSpin;
//add stretchy boolean ON/OFF
addAttr -ln "stretchy" -at bool |R_footCtrl_IK;
setAttr -e-keyable true |R_footCtrl_IK.stretchy;
addAttr -ln "stretchy" -at bool |L_footCtrl_IK;
setAttr -e-keyable true |L_footCtrl_IK.stretchy;
//end of ADDING attributes
//****************************************************************
//adding and renaming all the locators needed. all placed at origo.
//the left side
spaceLocator -p 0 0 0;
rename "locator1" "L_heelLoc";
spaceLocator -p 0 0 0;
rename "locator1" "L_toeLoc";
spaceLocator -p 0 0 0;
rename "locator1" "L_outsideLoc";
spaceLocator -p 0 0 0;
rename "locator1" "L_insideLoc";
spaceLocator -p 0 0 0;
rename "locator1" "L_toeWiggleLoc";
spaceLocator -p 0 0 0;
rename "locator1" "L_ballLoc";
spaceLocator -p 0 0 0;
rename "locator1" "L_kneeTwist";
spaceLocator -p 0 0 0;
rename "locator1" "L_kneeLoc";
//the right side
spaceLocator -p 0 0 0;
rename "locator1" "R_heelLoc";
spaceLocator -p 0 0 0;
rename "locator1" "R_toeLoc";
spaceLocator -p 0 0 0;
rename "locator1" "R_outsideLoc";
spaceLocator -p 0 0 0;
rename "locator1" "R_insideLoc";
spaceLocator -p 0 0 0;
rename "locator1" "R_toeWiggleLoc";
spaceLocator -p 0 0 0;
rename "locator1" "R_ballLoc";
spaceLocator -p 0 0 0;
rename "locator1" "R_kneeTwist";
spaceLocator -p 0 0 0;
rename "locator1" "R_kneeLoc";

//snap the locators in place - Left side
select -r L_ankleBuffer ;
select -add L_heelLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r L_heelLoc_pointConstraint1 ;
doDelete;
select -r L_ankleBuffer ;
select -add L_kneeTwist ;
pointConstraint -offset 0 0 0 -weight 1;
select -r L_kneeTwist_pointConstraint1 ;
doDelete;
select -r L_hipGroin ;
select -add L_kneeLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r L_kneeLoc_pointConstraint1 ;
doDelete;
select -r L_toe ;
select -add L_toeLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r L_toeLoc_pointConstraint1 ;
doDelete;
select -r L_ball ;
select -add L_toeWiggleLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r L_toeWiggleLoc_pointConstraint1 ;
doDelete;
select -r L_ball ;
select -add L_ballLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r L_ballLoc_pointConstraint1 ;
doDelete;
//move some stuff roughly in place
select -r L_kneeTwist ;
select -add L_heelLoc ;
move -r 0 0 -0.5 ;
setAttr "L_kneeTwist.translateY" 0;
setAttr "L_heelLoc.translateY" 0;
setAttr "L_insideLoc.translateX" .1;
setAttr "L_outsideLoc.translateX" .9;
setAttr "L_kneeLoc.translateZ" 5;
//snap the locators in place -Right side
select -r R_ankleBuffer ;
select -add R_heelLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r R_heelLoc_pointConstraint1 ;
doDelete;
select -r R_ankleBuffer ;
select -add R_kneeTwist ;
pointConstraint -offset 0 0 0 -weight 1;
select -r R_kneeTwist_pointConstraint1 ;
doDelete;
select -r R_hipGroin ;
select -add R_kneeLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r R_kneeLoc_pointConstraint1 ;
doDelete;
select -r R_toe ;
select -add R_toeLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r R_toeLoc_pointConstraint1 ;
doDelete;
select -r R_ball ;
select -add R_toeWiggleLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r R_toeWiggleLoc_pointConstraint1 ;
doDelete;
select -r R_ball ;
select -add R_ballLoc ;
pointConstraint -offset 0 0 0 -weight 1;
select -r R_ballLoc_pointConstraint1 ;
doDelete;
//move some stuff roughly in place
select -r R_kneeTwist ;
select -add R_heelLoc ;
move -r 0 0 -0.5 ;
setAttr "R_kneeTwist.translateY" 0;
setAttr "R_heelLoc.translateY" 0;
setAttr "R_insideLoc.translateX" -.1;
setAttr "R_outsideLoc.translateX" -.9;
setAttr "R_kneeLoc.translateZ" 5;


//create the IK-handles
ikHandle -sj L_hipGroin -ee L_ankle -n ("L_ikRp_leg") -sticky sticky -solver ikRPsolver;
ikHandle -sj L_ankle -ee L_ball -n ("L_ikSc_ball") -sticky sticky -solver ikSCsolver;
ikHandle -sj L_ball -ee L_toe -n ("L_ikSc_toe") -sticky sticky -solver ikSCsolver;

ikHandle -sj R_hipGroin -ee R_ankle -n ("R_ikRp_leg") -sticky sticky -solver ikRPsolver;
ikHandle -sj R_ankle -ee R_ball -n ("R_ikSc_ball") -sticky sticky -solver ikSCsolver;
ikHandle -sj R_ball -ee R_toe -n ("R_ikSc_toe") -sticky sticky -solver ikSCsolver;

//parent everything according to the image below
parent L_kneeLoc L_kneeTwist ;
parent L_kneeTwist L_ballLoc ;
parent L_ikRp_leg L_ballLoc;
parent L_toeWiggleLoc L_insideLoc;
parent L_ikSc_toe L_toeWiggleLoc;
parent L_ikSc_ball L_insideLoc;
parent L_ballLoc L_insideLoc ;
parent L_insideLoc L_outsideLoc;
parent L_outsideLoc L_toeLoc;
parent L_toeLoc L_heelLoc;
parent L_heelLoc L_footCtrl_IK;
reorder -relative -1 L_ikRp_leg ;
reorder -relative -1 L_ikSc_ball ;

parent R_kneeLoc R_kneeTwist ;
parent R_kneeTwist R_ballLoc ;
parent R_ikRp_leg R_ballLoc;
parent R_toeWiggleLoc R_insideLoc;
parent R_ikSc_toe R_toeWiggleLoc;
parent R_ikSc_ball R_insideLoc;
parent R_ballLoc R_insideLoc ;
parent R_insideLoc R_outsideLoc;
parent R_outsideLoc R_toeLoc;
parent R_toeLoc R_heelLoc;
parent R_heelLoc R_footCtrl_IK;
reorder -relative -1 R_ikRp_leg ;
reorder -relative -1 R_ikSc_ball ;

Leffler
08-07-2008, 01:24 PM
And after that I ... TRY ... to run the code that connects the foot-attributes but Maya crashes every time .... :hmm: Donīt know why, this script used to work


$roll = L_footCtrl_IK.roll;
$toeLift = L_footCtrl_IK.toeLift;
$toeStraight = L_footCtrl_IK.toeStraight;
$lean = L_footCtrl_IK.lean;
$side = L_footCtrl_IK.side;
$spin = L_footCtrl_IK.toeSpin;
$wiggle = L_footCtrl_IK.toeWiggle;
$kneeSpin = L_footCtrl_IK.kneeSpin;

L_heelLoc.rotateX = min($roll,0);
L_ballLoc.rotateX = (linstep(0,$toeLift,$roll)) * (1-(linstep($toeLift,$toeStraight,$roll))) * $roll;
L_toeLoc.rotateX = linstep($toeLift, $toeStraight, $roll) * $roll;
L_ballLoc.rotateZ = $lean;
L_outsideLoc.rotateZ = min($side,0);
L_insideLoc.rotateZ = max(0,$side);
L_toeLoc.rotateY = $spin;
L_toeWiggleLoc.rotateX = $wiggle;
L_kneeTwist.rotateY = $kneeSpin;


You might recognise it from the Jason Schleiffer DVDs :)

Any ideas why Maya crashes from this? Shouldnīt be a cycle ...

// Otto

Impus
08-07-2008, 01:48 PM
Might be good to post a scene (.ma) of this setup. It's hard to check for a bug without having the rig setup and the script you've created doesn't do everything from scratch. I'm guessing it assumes a standard reverse foot setup?


In general I think your code could be cleaned up a lot with some useful procedures:


i.e.

setAttr -lock true -keyable false "R_footCtrl_IK.sx";
setAttr -lock true -keyable false "L_footCtrl_IK.sx";
setAttr -lock true -keyable false "R_footCtrl_IK.sy";
setAttr -lock true -keyable false "L_footCtrl_IK.sy";
setAttr -lock true -keyable false "R_footCtrl_IK.sz";
setAttr -lock true -keyable false "L_footCtrl_IK.sz";
setAttr -lock true -keyable false "R_footCtrl_IK.v";
setAttr -lock true -keyable false "L_footCtrl_IK.v";


could be replaced with


proc setKeyableLockValues(string $inputObjs[], string $attrs[], int $keyable, int $lock)
{
for($eachObj in $inputObjs)
{
for($eachAttr in $attrs)
{
setAttr -lock $lock -keyable $keyable ($eachObj + "." + $eachAttr);
}
}
}

setKeyableLockValues({"R_footCtrl_IK", "L_footCtrl_IK"}, {"sx", "sy", "sz", "v"}, 0, 1);

Leffler
08-08-2008, 07:59 AM
Well its kind of the standard reverse foot but with locators insteed of joints to drive the "reverse action"

I have trouble with saving MA-files in Maya 2008 so I upload it as MB, hope it works

Two scenes, one with the scripts working fine and one were I took out the script so all that should be needed to do is run it ... but then Maya crashes

CGTalk Moderation
08-08-2008, 07:59 AM
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.