Copy/Paste operators cleanup

refactor
Jonas Holzman 2025-03-20 01:42:42 +01:00
parent 9940a9c8ca
commit 4e029c59a2
2 changed files with 13 additions and 8 deletions

View File

@ -31,7 +31,7 @@ def get_bl_object(data):
return bpy.context.object.modifiers.active.node_group
def dump(ob):
def dump_nodes(ob):
"""Generic Recursive Dump, convert any object into a dict"""
Dumper.pointers.clear()
@ -45,7 +45,7 @@ def dump(ob):
return data
def load(data, bl_object=None):
def load_nodes(data, bl_object=None):
"""Generic Load to create an object from a dict"""
Dumper.pointers.clear()

View File

@ -15,7 +15,7 @@ from bpy.props import BoolProperty, EnumProperty
from bpy.types import Operator
# from node_kit.core.node_tree import NodeTree
from .core.dumper import dump, load
from .core.dumper import dump_nodes, load_nodes
from .core.node_utils import remap_node_group_duplicates
from .core.pack_nodes import combine_objects, extract_objects
@ -23,39 +23,44 @@ from .core.pack_nodes import combine_objects, extract_objects
class NODEKIT_OT_copy(Operator):
bl_idname = "node_kit.copy_node_tree"
bl_label = "Copy nodes"
bl_description = "Copy nodes to system clipboard"
bl_options = {"REGISTER", "UNDO"}
select_only: BoolProperty(default=True)
select_only: BoolProperty(default=True) # TODO: Expose/rework this property properly - No F3 panel in Node Editor - Only F9
def execute(self, context):
ntree = context.space_data.edit_tree
if self.select_only:
ntree_data = {
"nodes": dump(
"nodes": dump_nodes(
[n for n in ntree.nodes if n.select]
), # [dump(n) for n in ntree.nodes if n.select],
"links": dump(
"links": dump_nodes(
[l for l in ntree.links if l.from_node.select and l.to_node.select]
),
}
else:
ntree_data = dump(ntree)
ntree_data = dump_nodes(ntree)
pprint(ntree_data)
context.window_manager.clipboard = json.dumps(ntree_data)
self.report({"INFO"}, f"Copied 5 selected nodes to system clipboard")
return {"FINISHED"}
class NODEKIT_OT_paste(Operator):
bl_idname = "node_kit.paste_node_tree"
bl_label = "Paste nodes"
bl_description = "Paste nodes from system clipboard"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
ntree_data = json.loads(context.window_manager.clipboard)
load(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")
return {"FINISHED"}