3
$\begingroup$

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

$\endgroup$

1 Answer 1

3
$\begingroup$

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
$\endgroup$
3
  • 2
    $\begingroup$ 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? $\endgroup$ Apr 18, 2023 at 8:47
  • $\begingroup$ 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? $\endgroup$ Apr 18, 2023 at 9:07
  • $\begingroup$ 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. $\endgroup$ Apr 18, 2023 at 9:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .