Variable set to an object not updating

On 12/07/2014 at 18:07, xxxxxxxx wrote:

I tracked an annoying bug down to a simple example that, hopefully someone can help me with.

I'm setting a global variable to an object like this with a button:

gl_active_rig = doc.GetActiveObject()

Then, I physically move the object in the viewport.

Then I press another button that prints out the position by doing this:

print gl_active_rig.GetMg().off

Okay, it works fine.  It prints the correct location.

Now press undo, and the objects jumps back to it previous location.
Press the button to print the location again and it doesn't print the 'undone' location.
It is still printing the moved location.

If I click on the object in the viewport, and then press the button to display the location again, it prints the 'undone' location properly.

What is going on?

On 13/07/2014 at 05:57, xxxxxxxx wrote:

You still have the reference to the object in the undo-stack. Get it again with doc.GetActiveObject().


On 13/07/2014 at 10:09, xxxxxxxx wrote:

The problem is, in my actual plugin, it is no longer active.
That's why I'm storing it.

And since Objects in C4D can have identical names, if they are in different parental hierarchies, I can't search by name to find the object.

So how do I 'refresh' the reference?


On 18/07/2014 at 10:58, xxxxxxxx wrote:

Per-Anders, on another forum solved it, although he warned base containers are slow.

For others who don't know how to do this, here's what I did:

# At module level
gl_bc_active_rig = c4d.BaseContainer()    
# Then in a function to set it:
global gl_bc_active_rig
obj = ?  #whatever you want, a null perhaps.
gl_bc_active_rig.SetData(0, obj)
# Then in any other function when valid data is needed...and when isn't it?, call this function
def GetActiveRig() :
    global gl_bc_active_rig    
    doc = documents.GetActiveDocument() 
    return gl_bc_active_rig.GetLink(0, doc)

That solved the problem.