Movement tracking

I’ve got a python script linked to a blender object. Is there a way to determine how far the object has moved from one frame to the next.

At the moment I’m measuring the distance from a fixed datum, but that only works as long as the object is moving in a straight line away from the datum.

I suppose I could place an empty aligned with the object, then move to the next frame and measure the distance back to the empty. I just need to make sure that the empty is positioned on the object for frame 1, using on onLoad script perhaps.

Is there a better way?

global variables are persistent in Blender, so they will keep their value from each time you run the script.

I hope that can help you.

Martin

How do I define a global variable then? It’s one of those things thats so simple it’s not written down anywhere.

For this test code:


import Blender

global oldx  ??????????????

if (Blender.bylink) :
    mObject = Blender.link
    print mObject.LocX, oldx
    oldx = mObject.LocX

Any variable defined outside of a function/class is global, no need to use any keywords, HOWEVER, this is not true anymore for Blender versions >223, so it will not work for Publisher. Variables only exist for as long as the script is running. For 223 and lower, try this:


a =123
print a

run it, then put a comment in front of a=123, and run again, the variable contents will still be printed.

Thanks for your help, I’ve now got a solution that will work most of the time in 223.

Basically, I test the frame number and ignore the operation (print in this case) if this is the first of the animation. This prevents the undefined variable from being referenced and causing an error.


if (Blender.bylink) : 
    mObject = Blender.link 
    if (Blender.Get("curframe") != 1) :
        print mObject.LocX, oldx 
    
    oldx = mObject.LocX 

As long as I save the animation on frame 1, I should not get any problems.

You could also use this method, which relies on error trapping if the variable doesn’t exist yet:

try:
    a += 1
except:
    a = 1

print a

or you could use this, which works will all type of variables, not just numbers:


try:
        Ob
except NameError:
        Ob = Blender.Object.GetSelected() # initialization

Blah(Ob) # do something with the variable


Martin