PDA

View Full Version : Scripting an expression


kmp3d
12-30-2003, 02:08 AM
I'm trying to figure out to add an animation expression to the attribute of an object through a script... let me show you what I have so I can better explain my problem. This just a simple example to explain what I want to acheive. This script creates 3 locators and adds an expression to one that will cause it maintain a center point between the other two locators just on the X axis. As you can see the name I'm casting into the script is "bob". So it creates the locators adding "bob" to the name of the locators. Now the animation expression works at this point because I used the string "bob" but what if I use "carl"?... the animation expression no longer works because its looking for the string
"bob". So my question is how can I cast any string I want into the procedure and the animation expression will accept it. I'd imagine that there are many times when you want to add animation expressions to a lot of different objects.... but it shouldn't be necessary to alter your code for the name of every different object. Thanks for any help.

scripter "bob";

global proc scripter (string $name)
{

spaceLocator -p 0 0 0 -n ($name + "_locator");

spaceLocator -p 0 0 0 -n ($name + "_locator2");
move -r -os -wd 10 0 0 ;

spaceLocator -p 0 0 0 -n ($name + "_middle");
move -r -os -wd 5 0 0 ;

select -d;


//the exprression that only likes "bob"

expression -s "bob_middle.translateX =(abs (bob_locator.translateX - bob_locator2.translateX)) /2;"
-o bob_middle -ae 1 -uc all ;

}

misterdi
12-30-2003, 08:10 AM
Your script cannot understand "carl" since "bob" is hard coded to the expression.

To have the expression be more flexible try using variables instead of constant string.

Here is an example, I just add 3 string variables to hold the locators name and 1 string variable for the expression (you don't really needed if you want to put it directly into the expression command.

global proc scripter (string $name)
{
string $locator[] = `spaceLocator -p 0 0 0 -n ($name + "_locator")`;
string $locator2[] = `spaceLocator -p 0 0 0 -n ($name + "_locator2")`;
move -r -os -wd 10 0 0 ;

string $middle[] =`spaceLocator -p 0 0 0 -n ($name + "_middle")`;
move -r -os -wd 5 0 0 ;

select -d;


//the exprression that only likes any string

string $expression = $middle[0] + ".translateX = (abs (" + $locator[0] +
".translateX - " + $locator2[0] + ".translateX)) / 2;";

expression -s $expression -o $middle[0] -ae 1 -uc all ;
}

kmp3d
12-30-2003, 06:59 PM
thanks for the help, thats what I needed :thumbsup:

CGTalk Moderation
01-17-2006, 01:00 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.