Goal
Generate several scene from files (csv, yml with parameters) and thus, initialize the scene before loading a file.
Bug
In my code, I am creating a plane(object mode) and want to subdivide it (edit mode). Yet I have this error message:
RuntimeError: Operator bpy.ops.object.editmode_toggle.poll() failed, context is incorrect
can anyone has a solution?
Info: before toggle to edit mode, I select and activate the plane and on my Blender GUI, the plane seems to be in Edit mode (with the vertices visible) yet I am still in object mode 😕
UPDATE
It seems that it come from the bpy.ops.wm.read_homefile()..
@rob: I made a little addons that you can place in a directory "test_addons" in the scripts/addons. This show how you cannot (?) use
bpy.ops.wm.read_homefile()
with a toggle_mode right after...
__ init__.py
bl_info = {
"name": "Test Addons",
"author": "",
"location": "View3D > Tools > Test_addons",
"version": (1, 0, 0),
"blender": (2, 7, 9),
"description": " ",
"wiki_url": "",
"category": "Development"
}
if "bpy" in locals():
import importlib
importlib.reload(ui)
importlib.reload(new_plane)
else:
import bpy
from bpy.types import PropertyGroup
from bpy.props import (PointerProperty, FloatProperty)
from . import ui
from . import new_plane
class MyPlane(PropertyGroup):
size = FloatProperty(name="Size", default=5., min=0, max=100,)
classes = (ui.NewToolBar, new_plane.NewPlane, MyPlane)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.my_plane = PointerProperty(type=MyPlane)
ui.py
import bpy
from bpy.types import Panel
class NewToolBar(Panel):
bl_category = "test_addons"
bl_label = "test_addons"
bl_region_type = "TOOLS"
bl_space_type = "VIEW_3D"
bl_context = "objectmode"
def draw(self, context):
self.layout.operator("my_plane.new", text="New Plane")
new_plane.py
import bpy
from bpy.types import Operator
class NewPlane(Operator):
bl_idname = "my_plane.new"
bl_label = "My Plane"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
# I load the home file
bpy.ops.wm.read_homefile()
# create an object and change its mesh in EDIT mode : I have an error message...
my_plane = bpy.context.scene.my_plane
bpy.ops.mesh.primitive_plane_add(radius=my_plane.size, location=(0., 0., 0.))
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.subdivide()
bpy.ops.object.mode_set(mode='OBJECT')
return {'FINISHED'}
Error message
bpy.ops.object.mode_set(mode='EDIT')
File "C:\Blender\2.79\scripts\modules\bpy\ops.py", line 189, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect
context.mode
and usingbpy.ops.object.mode_set(mode='EDIT')
As mentioned posting code, and how and where you are running it will give more context to q. $\endgroup$