Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active November 30, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/e1cf3a62750475e1f289 to your computer and use it in GitHub Desktop.
Save zeffii/e1cf3a62750475e1f289 to your computer and use it in GitHub Desktop.
import bpy
import itertools
node_tree = bpy.data.materials['Material'].node_tree
node = node_tree.nodes['Mapping']
def store_replace(node_tree, node):
props_to_copy = 'bl_idname name location height width'.split(' ')
reconnections = []
mappings = itertools.chain.from_iterable([node.inputs, node.outputs])
for i in (i for i in mappings if i.is_linked):
for L in i.links:
reconnections.append([L.from_socket.path_from_id(), L.to_socket.path_from_id()])
props = {j: getattr(node, j) for j in props_to_copy}
new_node = node_tree.nodes.new(props['bl_idname'])
props_to_copy.pop(0)
for prop in props_to_copy:
setattr(new_node, prop, props[prop])
nodes = node_tree.nodes
nodes.remove(node)
new_node.name = props['name']
for str_from, str_to in reconnections:
node_tree.links.new(eval(str_from), eval(str_to))
store_replace(node_tree, node)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment