# poll failed - context is incorrect

## 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 = {
"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

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_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

# create an object and change its mesh in EDIT mode : I have an error message...
my_plane = bpy.context.scene.my_plane
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

• can you post the code? then we can try and replicate the issue. – rob Jul 31 at 9:50
• If you want to be in a certain mode I'd recommend testing context.mode and using bpy.ops.object.mode_set(mode='EDIT') As mentioned posting code, and how and where you are running it will give more context to q. – batFINGER Jul 31 at 10:20
• Would strongly reconsider the use of read homefile in an operator like above. The context gets trashed after the call. Would trash anything in the file too, doubt there would be any undo available.... – batFINGER Aug 2 at 11:11
• Actually, I did reconsider it now as I found out that I would have this issue... The goal I have is to generate several scene from a set of file. Thus I need to initialize the scene before creating a new one (or delete everything). I am still wondering if there is a nice way to do it ! – Mathou Aug 2 at 11:14
• The elephant in the room is 2.8. eg see the write-up re templates – batFINGER Aug 2 at 14:36