Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active November 30, 2015 19:48
Show Gist options
  • Save zeffii/9f951015974389404bc7 to your computer and use it in GitHub Desktop.
Save zeffii/9f951015974389404bc7 to your computer and use it in GitHub Desktop.
import bpy
import itertools
def store_replace(node):
node_tree = node.id_data
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))
def main(operator, context):
space = context.space_data
node_active = context.active_node
node_selected = context.selected_nodes
# now we have the context, perform a simple operation
if not (len(node_selected) == 1):
operator.report({'ERROR'}, "1 nodes must be selected")
return
store_replace(node_active)
class NodeResetOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "node.delete_and_rebuild"
bl_label = "Reset Node (keep connections)"
@classmethod
def poll(cls, context):
space = context.space_data
return space.type == 'NODE_EDITOR'
def execute(self, context):
main(self, context)
return {'FINISHED'}
def register():
bpy.utils.register_class(NodeResetOperator)
def unregister():
bpy.utils.unregister_class(NodeResetOperator)
if __name__ == "__main__":
register()
@zeffii
Copy link
Author

zeffii commented Nov 30, 2015

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