View Full Version : How to swap the Z position of two objects ?
Horganovski 08-14-2007, 02:18 PM I'm starting to practice walkcycles at the moment and one thing I find myself doing a lot is swaping the Z position of the feet while setting them up to loop.
At the moment I find I end up squaring off the values of the positions of each controller and then manually copying the Z values from one to the other.. this seems to me to be a slow way to do it, and I've been looking for a quicker way to swap the positions, as I've seen mentioned in tuts for other apps..
I've been looking at the transfer function but as far as I can see there isin't any way to do this..
Perhaps there's a script out there that can do it ? It seems like the kind of thing that would have come up before, but searching hasin't thrown up anything yet..
Any ideas appreciated..
Thanks
Brian
|
|
Per-Anders
08-14-2007, 04:52 PM
You can write a script to do this yourself, it's not hard, the most complex bit is getting the two objects, basically your script would look something like:
//Returns the next object in the list to "op"
next_object(op)
{
if (!op) return NULL;
if (op->GetDown()) return op->GetDown();
while (!op->GetNext() && op->GetUp()) op = op->GetUp();
return op->GetNext();
}
//Returns the first selected object it finds
next_selected(op)
{
while (op)
{
if (op->GetBit(BIT_ACTIVE)) return op;
op = next_object(op);
}
return NULL;
}
main(doc, op)
{
//Find two selected objects in the document
var first_object = next_selected(doc->GetFirstObject()),
second_object = next_selected(next_object(first_object));
if (!first_object || !second_object) return FALSE;
//Get the objects positions
var first_pos = first_object->GetPosition();
var second_pos = second_object->GetPosition();
//Swap the Z's around
var temp = first_pos.z;
first_pos.z = second_pos.z;
second_pos.z = temp;
//Set the objects new positions
first_object->SetPosition(first_pos);
second_object->SetPosition(second_pos);
return TRUE;
}
Horganovski
08-14-2007, 06:14 PM
That does exactly what I want to do.. thanks a million..
I don't know much code yet (apart from some Flash Actionscript2!) but I reckon I can modify that to do other stuff if needed.. thank you again,
Brian
Horganovski
08-14-2007, 08:07 PM
I've managed to modify that script to control the Y or X position, and now I'm having a look to se if I can swap the rotations too, I see that you can use the GetRotation property, but I can't seem to access the individual settings.. I tried using .h as in heading but that didn't work.. Here's where I am so far but this doesin't work yet..
It seems like I'm close.. any ideas appreciated..
here's the snippet of code I've modified..
//Get the objects rotations
var first_rot = first_object->GetRotation();
var second_rot = second_object->GetRotation();
//Swap the h's around
var temp = first_rot.h;
first_rot.h = second_rot.h;
second_rot.h = temp;
//Set the objects new positions
first_object->SetRotation(first_rot);
second_object->SetRotation(second_rot);
Per-Anders
08-14-2007, 09:02 PM
It returns a vector, the same as GetPosition, h = x, p = y, b = z.
Horganovski
08-14-2007, 09:09 PM
That's fantastic.. thank you again.. much appreciated. I'm combining those functions together so I'll have a one button solution.. should be a great time saver..
Cheers,
Brian
Horganovski
08-14-2007, 11:20 PM
Ok.. here's my finished version.. I like to call it the 'Walkcycle Buddy' :)
Most typical use would be to select two arm or leg controllers, run the script and it will swap their Z,Y and Pitch coordinates and mirror their X, Heading and Banking Coordinates..
I thought I'd post it in case it's useful for someone else..
Thanks again for your help.
Cheers,
Brian
//Returns the next object in the list to "op"
next_object(op)
{
if (!op) return NULL;
if (op->GetDown()) return op->GetDown();
while (!op->GetNext() && op->GetUp()) op = op->GetUp();
return op->GetNext();
}
//Returns the first selected object it finds
next_selected(op)
{
while (op)
{
if (op->GetBit(BIT_ACTIVE)) return op;
op = next_object(op);
}
return NULL;
}
main(doc, op)
{
//Find two selected objects in the document
var first_object = next_selected(doc->GetFirstObject()),
second_object = next_selected(next_object(first_object));
if (!first_object || !second_object) return FALSE;
//Get the objects rotations
var first_rot = first_object->GetRotation();
var second_rot = second_object->GetRotation();
//Get the objects positions
var first_pos = first_object->GetPosition();
var second_pos = second_object->GetPosition();
//Swap the Z's and Y's around and Mirror the X's
var tempx = first_pos.x*-1;
var tempz = first_pos.z;
var tempy = first_pos.y;
first_pos.z = second_pos.z;
second_pos.z = tempz;
first_pos.y = second_pos.y;
second_pos.y = tempy;
first_pos.x = second_pos.x*-1;
second_pos.x = tempx;
//Swap the Rotations around - Heading and Banking are Mirrored
var temphead = first_rot.x*-1;
var temppitch = first_rot.y;
var tempbank = first_rot.z*-1;
first_rot.x = second_rot.x*-1;
second_rot.x = temphead;
first_rot.y = second_rot.y;
second_rot.y = temppitch;
first_rot.z = second_rot.z*-1;
second_rot.z = tempbank;
//Set the objects new positions
first_object->SetPosition(first_pos);
second_object->SetPosition(second_pos);
//Set the objects new rotations
first_object->SetRotation(first_rot);
second_object->SetRotation(second_rot);
return TRUE;
}
CGTalk Moderation
08-14-2007, 11:20 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.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.