Movement tracking

(gowen) #1

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?

(theeth) #2

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.


(gowen) #3

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 =
    print mObject.LocX, oldx
    oldx = mObject.LocX

(eeshlo) #4

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.

(gowen) #5

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 = 
    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.

(RipSting) #6

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

    a += 1
    a = 1

print a

(theeth) #7

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

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

Blah(Ob) # do something with the variable