Skip to content

Instantly share code, notes, and snippets.

@eliemichel
Last active May 30, 2022 17:06
Show Gist options
  • Save eliemichel/73dcb5c5f6980c3749ce13ff356931b5 to your computer and use it in GitHub Desktop.
Save eliemichel/73dcb5c5f6980c3749ce13ff356931b5 to your computer and use it in GitHub Desktop.
Snippet for reproducing the Shift+D in a node graph editor in Blender
import bpy
def duplicate_node(n1, node_group=None, duplicate_links=False):
"""
Duplicate a node (without duplicating its links)
@param n1 The node to duplicate
@param node_group (optional) Group in which the node must be duplicated
@param duplicate_links Whether to keep input links or not
@return the new duplicate node
"""
if node_group is None:
node_group = n1.id_data
n2 = node_group.nodes.new(type=n1.rna_type.identifier)
for in1, in2 in zip(n1.inputs, n2.inputs):
if hasattr(in1, 'default_value'):
in2.default_value = in1.default_value
if duplicate_links:
for link in in1.links:
node_group.links.new(input=link.from_socket, output=in2)
for out1, out2 in zip(n1.outputs, n2.outputs):
if hasattr(out1, 'default_value'):
out2.default_value = out1.default_value
for prop in n1.bl_rna.properties:
prop_id = prop.identifier
if prop_id in n1.bl_rna.base.properties or prop.is_readonly:
continue
value = getattr(n1, prop_id)
setattr(n2, prop_id, value)
if __name__ == "__main__":
# Example, assuming there is a node called 'Extrude Mesh'
g = bpy.data.node_groups['Geometry Nodes']
duplicate_node(g.nodes['Extrude Mesh'], duplicate_links=True)
@eliemichel
Copy link
Author

(Tested in Blender 3.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment