3

I am trying to use this operator: bpy.ops.outliner.id_paste() to paste a collection I have copied in the clipboard, and my problem is setting the right context, because I keep getting this error:

RuntimeError: Operator bpy.ops.outliner.id_paste.poll() failed, context is incorrect

This has been my current approach. I suspect it might be solved by being able to select the scene collection, but I have tried almost everything I could find.

override = bpy.context.copy()
for area in bpy.context.screen.areas:
    if area.type == 'OUTLINER':
        override['area'] = area

bpy.ops.outliner.id_paste()

Any help would be greatly appreciated :)

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51

1 Answers1

3

You have to override the context:

import bpy

obj = bpy.context.active_object

for area in bpy.context.screen.areas: if area.type != 'OUTLINER': continue with bpy.context.temp_override(active_object=obj, area=area): bpy.ops.outliner.id_paste() break

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51
  • 2
    Wow, okay yeah this gets me pretty close, now this actually pastes the collection into the outliner, fantastic, the only problem now is that it pastes it to whatever is active I think? but it should always paste to the scene collection. I am creating a script that sends collections from one file to another, and I won't always know what is active, so it should just default to scene collection. Also do you know how to get a reference to the collection that was just pasted? – William Larsen Bang Apr 18 '23 at 08:47
  • Yeah you need to set the Scene Collection active but there seems to be no way to do that via script, at least I'm struggling to find a way. Have you found a way? – Harry McKenzie Apr 18 '23 at 09:07
  • No I haven't either, all I have is the fact that this exists bpy.context.scene.collection, but no way of getting me there. I think you got as close as possible right now, but I might do the whole operation a different way, by simply pasting the objects and then creating a new collection for the objects, then I can control that collection. – William Larsen Bang Apr 18 '23 at 09:10