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):


    bpy.context.view_layer.objects.active = object

    bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

class Dupper(bpy.types.Operator):
    bl_idname = "object.dupper"
    bl_label = "dupper"

    def poll(cls, context):
        return True

    def execute(self, context):




        return {'FINISHED'}
  • $\begingroup$ Recommend you remove 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 a print(context.copy()) before and after the operator to examine if the context is "refreshed" to be that of the newly opened file. Eg context.scene of file A from within the script is run does not automatically become the context.scene of newly loaded file B... $\endgroup$
    – batFINGER
    Mar 13, 2019 at 12:34
  • $\begingroup$ Thanks, I will try that. You recommend to remove open_mailfile, but how do I open a .blend file in a script that? Is it impossible to do? $\endgroup$
    – yarun can
    Mar 13, 2019 at 14:56
  • $\begingroup$ You can open a blend file with 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 example subprocess.Popen("blender -b foo.py -P convert_meta.py".split(" ")). $\endgroup$
    – batFINGER
    Mar 13, 2019 at 17:42
  • $\begingroup$ I am not around my desktop yet, I will try your options and write back here. Basically I am opening the file because I am modifying some options in that file and then I need to save it back as a different file. I do not understand your comment about converting? Do you mean saving objects as a .obj type thing? I will investigate the mode, I assume I can get the blender.exe path properly so I can pass it? In any case thanks for the recommendations $\endgroup$
    – yarun can
    Mar 13, 2019 at 18:24
  • $\begingroup$ Ok I tried running the subprocess method, it might work except that I cant make Blender to to load my local module (local to the script) :( $\endgroup$
    – yarun can
    Mar 14, 2019 at 5:23


You must log in to answer this question.

Browse other questions tagged .