View Full Version : Loop through array
beetz15s 05-15-2009, 03:32 PM for($item in $selected)
{
setAttr ($selected[0] + ".translateX") $camRX ;
setAttr ($selected[0] + ".translateY") $camRY ;
setAttr ($selected[0] + ".translateZ") $camRZ ;
}
I tried a few ways including $i++ increment. if someone can help me with the correct syntax for looping through every object in the array
|
|
scroll-lock
05-15-2009, 04:01 PM
replace $selected on the three rows with $item
for($item in $selected)
{
setAttr ($item + ".translateX") $camRX ;
setAttr ($item + ".translateY") $camRY ;
setAttr ($item + ".translateZ") $camRZ ;
}
beetz15s
05-15-2009, 04:36 PM
thanks alot. Works like a charm.
My next problem would be how to get the aim direction of a camera so that i can move a object in front of the camera.
strarup
05-18-2009, 08:19 AM
hi,
you can use the Camera world center of interest point... this point is always in front of the camera...
e.g. for the perspective camera... --> camera -q -wci persp;
convert the "world center of interest point" into a vector... and do the same with the worldposition
of the camera... then substract the "interest point" from the world position into a new vector...
convert this Vector to a unit vector and multiply it with minus 1 and the distance you want the object moved away from the
camera...
I have made 2 examples scripts... test1 uses a parentConstraint to first move the Object to the camera... and then move
it relative in the distance away from the camera...
in test 2 it adds the worldposition of the camera to the "MovePos"... to move the object to the worldposition given with
the distance away from the camera...
this will move the object directly in front of the camera... :)
proc moveDaObjectInFrontOfCamTest1(string $daCam, string $daObj, float $daDval)
{
vector $daCamPos = `camera -q -p $daCam`;
vector $daWciPos = `camera -q -wci $daCam`;
vector $daDistVec = $daCamPos - $daWciPos;
vector $daDistUnitVec = `unit $daDistVec`;
float $daMovePos[] = {(-1*($daDval*$daDistUnitVec.x)),(-1*($daDval*$daDistUnitVec.y)), (-1*($daDval*$daDistUnitVec.z))};
delete `parentConstraint $daCam $daObj`;
move -r $daMovePos[0] $daMovePos[1] $daMovePos[2] $daObj;
}
//moveDaObjectInFrontOfCamTest1(arg1, arg2, arg3);
moveDaObjectInFrontOfCamTest1("camera1", "pCube1", 5);
proc moveDaObjectInFrontOfCamTest2(string $daCam, string $daObj, float $daDval)
{
vector $daCamPos = `camera -q -p $daCam`;
vector $daWciPos = `camera -q -wci $daCam`;
vector $daDistVec = $daCamPos - $daWciPos;
vector $daDistUnitVec = `unit $daDistVec`;
float $daMovePos[] = {((-1*($daDval*$daDistUnitVec.x))+$daCamPos.x),((-1*($daDval*$daDistUnitVec.y))+$daCamPos.y), ((-1*($daDval*$daDistUnitVec.z))+$daCamPos.z)};
//if you want it to match the rotation of the cam...
//delete `orientConstraint $daCam $daObj`;
//move to worldpos...
move -a -ws $daMovePos[0] $daMovePos[1] $daMovePos[2] $daObj;
}
//moveDaObjectInFrontOfCamTest2(arg1, arg2, arg3);
moveDaObjectInFrontOfCamTest2("camera1", "pCube1", 5);
hope that helps a bit... :)
kind regards
Strarup
CGTalk Moderation
05-18-2009, 08:19 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.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.