Further Operators / UI Improvements

refactor
Jonas Holzman 2025-03-20 11:34:21 +01:00
parent 4e029c59a2
commit 698ace38fd
2 changed files with 55 additions and 41 deletions

View File

@ -1,5 +1,5 @@
""" """
This module contains all addons operators Node Kit Operators
:author: Autour de Minuit :author: Autour de Minuit
:maintainers: Florentin LUCE :maintainers: Florentin LUCE
@ -21,38 +21,51 @@ from .core.pack_nodes import combine_objects, extract_objects
class NODEKIT_OT_copy(Operator): class NODEKIT_OT_copy(Operator):
bl_idname = "node_kit.copy_node_tree" bl_idname = "node_kit.copy_nodes"
bl_label = "Copy nodes" bl_label = "Copy Nodes"
bl_description = "Copy nodes to system clipboard" bl_description = "Copy nodes to system clipboard"
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
select_only: BoolProperty(default=True) # TODO: Expose/rework this property properly - No F3 panel in Node Editor - Only F9
def execute(self, context): def execute(self, context):
ntree = context.space_data.edit_tree ntree = context.space_data.edit_tree
if self.select_only: selected_nodes = [node for node in ntree.nodes if node.select]
ntree_data = {
"nodes": dump_nodes( ntree_data = {
[n for n in ntree.nodes if n.select] "nodes": dump_nodes(selected_nodes),
), # [dump(n) for n in ntree.nodes if n.select], "links": dump_nodes(
"links": dump_nodes( [l for l in ntree.links if l.from_node.select and l.to_node.select]
[l for l in ntree.links if l.from_node.select and l.to_node.select] ),
), }
}
else:
ntree_data = dump_nodes(ntree)
pprint(ntree_data) pprint(ntree_data)
context.window_manager.clipboard = json.dumps(ntree_data) context.window_manager.clipboard = json.dumps(ntree_data)
self.report({"INFO"}, f"Copied 5 selected nodes to system clipboard") self.report({"INFO"}, f"Copied {len(selected_nodes)} selected nodes to system clipboard")
return {"FINISHED"}
class NODEKIT_OT_copy_tree(Operator):
bl_idname = "node_kit.copy_node_tree"
bl_label = "Copy Node Tree"
bl_description = "Copy node tree to system clipboard"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
ntree = context.space_data.edit_tree
ntree_data = dump_nodes(ntree)
pprint(ntree_data)
context.window_manager.clipboard = json.dumps(ntree_data)
self.report({"INFO"}, f"Copied {len(ntree.nodes)} selected nodes to system clipboard")
return {"FINISHED"} return {"FINISHED"}
class NODEKIT_OT_paste(Operator): class NODEKIT_OT_paste(Operator):
bl_idname = "node_kit.paste_node_tree" bl_idname = "node_kit.paste_nodes"
bl_label = "Paste nodes" bl_label = "Paste Nodes"
bl_description = "Paste nodes from system clipboard" bl_description = "Paste nodes from system clipboard"
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
@ -60,13 +73,13 @@ class NODEKIT_OT_paste(Operator):
ntree_data = json.loads(context.window_manager.clipboard) ntree_data = json.loads(context.window_manager.clipboard)
load_nodes(ntree_data, context.space_data.edit_tree) load_nodes(ntree_data, context.space_data.edit_tree)
self.report({"INFO"}, f"5 node(s) pasted from system clipboard") self.report({"INFO"}, f"X node(s) pasted from system clipboard") # TODO: Ge the number of parsed nodes returned
return {"FINISHED"} return {"FINISHED"}
class NODEKIT_OT_remap_node_group_duplicates(Operator): class NODEKIT_OT_remap_node_group_duplicates(Operator):
bl_idname = "node_kit.remap_node_group_duplicates" bl_idname = "node_kit.remap_node_group_duplicates"
bl_label = "Clean nodes" bl_label = "Remap Node Groups Duplicates"
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
selection: EnumProperty( selection: EnumProperty(
@ -127,7 +140,7 @@ class NODEKIT_OT_remap_node_group_duplicates(Operator):
class NODEKIT_OT_update_nodes(Operator): class NODEKIT_OT_update_nodes(Operator):
bl_idname = "node_kit.update_nodes" bl_idname = "node_kit.update_nodes"
bl_label = "Update node" bl_label = "Update Nodes"
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
selection: EnumProperty( selection: EnumProperty(
@ -237,7 +250,7 @@ class NODEKIT_OT_update_nodes(Operator):
class NODEKIT_OT_pack_nodes(Operator): class NODEKIT_OT_pack_nodes(Operator):
bl_idname = "node_kit.pack_nodes" bl_idname = "node_kit.pack_nodes"
bl_label = "Update node" bl_label = "Pack Nodes"
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
def execute(self, context): def execute(self, context):
@ -247,7 +260,7 @@ class NODEKIT_OT_pack_nodes(Operator):
class NODEKIT_OT_unpack_nodes(Operator): class NODEKIT_OT_unpack_nodes(Operator):
bl_idname = "node_kit.unpack_nodes" bl_idname = "node_kit.unpack_nodes"
bl_label = "Update node" bl_label = "Unpack Nodes"
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
def execute(self, context): def execute(self, context):
@ -257,6 +270,7 @@ class NODEKIT_OT_unpack_nodes(Operator):
classes = ( classes = (
NODEKIT_OT_copy, NODEKIT_OT_copy,
NODEKIT_OT_copy_tree,
NODEKIT_OT_paste, NODEKIT_OT_paste,
NODEKIT_OT_remap_node_group_duplicates, NODEKIT_OT_remap_node_group_duplicates,
NODEKIT_OT_update_nodes, NODEKIT_OT_update_nodes,

34
ui.py
View File

@ -1,5 +1,5 @@
""" """
This module contains blender UI elements Node Kit UI elements and menus.
:author: Autour de Minuit :author: Autour de Minuit
:maintainers: Florentin LUCE :maintainers: Florentin LUCE
@ -15,22 +15,22 @@ class NODEKIT_MT_node_kit(bpy.types.Menu):
def draw(self, context): def draw(self, context):
layout = self.layout layout = self.layout
layout.operator("node_kit.copy_node_tree", text="Copy Nodes", icon="COPYDOWN") layout.operator("node_kit.copy_nodes", icon="COPYDOWN")
layout.operator( layout.operator("node_kit.paste_nodes", icon="PASTEDOWN")
"node_kit.paste_node_tree", text="Paste Nodes", icon="PASTEDOWN"
)
layout.separator() layout.separator()
layout.operator(
"node_kit.remap_node_group_duplicates", layout.operator("node_kit.copy_node_tree", icon="NODETREE")
text="Remap Node Groups Duplicates",
icon="NODE_INSERT_OFF",
)
layout.operator("node_kit.update_nodes", text="Update Nodes", icon="IMPORT")
layout.separator() layout.separator()
layout.operator("node_kit.pack_nodes", text="Pack Nodes", icon="PACKAGE")
layout.operator( layout.operator("node_kit.remap_node_group_duplicates",icon="NODE_INSERT_OFF")
"node_kit.unpack_nodes", text="UnPack Nodes", icon="UGLYPACKAGE" layout.operator("node_kit.update_nodes", icon="IMPORT")
)
layout.separator()
layout.operator("node_kit.pack_nodes", icon="PACKAGE")
layout.operator("node_kit.unpack_nodes", icon="UGLYPACKAGE")
classes = (NODEKIT_MT_node_kit,) classes = (NODEKIT_MT_node_kit,)
@ -48,7 +48,7 @@ def register():
def unregister(): def unregister():
bpy.types.NODE_MT_editor_menus.remove(draw_menu)
for c in reversed(classes): for c in reversed(classes):
bpy.utils.unregister_class(c) bpy.utils.unregister_class(c)
bpy.types.NODE_MT_editor_menus.remove(draw_menu)