[Bf-blender-cvs] [f1de50b3872] builtin-simulation-nodes: initial export node group operator

Jacques Lucke noreply at git.blender.org
Thu Mar 19 15:10:16 CET 2020


Commit: f1de50b38722634a5757f23fe2e35f58a14b69fc
Author: Jacques Lucke
Date:   Thu Mar 19 14:58:52 2020 +0100
Branches: builtin-simulation-nodes
https://developer.blender.org/rBf1de50b38722634a5757f23fe2e35f58a14b69fc

initial export node group operator

===================================================================

M	release/scripts/startup/bl_operators/node.py

===================================================================

diff --git a/release/scripts/startup/bl_operators/node.py b/release/scripts/startup/bl_operators/node.py
index 9fdaebc7540..ff0bd543f15 100644
--- a/release/scripts/startup/bl_operators/node.py
+++ b/release/scripts/startup/bl_operators/node.py
@@ -18,7 +18,9 @@
 
 # <pep8-80 compliant>
 
+import os
 import bpy
+import json
 import nodeitems_utils
 from bpy.types import (
     Operator,
@@ -295,6 +297,130 @@ class NODE_OT_tree_path_parent(Operator):
         return {'FINISHED'}
 
 
+def get_socket_value(sock):
+    if sock.type in {"VALUE", "INT", "BOOLEAN", "STRING"}:
+        return sock.default_value
+    elif sock.type in {"VECTOR", "COLOR"}:
+        return tuple(sock.default_value)
+    else:
+        return None
+
+def insert_socket_data(sock_data, sock):
+    sock_data["name"] = sock.name
+    sock_data["identifier"] = sock.identifier
+    sock_data["default_value"] = get_socket_value(sock)
+    sock_data["type"] = sock.type
+
+def insert_group_interface(json_data, group):
+    for sock in group.inputs:
+        sock_data = {}
+        insert_socket_data(sock_data, sock)
+        sock_data["bl_socket_idname"] = sock.bl_socket_idname
+        json_data["inputs"].append(sock_data)
+
+    for sock in group.outputs:
+        sock_data = {}
+        insert_socket_data(sock_data, sock)
+        sock_data["bl_socket_idname"] = sock.bl_socket_idname
+        json_data["outputs"].append(sock_data)
+
+def insert_node_data(node_data, node):
+    node_data["name"] = node.name
+    node_data["bl_idname"] = node.bl_idname
+    node_data["location"] = tuple(map(int, node.location))
+    node_data["width"] = node.width
+
+    # Don't compute this only once in the beginning, because addons might register more properties.
+    base_node_property_names = {prop.identifier for prop in bpy.types.Node.bl_rna.properties}
+
+    if node.bl_idname in {"NodeGroupInput", "NodeGroupOutput"}:
+        pass
+    elif node.bl_idname == "SimulationNodeGroup":
+        node_data["group_name"] = getattr(node.node_tree, "name", None)
+    else:
+        node_data["properties"] = {}
+        for prop in node.bl_rna.properties:
+            if prop.identifier not in base_node_property_names:
+                print("Allow: ", prop.identifier)
+                node_data["properties"][prop.identifier] = getattr(node, prop.identifier)
+
+        node_data["inputs"] = []
+        node_data["outputs"] = []
+        for sock in node.inputs:
+            sock_data = {}
+            insert_socket_data(sock_data, sock)
+            node_data["inputs"].append(sock_data)
+        for sock in node.outputs:
+            sock_data = {}
+            insert_socket_data(sock_data, sock)
+            node_data["outputs"].append(sock_data)
+
+def insert_nodes_data(json_data, group):
+    for node in group.nodes:
+        node_data = {}
+        insert_node_data(node_data, node)
+        json_data["nodes"].append(node_data)
+
+def insert_links_data(json_data, group):
+    for link in group.links:
+        link_data = {}
+        link_data["from"] = [link.from_node.name, link.from_socket.identifier, list(link.from_node.outputs).index(link.from_socket)]
+        link_data["to"] = [link.to_node.name, link.to_socket.identifier, list(link.to_node.inputs).index(link.to_socket)]
+        json_data["links"].append(link_data)
+
+def find_direct_dependency_groups(group):
+    dependencies = set()
+    for node in group.nodes:
+        if node.bl_idname == "SimulationNodeGroup":
+            if node.node_tree is not None:
+                dependencies.add(node.node_tree)
+    return dependencies
+
+def insert_dependencies(json_data, group):
+    dependency_names = [dependency.name for dependency in find_direct_dependency_groups(group)]
+    json_data["dependencies"] = dependency_names
+
+def node_group_to_json_data(group):
+    json_data = {}
+    json_data["blender_version"] = bpy.app.version
+    json_data["name"] = group.name
+    json_data["inputs"] = []
+    json_data["outputs"] = []
+    json_data["nodes"] = []
+    json_data["links"] = []
+
+    insert_group_interface(json_data, group)
+    insert_nodes_data(json_data, group)
+    insert_links_data(json_data, group)
+    insert_dependencies(json_data, group)
+
+    return json_data
+
+def save_group_as_json(group, file_path):
+    json_data = node_group_to_json_data(group)
+    json_str = json.dumps(json_data, indent=1)
+    with open(file_path, "w") as f:
+        f.write(json_str)
+
+
+group_template_directory = "/home/jacques/Documents/node_groups"
+
+class NODE_OT_export_group_template(Operator):
+    bl_idname = "node.export_group_template"
+    bl_label = "Export Node Group Template"
+
+    @classmethod
+    def poll(cls, context):
+        space = context.space_data
+        return space.type == 'NODE_EDITOR' and getattr(space.edit_tree, "bl_idname", "") == "SimulationNodeTree"
+
+    def execute(self, context):
+        group = context.space_data.edit_tree
+        file_path = os.path.join(group_template_directory, group.name + ".json")
+        save_group_as_json(group, file_path)
+        return {'FINISHED'}
+
+
 classes = (
     NodeSetting,
 
@@ -303,4 +429,5 @@ classes = (
     NODE_OT_add_search,
     NODE_OT_collapse_hide_unused_toggle,
     NODE_OT_tree_path_parent,
+    NODE_OT_export_group_template,
 )



More information about the Bf-blender-cvs mailing list