Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difficulty reading updated scene information in a script #1250

Closed
sqward opened this issue Dec 6, 2019 · 37 comments
Closed

Difficulty reading updated scene information in a script #1250

sqward opened this issue Dec 6, 2019 · 37 comments

Comments

@sqward
Copy link

sqward commented Dec 6, 2019

I have an export script which I execute in Blender 2.81 in headless mode (on commandline). It's meant to export and animation I've created using Animation Nodes. My script worked fine in 2.79. In 2.81 my object animation is not updated: for every scene.frame_set() position, rotatino etc. is the same.

Can you help me get to the bottom of the problem? I must be missing something that was introduced in Blender 2.8?

Regards

@OmarEmaraDev
Copy link
Collaborator

This seems like the issue reported in #1171, is this the issue you are having?

@sqward
Copy link
Author

sqward commented Dec 6, 2019

I've tried to render my animation and it seems to behave as expected.

@OmarEmaraDev
Copy link
Collaborator

So the issue only occur when you are rendering from the command line?

@sqward
Copy link
Author

sqward commented Dec 6, 2019

I'm not even rendering. My aim is to export animation data. So for every frame I captura object transform and store it. But I don't render anything.

@OmarEmaraDev
Copy link
Collaborator

So you have a node tree that sets the transformation of the object, and then you run a script that access the transformation of the object?

@sqward
Copy link
Author

sqward commented Dec 6, 2019 via email

@OmarEmaraDev
Copy link
Collaborator

Can you share a minimal file that reproduces the issue?

@sqward
Copy link
Author

sqward commented Dec 6, 2019 via email

@sqward
Copy link
Author

sqward commented Dec 10, 2019

Hi and sorry for delay.

My toy export script looks like this:

import bpy

def printObjectPosition(obj):
    print(obj.name, obj.matrix_world.to_translation())

def convertAnimation(scene):
    objs = scene.objects
    scene.frame_set(scene.frame_start)
    printObjectPosition(objs["Plane"])   
    printObjectPosition(objs["Plane.001"])   
    scene.frame_set(scene.frame_start+100)
    printObjectPosition(objs["Plane"])   
    printObjectPosition(objs["Plane.001"])   
  
convertAnimation(bpy.data.scenes['Scene'])

and blender invocation looks like this:

$ ../sv2017/src/Tools/blender-2.81a-windows64/blender.exe -noaudio -y --background an_test.blend -P export_example.py
Blender 2.81 (sub 16) (hash f1aa4d18d49d built 2019-12-04 14:30:40)
Read prefs: C:\Users\sqward\AppData\Roaming\Blender Foundation\Blender\2.81\config\userpref.blend
found bundled python: C:\Work\devel\sv2017\src\Tools\blender-2.81a-windows64\2.81\python
Read blend: C:\Work\devel\animation_nodes_export\an_test.blend
Registered Animation Nodes
Plane <Vector (-0.5003, 1.7544, 0.0000)>
Plane.001 <Vector (4.2274, 0.1166, 0.0000)>
Plane <Vector (-0.5003, 1.7544, 0.0000)>
Plane.001 <Vector (0.5983, -0.7396, 0.0000)>

My attached blend file contains two objects: "Plane" which is driven by AN and "Plane.001" which moved via key frames.

Note how position of "Plane.001" changes between scene.frame_set(scene.frame_start) and scene.frame_set(scene.frame_start+100) but not for "Plane".

This worked fine in 2.7x. Am I missing a trick in 2.8x?

an_test.zip

@OmarEmaraDev
Copy link
Collaborator

OmarEmaraDev commented Dec 11, 2019

Animation Nodes needs to execute when you change the current frame in order for the object location to be updated. Animation Nodes can only see such change in the current frame after the script finish execution, so Auto Execution is not an option. To fix this, you just need to execute the node tree yourself when needed. Your script should be something like:

import bpy

def printObjectPosition(obj):
    print(obj.name, obj.matrix_world.to_translation())

def convertAnimation(scene):
    objs = scene.objects
    scene.frame_set(scene.frame_start)
    printObjectPosition(objs["Plane"])   
    printObjectPosition(objs["Plane.001"])   
    scene.frame_set(scene.frame_start+100)
    bpy.data.node_groups['NodeTree'].execute() # Execute node tree after changing frame.
    printObjectPosition(objs["Plane"])   
    printObjectPosition(objs["Plane.001"])   
  
convertAnimation(bpy.data.scenes['Scene'])

Makes sense?

@sqward
Copy link
Author

sqward commented Dec 11, 2019 via email

@OmarEmaraDev
Copy link
Collaborator

Blender 2.8 removed the ALWAYS event handler we used in 2.7x to implement Auto Execution, so now we use an infinite timer. I don't know exactly why this happens, but that must be the cause of this.

@sqward
Copy link
Author

sqward commented Dec 11, 2019 via email

@OmarEmaraDev
Copy link
Collaborator

If you enable Auto Execution, Animation Nodes is always executing as many times as possible, even if the Always option is disabled. Animation Nodes does not use a proper trigger system for auto execution. For instance, when you enable Frame Changed, in the background, the frame value is retrieved and stored as a string, then it is compared to the previously stored value, if Animation Nodes notices that the value changed, it then executes the node tree.

To summarize, if you disable the Always option, Animation Nodes is still always executing an internal trigger mechanism.

@sqward
Copy link
Author

sqward commented Dec 11, 2019 via email

@sqward sqward closed this as completed Dec 11, 2019
@sqward sqward reopened this Dec 11, 2019
@sqward
Copy link
Author

sqward commented Dec 11, 2019

bpy.data.node_groups['NodeTree'].execute() doesn't seem to help. Position of "Plane" object is not updated. Did it work for you? Regards

@OmarEmaraDev
Copy link
Collaborator

Yes, it works with the blend file you sent.

@sqward
Copy link
Author

sqward commented Dec 11, 2019

Position of "Plane" stays the same after calling execute. Could that be platform difference?

$ ../sv2017/src/Tools/blender-2.81a-windows64/blender.exe -noaudio -y --background an_test.blend -P export_example.py
Blender 2.81 (sub 16) (hash f1aa4d18d49d built 2019-12-04 14:30:40)
Read prefs: C:\Users\sqward\AppData\Roaming\Blender Foundation\Blender\2.81\config\userpref.blend
found bundled python: C:\Work\devel\sv2017\src\Tools\blender-2.81a-windows64\2.81\python
Read blend: C:\Work\devel\animation_nodes_export\an_test.blend
Registered Animation Nodes
Plane <Vector (0.3774, 1.7612, 0.0000)>
Plane.001 <Vector (4.2274, 0.1166, 0.0000)>
Plane <Vector (0.3774, 1.7612, 0.0000)>
Plane.001 <Vector (0.5983, -0.7396, 0.0000)>

@sqward
Copy link
Author

sqward commented Dec 11, 2019

It doesn't work for me from within Blender either:
image

@OmarEmaraDev
Copy link
Collaborator

OmarEmaraDev commented Dec 12, 2019

Please note that Blender doesn't update matrices by default when they are changed, so you have to update them yourself. You can use the Update Matrices node for that.

20191212-103129

But preferably, you should update them before reading them in the script.

@sqward
Copy link
Author

sqward commented Dec 12, 2019

It seems to work for me now from within Blender, but not when executed from the commandline.
Any idea?

@OmarEmaraDev
Copy link
Collaborator

hmmmm, no. Can you update the matrices from the script itself. See:
https://stackoverflow.com/questions/13840418/force-matrix-world-to-be-recalculated-in-blender

Otherwise, I would consider this a bug as in #1171.

@sqward
Copy link
Author

sqward commented Dec 12, 2019

I see my object.location doesn't change so world_matrix would be the same.

@OmarEmaraDev
Copy link
Collaborator

In this case, I would need to investigate this further. Might take some time, sorry.

@OmarEmaraDev
Copy link
Collaborator

Fixed in #1294 and b1ac938.

@sqward
Copy link
Author

sqward commented Feb 17, 2020

Thanks! I tried to find a relevant build but had no luck. Is that available?

@OmarEmaraDev
Copy link
Collaborator

Should be available in https://animation-nodes.com/

@sqward
Copy link
Author

sqward commented Feb 17, 2020

Thanks. In that case the might be something wrong with the build:
image

@OmarEmaraDev
Copy link
Collaborator

See #1240.

@sqward
Copy link
Author

sqward commented Feb 17, 2020

Thanks. I can run it now.

Sadly, the above toy script still doesn't work. I've tried all your earlier suggestions (except bpy.data.node_groups['NodeTree'].execute() which doesn't seem to work at all).

@OmarEmaraDev
Copy link
Collaborator

hmm, let me test this again.

@OmarEmaraDev
Copy link
Collaborator

OmarEmaraDev commented Feb 18, 2020

It seems to be working fine for me:

┌─[omar]──[~/Desktop]
└─╼ blender -noaudio -y --background an_test.blend -P export_example.py
Blender 2.83 (sub 4) (hash dcb905a58f59 built 2020-02-17 14:36:20)
Read prefs: /home/omar/.config/blender/2.83/config/userpref.blend
/run/user/1000/gvfs/ non-existent directory
Registered Animation Nodes
Read blend: /home/omar/Desktop/an_test.blend
Plane <Vector (0.3774, 1.7612, 0.0000)>
Plane.001 <Vector (4.2274, 0.1166, 0.0000)>
Plane <Vector (-2.3628, -1.3816, 0.0000)>
Plane.001 <Vector (0.5983, -0.7396, 0.0000)>

Blender quit

This was using your original toy script:

import bpy

def printObjectPosition(obj):
    print(obj.name, obj.matrix_world.to_translation())

def convertAnimation(scene):
    objs = scene.objects
    scene.frame_set(scene.frame_start)
    printObjectPosition(objs["Plane"])   
    printObjectPosition(objs["Plane.001"])   
    scene.frame_set(scene.frame_start+100)
    printObjectPosition(objs["Plane"])   
    printObjectPosition(objs["Plane.001"])   
  
convertAnimation(bpy.data.scenes['Scene'])

We now execute automatically on frame_set, so you don't have to execute manually.

Can you test with 2.83? Just so that we have the same setup and can debug this better.

@sqward
Copy link
Author

sqward commented Feb 18, 2020

Ok, it seem to work on 2.83... But I had to remove the "Update Object Matrices" node. Is this what you'd expect? Regards

@OmarEmaraDev
Copy link
Collaborator

Not exactly. Does it fail with the Update Object Matrices node there?

@sqward
Copy link
Author

sqward commented Feb 18, 2020

Yes, It fails to update the positions.

@OmarEmaraDev
Copy link
Collaborator

I am not entirely sure why this is the case. It could be that the depsgraph at this point is not up to date. But look at the bright side, it works. 😀

@sqward
Copy link
Author

sqward commented Feb 18, 2020

Indeed. Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants