1
$\begingroup$

For a project involving 3D printing of some labels with varying text, I want to use a small script. I checked all commands interactively and piled them up. However the script fails while the interactive session doing this with mouse and keyboard succeeds (repeatedly).

I tried to minimize the problem, removed everything not strictly needed, to pinpoint the place where I stumbled, but I cannot put my finger on the right spot. It fails when converting the text object into a regular mesh, complaining on the context not being right.

What am I doing wrong?

The script contains:

### (re-)start Blender, I used version 2.77a
### - open the scripting window in the top menu bar
### - load this file into the text window
### - run it with the "Run Script" button
import bpy
status = bpy.ops.object.text_add(radius = 1.0)
print("status after object creation is: ", status)
myobject = bpy.context.active_object
print("myobject is: ", myobject)
myobject.data.body = "Hello World !"
status = bpy.ops.object.select_all(action='DESELECT')
print("status after deselect all is: ", status)
status = bpy.context.scene.update()                     # forces update
print("status after update is: ", status)
bpy.context.scene.objects.active = myobject
status = bpy.context.object.type
print(" bpy.context.object.type is: ", status)
status = bpy.ops.object.convert(target='MESH')          # fatal error
print("status after converting to a mesh is: ", status) # never reached

The console shows:

Read new prefs: /Users/wim/Library/Application Support/Blender/2.77/config/userpref.blend
found bundled python: /Applications/Utilities-3rd-party/blender.app/Contents/MacOS/../Resources/2.77/python
measureit: added to phytonpath
measureit: Imported multifiles
status after object creation is:  {'FINISHED'}
myobject is:  <bpy_struct, Object("Text")>
status after deselect all is:  {'FINISHED'}
status after update is:  None
 bpy.context.object.type is:  FONT
Traceback (most recent call last):
  File "/test-convert-mesh.py", line 18, in <module>
  File "/Applications/Utilities-3rd-party/blender.app/Contents/MacOS/../Resources/2.77/scripts/modules/bpy/ops.py", line 189, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.convert.poll() failed, context is incorrect
Error: Python script fail, look in the console for now...
$\endgroup$
2

1 Answer 1

1
$\begingroup$

As well as making the text object the context object you need to reselect it. (You did call deselect after all). An object can be context.active_object while not context.active_object.select Some operators that work on all objects selected may only go thru the context.selected_objects list.

bpy.context.scene.objects.active = myobject
myobject.select = True
status = bpy.context.object.type
print(" bpy.context.object.type is: ", status)
status = bpy.ops.object.convert(target='MESH')          # fatal error

print("status after converting to a mesh is: ", status)
$\endgroup$
1
  • $\begingroup$ Marvellous, I tested the select flag, but apparantly not in combination with setting it active in the scene as well. This solves it! Many thanks! $\endgroup$
    – Wim
    Jun 16, 2016 at 15:01

You must log in to answer this question.

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