PDA

View Full Version : Swap UserData Script?


Horganovski
03-14-2008, 11:55 PM
Hey Folks,

A while ago I got some help here to make a script that will swap the coordinates and rotations of two selected objects. I adapted this a little so that after selecting two objects and running the script it will swap their Y+Z+H and mirror their X+P+B values.
This has proved to be very useful for setting up walkcycles for characters as I demonstrate here - http://www.graphite9.com/Walkcycle_Buddy.mov

Now I would like to adapt this script a little so that instead of position and rotation values, it can access some or all of the UserData sliders on each selected object and swap their values instead. I have been digging through the SDK and trying various combinations of GetData and SetData but really am not sure I am accessing the right parameters as nothing has worked so far. I realise I should probably start with something easier, but I would really appreciate any advice on how to access and manipulate the UserData sliders.

I enclose the previous script which I mentioned above by way of example and in case anyone finds it useful.


//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;
}


Cheers,
Brian

Horganovski
03-18-2008, 11:53 PM
One of the members over on the C4DCafe gave me the answer.. here is the finished script in case it's useful for someone else.

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;


//Swap UserData - NB the number between the;; decides which userdata
var tempUserdata1;
tempUserdata1=first_object#ID_USERDATA:1;
first_object#ID_USERDATA:1=second_object#ID_USERDATA:1;
second_object#ID_USERDATA:1=tempUserdata1;

////


return TRUE;
}

LucentDreams
03-19-2008, 04:51 AM
the mirror tool does all of this already.

Horganovski
03-19-2008, 06:16 AM
I'm trying the mirror tool now but I can't get it to swap the position of both objects, it just copies one to the other. It's also messing up the userdata on one side as some of the values need to be negated to get the rotations in the opposite direction.

To illustrate what I'm trying to do, here's a clip of the 1st version of the script that just swapped the postion and the rotations, taking into account that the character would be aligned in the -Z direction.

http://www.graphite9.com/Walkcycle_Buddy.mov

The one I'm working on now will do this and also swap the userdata sliders, negating the values as appropriate. The only tricky bit has been 'searching' to find which userdata slider related to which number by trial and error, as it seems they get numbered when you create them and if you move their order using 'manage userdata' after this to organise the interface this doesn't change the references to match their new positions.

I don't know if there's some way to see how they are assigned through the console maybe? It seems that even dividers get assigned a number, but again it's a matter of remembering what order they were created in.

Cheers,
Brian

LucentDreams
03-19-2008, 09:12 PM
your user data should never need to be negated, The idea behind user data is to make user friendly amounts so don't use negative rotations and such, use simple things like percentages, let xpresso handle negative outputs for you using range mapper. That way you can select both controllers at the same time and manipulate them as well. if one's positive and the other negative your making things more difficult.

Horganovski
03-20-2008, 01:38 AM
I'm the 1st to admit that I am self-taught and far from expert but I'm not sure if I agree with you on this.

I guess it would make it easier to copy keys from one controller to another if they are set up with the same values, but it seems counter-intuitive for posing and editing in the timeline to be looking at reversed rotation values on one side of the character, for example if you have a Heel Twist attribute set up the way you describe the controller on the right foot would have a positive rotation value when in fact it's rotated in a negative direction to make the character point his toe away from his body.
Then if you rotate the Foot controller object itself you would have also to remember that it is rotating opposite to the userdata value.

I get what you mean about converting everything to percentages, but again this seems counter-intuitive for rotations, it seems easier to visualise what 45degrees looks like rather than dealing with that as a percentage.

I've looked at a few rigs to confirm this and two Digital Tutors Maya rigs (one biped, one horse), and 2 Cinema Rigs (one is the Rabbit rig from the 10.5 DVD by Kiteman, the other is IKBob by Cactus Dan) that I have are rigged with the controllers negated as they are on my rigs. The Kiteman rig does have percentages for the knee twist attribute instead of degrees, but again negated on the right side. The Maya rigs have the rotation attributes set up in degrees for everything.

I'm not saying these are the 'right' rigs, just the ones I happen to have as reference.
I'm sure that, as with everything, there's absoute no right or wrong with this, I guess it depends what you find comfortable to work with.

Edit - After looking at some of your work online I think you may have alot more experience in this area than I first suspected! Forgive me if I was defensive but I feel your first comments were a little abrupt. Maybe you can point to some rig examples that are more like you describe? If you were to rig everything with percentages how would you approach, say a Heel twist? I'm just trying to think of something that would be practical but also controlable.

Cheers,
Brian

kiteman
07-06-2008, 01:05 PM
Hello Brian ,


thanks for sharing your code , I'm just trying to do a mirror tool in expresso. I don't really know things in coffee/C+ , but I'll try to understand it :)

thanks.

clement

Horganovski
07-06-2008, 05:17 PM
Hey Clement,

Glad you find it useful.

BTW I recently finished your 10.5 tutorial DVD, it was a little tricky in a couple of places but I learned a lot from it.. I really love the bendy setup for the arms and legs, many thanks for putting it together!

Cheers,
Brian

CGTalk Moderation
07-06-2008, 05:17 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.