RuntimeError: Operator bpy.ops.wm.link.poll() failed, context is incorrect

Hey,

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 bpy.data.libraries.load(path) as (data_from, data_to):
    #print([i for i in dir(data_to)])
    for itm in data_from.collections :
        if itm.endswith('.anim'):
            #print(itm)
            scenes.append({"name": itm})
            
    collPath = os.path.join(path, 'Collection')
    print(scenes[0]["name"])
    bpy.ops.wm.link(directory=collPath, files=scenes)

And through __init__.py 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\__init__.py", line 124, in execute
    bpy.ops.wm.link(directory=collPath, files=scenes)
  File "C:\Program Files\Blender Foundation\Blender 2.82\2.82\scripts\modules\bpy\ops.py", line 201, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.wm.link.poll() failed, context is incorrect

location: <unknown location>:-1

Have an idea ?

Try using context override:

https://www.blender.org/forum/viewtopic.php?t=27834

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

Finally…bpy.ops.wm.link() 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 bpy.ops.wm.link() to link a collection.
So, i removed bpy.ops.wm.link() and just pass with bpy.data.libraries.load() with the link=True args.

eg :

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

# load my .blend file
with bpy.data.libraries.load(currentPath, 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
    if new_coll.name.endswith('.anim'):
        # find if collection.name endswith '.anim'
        # link collection
        instance = bpy.data.objects.new(new_coll.name, None)
        instance.instance_type = 'COLLECTION'
        instance.instance_collection = new_coll
        # link hard collection to blend file
        mainCollection.objects.link(instance)

wildly this last line mainCollection.objects.link (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