RuntimeError on bpy.ops.object.mode_set if executed from terminal but works in blender's text editor #92464

Closed
opened 2021-10-25 06:35:43 +02:00 by Jonathan Mafi · 21 comments

System Information
Operating system: Linux-5.4.0-84-generic-x86_64-with-glibc2.31 64 Bits
Graphics card: NVIDIA GeForce GTX 780/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 470.63.01

Blender Version
Broken: version: 2.93.2, branch: master, commit date: 2021-08-03 05:58, hash: 1eb06de260

Short description of error
When changing armature mode from python script executed via terminal it errors on the mode_set method

RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect

NOTE: this isn't a problem if the python file is run from the text editor within blender.

Exact steps for others to reproduce the error

  1. Create a python module test.py
  2. After standard import of bpy and open_mainfile method to open a file with basic rig. Add these two lines.
bpy.data.objects['MyRig'].select_set(True) # Where "MyRig" is the name of rig in file.
bpy.ops.object.mode_set(mode='EDIT')  # fails on this line

  1. Launch python via commandline:
 "directory/to/blender-2.93.2-linux-x64/blender" -P "test.py"

MyRig_Test.blend

**System Information** Operating system: Linux-5.4.0-84-generic-x86_64-with-glibc2.31 64 Bits Graphics card: NVIDIA GeForce GTX 780/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 470.63.01 **Blender Version** Broken: version: 2.93.2, branch: master, commit date: 2021-08-03 05:58, hash: `1eb06de260` **Short description of error** When changing armature mode from python script executed via terminal it errors on the **mode_set** method > RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect **NOTE:** this isn't a problem if the python file is run from the text editor within blender. **Exact steps for others to reproduce the error** 1. Create a python module test.py 2. After standard import of bpy and open_mainfile method to open a file with basic rig. Add these two lines. ``` bpy.data.objects['MyRig'].select_set(True) # Where "MyRig" is the name of rig in file. bpy.ops.object.mode_set(mode='EDIT') # fails on this line ``` 3. Launch python via commandline: ``` "directory/to/blender-2.93.2-linux-x64/blender" -P "test.py" ``` [MyRig_Test.blend](https://archive.blender.org/developer/F11481344/MyRig_Test.blend)
Author

Added subscriber: @jmafi3d

Added subscriber: @jmafi3d

#100446 was marked as duplicate of this issue

#100446 was marked as duplicate of this issue
Jonathan Mafi changed title from Error on bpy.ops.object.mode_set if blender python executed from terminal to RuntimeError on bpy.ops.object.mode_set if blender python executed from terminal 2021-10-25 12:09:35 +02:00
Jonathan Mafi changed title from RuntimeError on bpy.ops.object.mode_set if blender python executed from terminal to RuntimeError on bpy.ops.object.mode_set if blender python executed from terminal but works in blender text editor 2021-10-25 12:09:55 +02:00
Jonathan Mafi changed title from RuntimeError on bpy.ops.object.mode_set if blender python executed from terminal but works in blender text editor to RuntimeError on bpy.ops.object.mode_set if executed from terminal but works in blender's text editor 2021-10-25 12:10:18 +02:00

Added subscriber: @iss

Added subscriber: @iss

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

In 3.0 I get error RuntimeError: Operator bpy.ops.object.mode_set.poll() Context missing active object, but can confirm the issue.

In 3.0 I get error `RuntimeError: Operator bpy.ops.object.mode_set.poll() Context missing active object`, but can confirm the issue.
Author

In relation to the missing active object. I should note that i tried varoius other methods explicitly setting the active object first, also used toggle method as wel, but still had the same approximate context issue.

In relation to the missing active object. I should note that i tried varoius other methods explicitly setting the active object first, also used toggle method as wel, but still had the same approximate context issue.

Added subscriber: @BorutBalazek

Added subscriber: @BorutBalazek

Hey Jonathan, did you find a workaround for this?

I've been experiencing the same issue on 2.93.0.

Hey Jonathan, did you find a workaround for this? I've been experiencing the same issue on 2.93.0.
Author

