bpy.ops.screenshot() provoke error if python script is called as command line argument #51983

Closed
opened 2017-07-06 23:53:45 +02:00 by Another Tux User · 8 comments

System Information
Ubuntu 16.04, Intel HD Graphics 530

Blender Version
Broken: 2.78.c (from ppa)
Broken: 2.76 (from Ubuntu repos)

Short description
When Blender is launched from command line, with "--python" option (with ou without "--background" option), script is executed before UI is ready so ops like screenshot cannot be made while the same code works when running within Blender !

Exact steps to reproduce the error

Working:
1/ Start Blender (from shortcut, toolbar or Ubuntu dash)
2/ Load attached screenshot.blend (minimalistic file with python script embedded inside)
3/ Switch to "Scripting" screen layout
3/ Click "Run Script" : Warning is emitted but screenshot is generated and stored in your $HOME directory (since provided path is "." as second parameter of my own screenshot() call)
(didn't find the way to fix this last warning)

Traceback (most recent call last):
  File "/usr/share/blender/2.78/scripts/startup/bl_ui/space_text.py", line 32, in draw
    text = st.text
AttributeError: 'SpaceView3D' object has no attribute 'text'

location: /usr/share/blender/2.78/scripts/modules/bpy/ops.py:187

location: /usr/share/blender/2.78/scripts/modules/bpy/ops.py:187

Working:
1/ From your terminal, call

$ blender screenshot.blend

(see blend file attached)
2/ Switch to "Scripting" screen layout
3/ Click "Run Script" : same warning is emitted but screenshot is generated and stored in current directory

NOT working:
1/ From your terminal, call

$ blender --python screenshot.py

(see attached file, same code as the one embedded in the provided blend file)
2/ A screenshot is generated containing an empty window filled with default color (gray by default)

NOT working: (alternative with same result as previous one)
1/ From your terminal, call

$ blender screenshot.blend --python screenshot.py

2/ A screenshot is generated containing an empty window filled with default color

NOT working:
1/ From your terminal, call

$ blender --background --python screenshot.py

2/ Crash, no screenshot generated
(unable to find what is incorrect in the context, probably associated to the fact the UI is not ready when script is executed)

Traceback (most recent call last):
(...)
File "/usr/share/blender/2.78/scripts/modules/bpy/ops.py", line 187, in __call__
    ret = op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
RuntimeError: Operator bpy.ops.screen.screenshot.poll() failed, context is incorrect

NOT working : (by the way)
If you uncomment the code trying to find a workaround playing with applications handlers (at bottom of provided file or inside Text editor), you'll find that load_pre or load_post are not raised when blender is called with a script from command line !

Thanks for your work. Blender is great, I'm trying to use it as an alternative to limited solution like OpenScad. I would like to be able to customize the UI to have a multiaxis view and make screenshots of the full window (or some areas depending on scenario) to "debug" 3D printed objects, being able (example) to "see" / check (step by step + different view angles) if the selected edge loop is the good one before extrusion and by "selection" I mean the edges highligthed (selected) before manipulation (so rendering and saving it is not a valid alternative). Not to mention the fact that fixing this bug can help a lot to automatize screen capture for tutorials !

screenshot.blend screenshot.py

**System Information** Ubuntu 16.04, Intel HD Graphics 530 **Blender Version** Broken: 2.78.c (from ppa) Broken: 2.76 (from Ubuntu repos) **Short description** When Blender is launched from command line, with "--python" option (with ou without "--background" option), script is executed before UI is ready so ops like screenshot cannot be made while the same code works when running within Blender ! **Exact steps to reproduce the error** **Working**: 1/ Start Blender (from shortcut, toolbar or Ubuntu dash) 2/ Load attached screenshot.blend (minimalistic file with python script embedded inside) 3/ Switch to "Scripting" screen layout 3/ Click "Run Script" : **Warning is emitted but screenshot is generated** and stored *in your $HOME directory* (since provided path is "." as second parameter of my own screenshot() call) (didn't find the way to fix this last warning) ``` Traceback (most recent call last): File "/usr/share/blender/2.78/scripts/startup/bl_ui/space_text.py", line 32, in draw text = st.text AttributeError: 'SpaceView3D' object has no attribute 'text' location: /usr/share/blender/2.78/scripts/modules/bpy/ops.py:187 location: /usr/share/blender/2.78/scripts/modules/bpy/ops.py:187 ``` **Working**: 1/ From your terminal, call > $ blender screenshot.blend (see blend file attached) 2/ Switch to "Scripting" screen layout 3/ Click "Run Script" : **same warning** is emitted but **screenshot is generated** and *stored in current directory* **NOT working**: 1/ From your terminal, call > $ blender --python screenshot.py (see attached file, same code as the one embedded in the provided blend file) 2/ A screenshot is generated **containing an empty window filled with default color** (gray by default) **NOT working**: (alternative with same result as previous one) 1/ From your terminal, call > $ blender screenshot.blend --python screenshot.py 2/ A screenshot is generated containing an empty window filled with default color **NOT working**: 1/ From your terminal, call > $ blender --background --python screenshot.py 2/ **Crash, no screenshot generated** (unable to find what is incorrect in the context, probably associated to the fact the UI is not ready when script is executed) ``` Traceback (most recent call last): (...) File "/usr/share/blender/2.78/scripts/modules/bpy/ops.py", line 187, in __call__ ret = op_call(self.idname_py(), C_dict, kw, C_exec, C_undo) RuntimeError: Operator bpy.ops.screen.screenshot.poll() failed, context is incorrect ``` NOT working : (by the way) If you uncomment the code trying to find a workaround playing with applications handlers (at bottom of provided file or inside Text editor), you'll find that load_pre or load_post are not raised when blender is called with a script from command line ! Thanks for your work. Blender is great, I'm trying to use it as an alternative to limited solution like OpenScad. I would like to be able to customize the UI to have a multiaxis view and make screenshots of the full window (or some areas depending on scenario) to "debug" 3D printed objects, being able (example) to "see" / check (step by step + different view angles) if the selected edge loop is the good one before extrusion and by "selection" I mean the edges highligthed (selected) before manipulation (so rendering and saving it is not a valid alternative). Not to mention the fact that fixing this bug can help a lot to automatize screen capture for tutorials ! [screenshot.blend](https://archive.blender.org/developer/F654136/screenshot.blend) [screenshot.py](https://archive.blender.org/developer/F654137/screenshot.py)

Changed status to: 'Open'

Changed status to: 'Open'

Added subscriber: @ntuxer

Added subscriber: @ntuxer

Added subscriber: @Sergey

Added subscriber: @Sergey

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Sergey Sharybin self-assigned this 2017-07-07 09:48:57 +02:00

The warning is caused by script trying to trick the system and override the context, that context is likely in inconsistent state. When running screenshot from the interface you don't need to override any of the contexts.

All the scripts passed via command line argument are executed at command line processing time, prior to any OpenGL draw is done. The screenshot operator itself is just reading OpenGL buffer,m without doing any actual drawing (it can't draw anything since that'd be unsafe).

When running blender in background mode, there is no window created, which also means there is no OpenGL context. You can;t use screenshot in such configuration.

So thanks for the report, but it's just limitation which can not be easily bypassed.

The warning is caused by script trying to trick the system and override the context, that context is likely in inconsistent state. When running screenshot from the interface you don't need to override any of the contexts. All the scripts passed via command line argument are executed at command line processing time, prior to any OpenGL draw is done. The screenshot operator itself is just reading OpenGL buffer,m without doing any actual drawing (it can't draw anything since that'd be unsafe). When running blender in background mode, there is no window created, which also means there is no OpenGL context. You can;t use screenshot in such configuration. So thanks for the report, but it's just limitation which can not be easily bypassed.

Thanks for your answer but, forgetting "--background" option (1st and 2nd "NOT working" cases), why are these commands not being able to a take valid screenshot since the UI is launched and can be modified by my script as you can test if you uncomment corresponding lines (so the OpenGL buffer should be readable as you mentioned, since Blender is not started in background) ?

Thanks for your answer but, forgetting "--background" option (1st and 2nd "NOT working" cases), why are these commands not being able to a take valid screenshot **since the UI is launched and can be modified by my script** as you can test if you uncomment corresponding lines (so the OpenGL buffer should be readable as you mentioned, since Blender is not started in background) ?

As mentioned above, UI is launched but is not being drawn at the time when your script runs.

As mentioned above, UI is launched but is not being drawn at the time when your script runs.

Thanks for your precision

Thanks for your precision
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 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#51983
No description provided.