getting velocity from rigidbody


#1

hello everyone,
I am really new to scripting and having some trouble getting this expression to work. I want to query the current velocityX Y and Z of a rigidbody and use that value to drive their respective formatted text nodes and update them every frame. I found this in the docs:

When you select a rigid body, the Attribute Editor shows the current X, Y, and Z component values for Velocity, Spin, Force, and Torque. You can also query these attributes in MEL commands or expressions by using the attribute names in the following table.
Example runtime expression print (velocityX + "
"); This displays the contents of velocityX in the Script Editor each frame.

whatever I tried so far has not given me any connection. running the script gives me an error (// Error: print (rigidBody1.velocityX + "
"); //
// Error: Line 1.29: Invalid use of Maya object “rigidBody1.velocityX”. // )
after reading up on this for about 4 hours I am going to get some sleep and try again tomorrow! have a good day everyone :slight_smile:


#2

Hi,

Are you running that script in the Script Editor or the Expression Editor?

In the Expression Editor, you can reference object attributes directly, without placing the object name and attribute in a string. So this code (the same as you posted), should work fine in the Expression Editor.

print (rigidBody1.velocityX + "
");

In the Script Editor, you would need to reference the object and attribute as a string, like so:

print ("rigidBody1.velocityX" + "
");

Notice the subtle difference with the quotes.

Hope that helps. Good luck!


#3

hey mlefevre,

thanks a lot for your reply, that cleared some things up for me. In the meantime I did manage to get it to work. I used a regular sphere and made an expression there

sphere1.rotateX = rigidBody1.velocityX;
sphere1.rotateY = rigidBody1.velocityY;
sphere1.rotateZ = rigidBody1.velocityZ;

I was able to make a connection using the hypergraph connections and the connection editor to link the sphere rotations to the value0 of the formatted text node and everything just worked :slight_smile:

I do have a question regarding framecache nodes though, I read about them somewhere and it seems you could also use those to transfer that information without using an expression? My scene does get quite slow now