No I have not. Keen to look into it from the source code side but cant get blender build working XD.

No I have not. Keen to look into it from the source code side but cant get blender build working XD.
Author

Hey @BorutBalazek

I found a workaround.... It helps in my case but wont help with others if they require file changing during the process.

Remove the file open from within the python code and put it direct into the shell process
../blender ../MyRig_Test.blend -P .../test.py
Cant believe i didnt think of it at the start...
But as mentioned not useful for pipelines that require more dynamic changing of files via python. So still a bug.

Opening the blend file via shell must sort the context out in advance somehow that python open_mainfile doesnt?

Hope that helps sort troubleshoot. I've got blender building from source now so if i have some time ill jump in and have a look around. If someone can tell me where i should be looking specifically it would help :D even some rough documentation.

Hey @BorutBalazek I found a workaround.... It helps in my case but wont help with others if they require file changing during the process. Remove the file open from within the python code and put it direct into the shell process ../blender ../MyRig_Test.blend -P .../test.py Cant believe i didnt think of it at the start... But as mentioned not useful for pipelines that require more dynamic changing of files via python. So still a bug. Opening the blend file via shell must sort the context out in advance somehow that python open_mainfile doesnt? Hope that helps sort troubleshoot. I've got blender building from source now so if i have some time ill jump in and have a look around. If someone can tell me where i should be looking specifically it would help :D even some rough documentation.

Hey @jmafi3d,

I didn't even fully check your initial description, but I actually do open the file as a part of the CLI command, but still experience the same issue (in addition I also run it in --background mode). For me, it basically always breaks if I trigger any of the following commands: bpy.ops.object.mode_set(), bpy.ops.mesh.select_all() and bpy.ops.uv.smart_project().

Thanks anyway! Hopefully, this gets fixed soon!

Hey @jmafi3d, I didn't even fully check your initial description, but I actually do open the file as a part of the CLI command, but still experience the same issue (in addition I also run it in --background mode). For me, it basically always breaks if I trigger any of the following commands: `bpy.ops.object.mode_set()`, `bpy.ops.mesh.select_all()` and `bpy.ops.uv.smart_project()`. Thanks anyway! Hopefully, this gets fixed soon!

This issue was referenced by 9bd97e62ad

This issue was referenced by 9bd97e62ade417f6b4025acbad46802c3e7e5683

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Campbell Barton self-assigned this 2021-11-02 12:03:39 +01:00

This issue was referenced by 50b0503a11

This issue was referenced by 50b0503a110b8159fe7a30645d7a2ebbf518860f

Changed status from 'Resolved' to: 'Confirmed'

Changed status from 'Resolved' to: 'Confirmed'

The fixed caused #92818, reverted & re-opening.

The fixed caused #92818, reverted & re-opening.

Hey @ideasman42, I'm not really familiar with the blender source code, but could you think of any workaround for this?

Specifically, in my case, I want to smart unwrap (if no UV map is not present), bake the materials into textures and then export them (the export works, but doesn't help a lot). Neither of those steps really work, because I guess bpy.ops.object.bake() also relies on the same mechanism.

So I was just thinking if it's still possible to do it somehow in a hacky way?

Hey @ideasman42, I'm not really familiar with the blender source code, but could you think of any workaround for this? Specifically, in my case, I want to smart unwrap (if no UV map is not present), bake the materials into textures and then export them (the export works, but doesn't help a lot). Neither of those steps really work, because I guess `bpy.ops.object.bake()` also relies on the same mechanism. So I was just thinking if it's still possible to do it somehow in a hacky way?

Hi @BorutBalazek, I'm proposing D13126 as an alternative solution.

Hi @BorutBalazek, I'm proposing [D13126](https://archive.blender.org/developer/D13126) as an alternative solution.

D13126 has been committed. Closing.

[D13126](https://archive.blender.org/developer/D13126) has been committed. Closing.

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Member

Added subscriber: @maumemoli

Added subscriber: @maumemoli
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
6 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#92464
No description provided.