RuntimeError: Operator failed, context is incorrect


It drives me crazy…I run a script in ‘scripting’ area in blender it work.
But through a panel it returns a context error ?

here an example :

Working on blender scripting area

import bpy
import os

path = 'path/of/myFile.blend'

scenes = []

with as (data_from, data_to):
    #print([i for i in dir(data_to)])
    for itm in data_from.collections :
        if itm.endswith('.anim'):
            scenes.append({"name": itm})
    collPath = os.path.join(path, 'Collection')
    print(scenes[0]["name"]), files=scenes)

And through placed on addons folder :
Not working

class simpleClass(bpy.types.Operator):
    bl_idname = "myPanel.simple_class"
    bl_label = "my simple script"
    bl_description = "simple script"

    def execute(self, context):
        ... my script above
        return {'FINISHED'}

...register stuff

Error :

location: <unknown location>:-1
Error: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender 2.82\2.82\scripts\addons\MyAddon\", line 124, in execute, files=scenes)
  File "C:\Program Files\Blender Foundation\Blender 2.82\2.82\scripts\modules\bpy\", line 201, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator failed, context is incorrect

location: <unknown location>:-1

Have an idea ?

Try using context override:

Thx @Cirno,
I would like to avoid going through overrides in order to understand why it does not work.

Finally… works with batch, but not with scripting area and not with operator through panel.
Like @Cirno suggest is to override context.

I’m learning to blender at the same time so I don’t know him well. But from what I was explained there is not necessarily need to go through a to link a collection.
So, i removed and just pass with with the link=True args.

eg :

collection = "COL_ORIG"  # collection target name allready setup in my import scene
mainCollection =[collection] #collection data

# load my .blend file
with, link=True) as (data_from, data_to):
        data_to.collections = data_from.collections # collect collections
for new_coll in data_to.collections:
    # list all collections
        # find if endswith '.anim'
        # link collection
        instance =, None)
        instance.instance_type = 'COLLECTION'
        instance.instance_collection = new_coll
        # link hard collection to blend file

wildly this last line (instance)
this link is not linked to the import of the file itself…But the link of the object in the collection.

Coming from other 3D software, I didn’t understand it like that :wink:

So…Like @Cirno said…i’ve no choice to go through override. grrr