I'm new to scripting in Blender, but after extensive googling and searching through the API, I've created the following script to create an empty scene (my default scene has no cube, light or camera in it), add a Text object, set some various parameters, and then convert it to a mesh. However, when trying to convert to a mesh, I get:

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

The following is my script:

import bpy

font_loc = #set to font location

bpy.ops.wm.read_homefile()                       #load default (empty) scene
bpy.ops.object.text_add()                        #create the text object
mytext = bpy.data.objects["Text"]                
mytext.data.body = "A"                           #set some stuff up
mytext.data.align_x = "CENTER"
mytext.data.align_y = "BOTTOM"
mytext.data.extrude = 0.1
fnt = bpy.data.fonts.load(font_loc)              #font loading works
mytext.data.font = fnt
bpy.context.scene.objects.active = mytext        #make sure my Text object is correctly selected
mytext.select = True
bpy.ops.object.convert(target="MESH")            #<------ context is incorrect?

On that last line is when I get the error, but if I type that exact same line into the in-editor console after the script exits, it converts the Text object to a Mesh object perfectly fine. What's going on? Is there some other way to convert the Text object that I can't find?


bpy.ops.wm.read_homefile() - kills your current file and causes the trouble. If you could avoid it, it will work. I think you can import a scene from some blend file instead.
Look also this thread, and this.

  • $\begingroup$ Had no idea read_homefile ruins the context like this. Removed that and ran on an empty scene and it worked fine. Thanks for the heads-up; I'll try loading from a .blend as suggested. $\endgroup$ – selenaut Jan 7 '19 at 5:05
  • $\begingroup$ maybe even spacer's hack from the last comment of the first link (blinking a fullscreen) will do. $\endgroup$ – Mechanic Jan 7 '19 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.