# How to copy nodes and their links from one node tree/material to another - in python?

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?