When switching scenes using "-S" argument in command line, scripts called by "--python-text" do not get correct scene context #55991

Closed
opened 2018-07-16 07:40:18 +02:00 by Sam Brubaker · 6 comments

Version: 2.79b (linux 64)

active_scene_api.blend

How to reproduce:

  • Open the file active_scene_api.blend and inspect the embedded python script.
  • Close blender.
  • Open the same file from the command line and execute the script like so: blender -b active_scene_api.blend --python-text "active_scene.py"
  • Observe the output from the script.
  • Now do the same, but change the scene using the scene argument: blender -b active_scene_api.blend -S "Scene B" --python-text "active_scene.py"
  • Note how it does not print the current active scene name, but instead prints whatever the active scene was when the file was saved.

This can cause errors and unexpected behavior with render scripts.

Version: 2.79b (linux 64) [active_scene_api.blend](https://archive.blender.org/developer/F3954545/active_scene_api.blend) **How to reproduce:** - Open the file active_scene_api.blend and inspect the embedded python script. - Close blender. - Open the same file from the command line and execute the script like so: `blender -b active_scene_api.blend --python-text "active_scene.py"` - Observe the output from the script. - Now do the same, but change the scene using the scene argument: `blender -b active_scene_api.blend -S "Scene B" --python-text "active_scene.py"` - Note how it does not print the current active scene name, but instead prints whatever the active scene was when the file was saved. This can cause errors and unexpected behavior with render scripts.
Author

Added subscriber: @rocketman

Added subscriber: @rocketman

Added subscribers: @ideasman42, @mont29

Added subscribers: @ideasman42, @mont29
Campbell Barton was assigned by Bastien Montagne 2018-07-16 17:52:43 +02:00

Took me some time to understand what is happening here… thing is, we do some backup/sanitizing of context before executing py scripts, and among other things, arg_py_context_backup calls CTX_wm_window_set, which sets again context's Scene from active windows' screen scene…

@ideasman42 think you are familiar with this area of the code, not sure what is the best/correct solution here. :/

Took me some time to understand what is happening here… thing is, we do some backup/sanitizing of context before executing py scripts, and among other things, `arg_py_context_backup` calls `CTX_wm_window_set`, which sets again context's Scene from active windows' screen scene… @ideasman42 think you are familiar with this area of the code, not sure what is the best/correct solution here. :/
Author

Thank you @mont29 . I did discover a workaround which was to wrap the script in a function that is appended to the app handlers. Consider the following script:

import bpy

def print_scene(dummy):
    scene = bpy.context.scene
    print("The active scene is " + scene.name + "... or is it?")
    
bpy.app.handlers.render_init.append(print_scene)

When called from bpy.app.handlers. render_init, the context is correct. Perhaps this is a better way to do it anyway, but as a matter of principle it still seems better to make sure that the context is consistent, and called from something other than the active window screen screen, since the screen isn't always there!

Thank you @mont29 . I did discover a workaround which was to wrap the script in a function that is appended to the app handlers. Consider the following script: ``` import bpy def print_scene(dummy): scene = bpy.context.scene print("The active scene is " + scene.name + "... or is it?") bpy.app.handlers.render_init.append(print_scene) ``` When called from bpy.app.handlers. render_init, the context is correct. Perhaps this is a better way to do it anyway, but as a matter of principle it still seems better to make sure that the context is consistent, and called from something other than the active window screen screen, since the screen isn't always there!

This issue was referenced by blender/blender@3e2dfc6db8

This issue was referenced by blender/blender@3e2dfc6db8e01ffa5e12a8e9b300ef6509a06552

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Sign in to join this conversation.
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#55991
No description provided.