View Full Version : // Error: Cannot use data of type float in a scalar operation
10 October 2010, 11:31 AM
Not sure why I am getting this error, but I am trying to convert a negative float value to positive, so that is my real question.
I am trying to multiply a float value by itself, in order to get a positive value:
float $theVelocity1 = ((`getAttr rigidBody2.velocity`)*(`getAttr rigidBody2.velocity`));
// Error: Cannot use data of type float in a scalar operation. //
my thought was to divide $theVelocity1 by (`getAttr rigidBody2.velocity`) to get my positive value. I assume there is a better way?
Thanks for reading!!
10 October 2010, 12:28 PM
I'm not sure exactly what's going wrong in your code there Wolfy, but multiplying a value by itself..? is that really what you want to do here..?!
-5.02 * -5.02 = 25.2004
first, does `getAttr rigidBody2.velocity` actually return a single float?
second, you are trying to assign the result to an array, and that's not gonna work is it?
anyway, if you simply want to reverse the sign of any number you can multiply it by -1 (not itself!) or, you can use the abs command on any number (neg or pos) to get the positive, and if you want the sign of a number (-1, 0 or +1) use the sign command. (in MEL that is!)
10 October 2010, 01:03 PM
That will work!
Yeah, I think I was missing something fundamental there. I think I need something more like:
float $theVelocityX = `getAttr rigidBody1.velocityX`;
print ("velocityX is " + $theVelocityX + "\n");
float $theVelocityY = `getAttr rigidBody1.velocityY`;
print ("velocityY is " + $theVelocityY + "\n");
float $theVelocityZ = `getAttr rigidBody1.velocityZ`;
print ("velocityZ is " + $theVelocityZ + "\n");
10 October 2010, 04:25 PM
or something like:
vector $theVelocity = `getAttr rigidBody1.velocity`;
print ("velocity is " + $theVelocity + "\n");
print ("velocityX is " + ($theVelocity.x) + "\n");
print ("velocityY is " + ($theVelocity.y) + "\n");
print ("velocityZ is " + ($theVelocity.z) + "\n");
10 October 2010, 02:35 AM
yes indeed, that is better. Vector type, ahhh yes, I feel a bit smarter now (less dum).
What is the most efficient way to add the abs command to this? The only way I can get it to work is to add another variable and do:
vector $absVelocity = `abs $theVelocity`;
print ("the abs Velocity is " + $absVelocity +"\n");
additional question: I am getting values like '3.799048073e-15'. What is this notation called?
10 October 2010, 09:30 AM
yeah, abs (and all those other MEL math functions) is very strict about it's input type, and as `getAttr node.velocity` doesn't technically return a vector type abs bitches about it. the way you have already done it is fine although you could do something like this too:
float $vX = abs(`getAttr node.velocityX`);
float $vY = abs(`getAttr node.velocityY`);
float $vZ = abs(`getAttr node.velocityZ`);
but that's three variables instead of two or one. You could use the same variable if you don't want to create a new one.
vector $velocity = `getAttr node.velocity`;
$velocity = `abs $velocity`;
which should be fine too.
those e- numbers can be a real pain in the ass at times. They are simply a way of showing numbers that are very tiny (almost zero) but NOT zero, without resorting to too many decimal places ( 0.000000000000000000002018, etc..) but as far as I've ever seen they only come about due to rounding errors so are better off as zero! ;)
They are usually fine, although they do sometimes cause errors in your scripts depending on what you're doing with them. You just have to learn to think of them as effectively zero whenever you see them.
10 October 2010, 09:46 AM
Thanks Nathan!!! Great info.
10 October 2010, 09:46 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.