I am trying to convert a metaball object to a mesh, but I cant pass this stage. I am basically stuck. I checked out all the bpy.context and context override and bpy.ops posts and documents and i still can't make these 2 ops work :( I do not understand why they need special context.
Traceback (most recent call last):
File "C:\Users\USER\AppData\Roaming\Blender Foundation\Blender\2.80\scripts\addons\Dupper\__init__.py", line 95, in dupper
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
File "C:\BLENDER_28\2.80\scripts\modules\bpy\ops.py", line 200, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect
location: <unknown location>:-1
location: <unknown location>:-1
def makedup(obj):
object=obj
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = object
object.select_set(True)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
bpy.ops.object.convert(target='MESH')
class Dupper(bpy.types.Operator):
bl_idname = "object.dupper"
bl_label = "dupper"
@classmethod
def poll(cls, context):
return True
def execute(self, context):
bpy.ops.wm.open_mainfile(filepath=str(FILE))
object=bpy.data.objects["mball"]
makedup(object)
return {'FINISHED'}
bpy.ops.wm.open_mainfile(filepath=str(FILE))
Ask yourself do you really want to trash the file you are in by opening another with this operator.? Add aprint(context.copy())
before and after the operator to examine if the context is "refreshed" to be that of the newly opened file. Egcontext.scene
of file A from within the script is run does not automatically become thecontext.scene
of newly loaded file B... $\endgroup$bpy.ops.wm.open_mainfile(...)
The issue is the context directly after opening the file with the operator (like above). Did you examine the difference in context, pre and aft? Why not convert the objects in the blend you are in? (which your code will do if you remove the open mainfile operator) . Why the need to open another file to convert? Look into running in background mode where you can pass the file and the script via command line (or call in python with for examplesubprocess.Popen("blender -b foo.py -P convert_meta.py".split(" "))
. $\endgroup$