PDA

View Full Version : Forming a float from a string, then setting attr.


dwalden74
12-19-2003, 11:52 AM
I need to declare an float variable as either positive or negative based on another criteria, then set many different obj.attrīs to this final float value.

OK, so for an example of the float variable:

string $minus = "-";
float $temp = ($minus + 5);
// Result: -5 //

This is fine. The problem occurs when I call `setAttr`, which doesnīt seem to accept a float in this "stringy" format:

setAttr objA.rz ($minus + 5);
// Error: line 2: Invalid flag: -5 /

And if I declare $minus as a "plus" character, I get a different error:
string $minus = "+";
setAttr objA.rz ($minus + 5);
// Error: line 2: Error reading data element number 1: +5 //


Does anyone know if there is a syntax that will work for this? The workaround Iīve thought of would be to put the "string" float into a new float, and set my attrs to this new float. However, thatīs an extra step I would prefer to avoid, seeing as I have to change around 100 attrs in my script.

BTW, this is part of a setup script which is setting joint rotations based on a given axis.

:beer:
David

macaroniKazoo
12-19-2003, 12:27 PM
david, try this: setAttr objA.rz ((float)($minus + 5));

you can use the (int) to cast data as whatever you want it to be for a certain command. its kinda useful when dealing with strings and floats.

dwalden74
12-19-2003, 12:48 PM
Thanks for showing me something new, Mac. Thatīs exactly it.

:beer:
David

dwalden74
12-19-2003, 03:05 PM
david, try this: setAttr objA.rz ((float)($minus + 5));

Ok, Iīm half the way there. Believe it or not, now I have to take the resulting value, and either leave it at itīs current value or negate it, then set the attr value to this. Like this:

string $minus = "-";
float $startValue = ($minus + 5);
float $finalValue = ($minus + ($minus + 5)); //here īs the error.
setAttr objA.rz $finalValue;

If itīs a negative number, negating it would make it positive, and vice-versa.... any ideas?

-d

GDC
12-19-2003, 06:07 PM
If you have need to make a value the opposite of what it is try this:

string $minus="-";
float $startValue=($minus+5);
$startValue = ($startValue - ($startValue*2));
setAttr objA.rz $startValue;

--g




Originally posted by dwalden74
Ok, Iīm half the way there. Believe it or not, now I have to take the resulting value, and either leave it at itīs current value or negate it, then set the attr value to this. Like this:

string $minus = "-";
float $startValue = ($minus + 5);
float $finalValue = ($minus + ($minus + 5)); //here īs the error.
setAttr objA.rz $finalValue;

If itīs a negative number, negating it would make it positive, and vice-versa.... any ideas?

-d

dwalden74
12-19-2003, 07:51 PM
Hi-

yeah, I donīt think Iīm explaining my problem very well. I want to negate its value based on the value of another variable. If my criteria is met, my $negateString is assigned to "-" (the value will be negative). Else, $negateString is assigned to "+" (the value will be positive). The $negateString is combined with a numeric value to arrive at an initial float value, like this:
float $startValue = ($negateString + 5).

This will return either 5 or -5 based on my criteria.

But now I need to negate *this* value based on another criteria (the negative will become positive, as in --5, or the positive will become negative, as in 5).
I think the problem lies in the fact that Maya will not convert a string with a double negative (like "--5") into a positive float. It reads it as some kind of impossible conversion and reports

// Warning: line 2: Converting string "--5" to a float value of 0.

So I may have to find another solution.... :(

ajk48n
12-20-2003, 05:10 AM
What about something like this:

int $r = 5;
int $g;

if (condition to negate)

$g = $r - 2*$r;

dwalden74
12-20-2003, 07:18 AM
Yes, in theory a conditional like this should work. The only reason I didnīt want that was because Iīm setting many different attrs in my script, and I didnīt want to have to create the conditional scenario each time I need to set an attr value. Doing it directly with a string would be the fastest and most direct solution. But I think now I have no choice, really, so Iīm working out a conditional now.

thanks-
David

galactor
12-20-2003, 11:04 AM
You can also turn that string that contains "-" into a integer that has an value of "-1" and multiply that. The value will only change form positive to negative, and visa versa.

:: Galactor ::

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