1
$\begingroup$

I'm trying to copy nodes from one node tree/material to another, in python.

As far as I can tell there is no good way to do this.

I have even tried context switching and then either try to use

bpy.ops.node.clipboard_copy() or bpy.ops.node.group_make() on the selected nodes.

But both will fail due to wrong context, even though I have successfully changed the area.type to "NODE_EDITOR" or the area.ui_type to "ShaderNodeTree".

So, I'm at a loss here. I don't want to have to figure out the entire tree structure and re-create all individual nodes. I can select the nodes I need easily though. I would also be fine with somehow merging both trees and removing what I don't need.

Any help with this would be really appreciated. Blender 2.80. Just getting the node ops to work after a context switch to the node editor, would suffice already.

To clarify: I'm running my tool from VIEW_3D in object mode. I'm switching the context to the node editor like so:

def change_context(string):
    area = bpy.context.area
    old_type = area.type
    area.type = string
    return old_type

This works, in that it switches the 3d view to the node editor, but following this with calling bpy.ops.node.group_make() or bpy.ops.node.copy() will fail:

RuntimeError: Operator bpy.ops.node.group_make.poll() failed, context is incorrect

Calling the node ops from the search bar in proper, manually changed context works. So I suspect my context switching is incomplete?

edit2: video demo https://www.youtube.com/watch?v=N9tZLqHvcL4

  1. running group_make() from node editor, works fine
  2. switching area to node editor and running it, fails
$\endgroup$
  • $\begingroup$ Do you really need to switch the context? Where do you call it? $\endgroup$ – brockmann Nov 29 '18 at 18:28
  • $\begingroup$ @jaroslav-jerryno-novotny I'm aware of these things, as I said, I don't want to recreate arbitarry node trees, node by node. $\endgroup$ – MACHIN3 Nov 29 '18 at 18:31
  • $\begingroup$ Ok, then I don't have a solution.. $\endgroup$ – Jaroslav Jerryno Novotny Nov 29 '18 at 18:32
  • $\begingroup$ @brockmann You tell me, he node ops don't work, in VIEW_3D, obviously, but also won't work in NODE_EDITOR. $\endgroup$ – MACHIN3 Nov 29 '18 at 18:32
  • $\begingroup$ You want to call it from 3d View? Obviously? What I'm going to I tell you? Sorry, don't get it @MACHIN3 $\endgroup$ – brockmann Nov 29 '18 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.