1
$\begingroup$

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?

$\endgroup$

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
2
  • $\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
    Commented Jan 7, 2019 at 5:05
  • $\begingroup$ maybe even spacer's hack from the last comment of the first link (blinking a fullscreen) will do. $\endgroup$
    – Mechanic
    Commented Jan 7, 2019 at 5:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .