Let's say I have an otherwise empty file with a single Text object, (imaginatively) named "Text". I want to convert this to a mesh using a Python script (obviously this is a ridiculously simplified example). I have the following code:
import bpy
object = bpy.data.objects['Text']
override = bpy.context.copy()
bpy.ops.object.convert(override, target='MESH')
If I select the Text object myself, then run this script, it works. If I select nothing and run it, it fails (RuntimeError: Operator bpy.ops.object.convert.poll() failed, context is incorrect).
Fine, that's because the context is incomplete. That's fine*, I get it, that's why I was ready with the override.
I inspected the difference between the context with the Text object selected (and script successful) and nothing selected (and script failing) using inspect.getmembers(bpy.context)
and found that there were only two differences: selected_objects
and selected_editable_objects
.
So, I added those to the override, giving me:
import bpy
object = bpy.data.objects['Text']
override = bpy.context.copy()
override['selected_objects'] = [object]
override['selected_editable_objects'] = [object]
bpy.ops.object.convert(override, target='MESH')
As far as I can work out, the context should now be functionally identical, and yet, the behaviour is unchanged. It works only if I select the Text object myself using the UI.
From poking around in the source code, it looks like it only cares about context.active_object
, which remained set in both cases - I can add that in too, of course, but to no avail. I have tried adding every combination of object
, active_object
, selected_objects
and selected_editable_objects
, and seen no difference in behaviour.
What am I missing?
* well, not fine, exactly - it is painful to attempt to puzzle out what parts of a context are required, given how generic that error message is.