How to support libraries in RenderEngine?

For scenes with objects linked from other .blend files everything works out of the box with e.g. workbench, but when I toggle my custom RenderEngine all linked objects are missing.

If I add the following code into view_update():

for l in context.blend_data.libraries:
    src_path = l.filepath
    with bpy.data.libraries.load(src_path) as (data_from, data_to):
        data_to.objects = data_from.objects

    for obj in data_to.objects:
    try:
        bpy.context.scene.collection.objects.link(obj)
    except:
        pass

then linked objects reappear in the 2nd frame but they are now added into the Scene Collection visible to user which is not how it works with workbench. How to make RenderEngine handle libraries transparently to user like workbench?

You shouldn’t need to do anything to support linked libraries. Linked objects are part of the scene just like local ones. I think the reason for missing objects is most likely elsewhere, perhaps because you’re not handling instancing (with depsgraph.object_instances) and the linked objects happen to be instanced.

Aha. Now that you mention it I remember seeing those missing objects in my early test versions. And yes, these were collection instances to objects in linked libraries.

I do support instances but I currently read them from depsgraph.scene.objects and depsgraph.updates. One reason for that is that depsgraph.updates for instances give bpy.types.Object with instance_type == ‘COLLECTION’ which I can can directly parse instead of looping through depsgraph.object_instances to find a matching entry, and that works for all objects whereas depsgraph.object_instances only lists ones potentially visible in the current context. So I probably need to change the design and use depsgraph.object_instances instead.