[Bf-blender-cvs] [76082b0494f] builtin-simulation-nodes: import node group loading

Jacques Lucke noreply at git.blender.org
Mon Mar 23 13:28:24 CET 2020


Commit: 76082b0494f406fc72aa0bb04d3390124c71bc2d
Author: Jacques Lucke
Date:   Mon Mar 23 12:50:51 2020 +0100
Branches: builtin-simulation-nodes
https://developer.blender.org/rB76082b0494f406fc72aa0bb04d3390124c71bc2d

import node group loading

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

A	release/datafiles/node_groups/Particle_Age_Reached_Event.json
M	release/scripts/modules/builtin_node_groups.py
M	release/scripts/startup/bl_operators/node.py

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

diff --git a/release/datafiles/node_groups/Particle_Age_Reached_Event.json b/release/datafiles/node_groups/Particle_Age_Reached_Event.json
new file mode 100644
index 00000000000..04974ede07c
--- /dev/null
+++ b/release/datafiles/node_groups/Particle_Age_Reached_Event.json
@@ -0,0 +1,170 @@
+{
+ "blender_version": [
+  2,
+  83,
+  10
+ ],
+ "name": "Particle Age Reached Event",
+ "inputs": [
+  {
+   "name": "Age",
+   "identifier": "Input_3",
+   "default_value": 0.5,
+   "type": "VALUE",
+   "bl_socket_idname": "NodeSocketFloat"
+  },
+  {
+   "name": "Execute",
+   "identifier": "Input_1",
+   "default_value": null,
+   "type": "CONTROL_FLOW",
+   "bl_socket_idname": "NodeSocketControlFlow"
+  }
+ ],
+ "outputs": [
+  {
+   "name": "Event",
+   "identifier": "Output_5",
+   "default_value": null,
+   "type": "EVENTS",
+   "bl_socket_idname": "NodeSocketEvents"
+  }
+ ],
+ "nodes": [
+  {
+   "name": "Execute Condition",
+   "bl_idname": "SimulationNodeExecuteCondition",
+   "location": [
+    -13,
+    -65
+   ],
+   "width": 140,
+   "properties": {},
+   "inputs": [
+    {
+     "name": "Condition",
+     "identifier": "Condition",
+     "default_value": false,
+     "type": "BOOLEAN"
+    },
+    {
+     "name": "If True",
+     "identifier": "If True",
+     "default_value": null,
+     "type": "CONTROL_FLOW"
+    },
+    {
+     "name": "If False",
+     "identifier": "If False",
+     "default_value": null,
+     "type": "CONTROL_FLOW"
+    }
+   ],
+   "outputs": [
+    {
+     "name": "Execute",
+     "identifier": "Execute",
+     "default_value": null,
+     "type": "CONTROL_FLOW"
+    }
+   ]
+  },
+  {
+   "name": "Group Input",
+   "bl_idname": "NodeGroupInput",
+   "location": [
+    -390,
+    -94
+   ],
+   "width": 140
+  },
+  {
+   "name": "Group",
+   "bl_idname": "SimulationNodeGroup",
+   "location": [
+    -384,
+    16
+   ],
+   "width": 140,
+   "group_name": "Particle Age"
+  },
+  {
+   "name": "Particle Time Step Event",
+   "bl_idname": "SimulationNodeParticleTimeStepEvent",
+   "location": [
+    168,
+    -60
+   ],
+   "width": 140,
+   "properties": {
+    "mode": "BEGIN"
+   },
+   "inputs": [
+    {
+     "name": "Execute",
+     "identifier": "Execute",
+     "default_value": null,
+     "type": "CONTROL_FLOW"
+    }
+   ],
+   "outputs": [
+    {
+     "name": "Event",
+     "identifier": "Event",
+     "default_value": null,
+     "type": "EVENTS"
+    }
+   ]
+  },
+  {
+   "name": "Group Output",
+   "bl_idname": "NodeGroupOutput",
+   "location": [
+    354,
+    -64
+   ],
+   "width": 140
+  }
+ ],
+ "links": [
+  {
+   "from": [
+    "Particle Time Step Event",
+    "Event",
+    0
+   ],
+   "to": [
+    "Group Output",
+    "Output_5",
+    0
+   ]
+  },
+  {
+   "from": [
+    "Group Input",
+    "Input_1",
+    1
+   ],
+   "to": [
+    "Execute Condition",
+    "If True",
+    1
+   ]
+  },
+  {
+   "from": [
+    "Execute Condition",
+    "Execute",
+    0
+   ],
+   "to": [
+    "Particle Time Step Event",
+    "Execute",
+    0
+   ]
+  }
+ ],
+ "dependencies": [
+  "Particle Age"
+ ]
+}
\ No newline at end of file
diff --git a/release/scripts/modules/builtin_node_groups.py b/release/scripts/modules/builtin_node_groups.py
index 72494c04a3d..8667139ddc7 100644
--- a/release/scripts/modules/builtin_node_groups.py
+++ b/release/scripts/modules/builtin_node_groups.py
@@ -1,6 +1,7 @@
 import os
 import bpy
 import json
+from functools import lru_cache
 
 
 def get_socket_value(sock):
@@ -133,7 +134,9 @@ def property_has_given_value(owner, prop_name, value):
     else:
         assert False
 
-def group_matches_json_data(group, json_data, loaded_group_by_name, json_data_by_group_name):
+def group_matches_json_data(group, json_data, loaded_group_by_name):
+    groups_json_data = get_builtin_groups_data()
+
     if len(group.nodes) != len(json_data["nodes"]):
         return False
     if len(group.links) != len(json_data["links"]):
@@ -158,9 +161,8 @@ def group_matches_json_data(group, json_data, loaded_group_by_name, json_data_by
             elif node.node_tree == loaded_group_by_name[node_data["group_name"]]:
                 pass
             elif not group_matches_json_data(node.node_tree,
-                                             json_data_by_group_name[node_data["group_name"]],
-                                             loaded_group_by_name,
-                                             json_data_by_group_name):
+                                             groups_json_data[node_data["group_name"]],
+                                             loaded_group_by_name):
                 return False
 
         else:
@@ -199,9 +201,9 @@ def group_matches_json_data(group, json_data, loaded_group_by_name, json_data_by
 
     return True
 
-def get_or_create_node_group(json_data, loaded_group_by_name, json_data_by_group_name):
+def get_or_create_node_group(json_data, loaded_group_by_name):
     for potential_group in bpy.data.node_groups:
-        if group_matches_json_data(potential_group, json_data, loaded_group_by_name, json_data_by_group_name):
+        if group_matches_json_data(potential_group, json_data, loaded_group_by_name):
             return potential_group
 
     group = bpy.data.node_groups.new(json_data["name"], "SimulationNodeTree")
@@ -238,53 +240,66 @@ def get_or_create_node_group(json_data, loaded_group_by_name, json_data_by_group
 
     return group
 
-from collections import OrderedDict
+def export_builtin_node_group(group):
+    file_path = group_name_to_file_path(group.name)
+    save_group_as_json(group, file_path)
+
+ at lru_cache()
+def get_builtin_groups_data():
+    json_data_by_group_name = dict()
 
-def get_json_data_for_all_groups_to_load(main_group_name):
-    json_data_by_group_name = OrderedDict()
+    if not os.path.exists(builtin_node_group_directory):
+        return json_data_by_group_name
+
+    for file_name in os.listdir(builtin_node_group_directory):
+        if not file_name.endswith(".json"):
+            continue
+        file_path = os.path.join(builtin_node_group_directory, file_name)
+        with open(file_path, "r") as f:
+            json_str = f.read()
+        json_data = json.loads(json_str)
+        group_name = json_data["name"]
+        json_data_by_group_name[group_name] = json_data
 
-    def load(group_name):
-        if group_name in json_data_by_group_name:
-            return
-        else:
-            file_path = group_name_to_file_path(group_name)
-            with open(file_path, "r") as f:
-                json_str = f.read()
-            json_data = json.loads(json_str)
-            assert group_name == json_data["name"]
-            for dependency_name in json_data["dependencies"]:
-                load(dependency_name)
-            json_data_by_group_name[group_name] = json_data
-
-    load(main_group_name)
     return json_data_by_group_name
 
-def export_builtin_node_group(group):
-    file_path = group_name_to_file_path(group.name)
-    save_group_as_json(group, file_path)
+def get_sorted_group_names_to_load(main_group_name):
+    groups_json_data = get_builtin_groups_data()
+
+    sorted_group_names = []
+
+    def add_group(group_name):
+        if group_name in sorted_group_names:
+            return
+        json_data = groups_json_data[group_name]
+        for dependency_name in json_data["dependencies"]:
+            add_group(dependency_name)
+        sorted_group_names.append(group_name)
+
+    add_group(main_group_name)
+    return sorted_group_names
 
 def get_builtin_group_items_cb(node_tree_idname):
-    # TODO: save strings
     def items_generator(self, context):
-        if not os.path.exists(builtin_node_group_directory):
-            return [('NONE', "None", "")]
         items = []
-        for file_name in os.listdir(builtin_node_group_directory):
-            if not file_name.endswith(".json"):
-                continue
-            group_name = file_name_to_group_name(file_name)
-            items.append((file_name, group_name, ""))
-        if len(items) == 0:
-            items.append(('NONE', "None", ""))
+        for i, name in enumerate(get_builtin_groups_data().keys()):
+            items.append((str(i), name, ""))
         return items
     return items_generator
 
 def import_builtin_node_group_with_dependencies(group_name):
-    json_data_by_group_name = get_json_data_for_all_groups_to_load(group_name)
+    sorted_group_names = get_sorted_group_names_to_load(group_name)
+    groups_json_data = get_builtin_groups_data()
     loaded_group_by_name = dict()
 
-    for group_name, json_data in json_data_by_group_name.items():
-        group = get_or_create_node_group(json_data, loaded_group_by_name, json_data_by_group_name)
+    for group_name in sorted_group_names:
+        json_data = groups_json_data[group_name]
+        group = get_or_create_node_group(json_data, loaded_group_by_name)
         loaded_group_by_name[group_name] = group
 
     return loaded_group_by_name[group_name]
+
+def import_builtin_node_group_by_item_identifier(item_identifier):
+    index = int(item_identifier)
+    group_name = list(get_builtin_groups_data().keys())[index]
+    return import_builtin_node_group_with_dependencies(group_name)
diff --git a/release/scripts/startup/bl_operators/node.py b/release/scripts/startup/bl_operators/node.py
index 450e63007d7..8dd5d1a4da9 100644
--- a/release/scripts/startup/bl_operators/node.py
+++ b/release/scripts/startup/bl_operators/node.py
@@ -34,10 +34,9 @@ from bpy.props import (
     StringProperty,
 )
 from builtin_node_groups import (
-    file_name_to_group_name,
     export_builtin_node_group,
     get_builtin_group_items_cb,
-    import_builtin_node_group_with_dependencies,
+    import_builtin_node_group_by_item_identifier,
 )
 
 class NodeSetting(PropertyGroup):
@@ -333,8 +332,7 @@ class NODE_OT_import_group_template_search(bpy.types.Operator):
         if self.item == "NONE":
             return {'CANCELLED'}
         else:
-            group_name = file_name_to_group_name(self.item)
-            import_builtin_node_group_with_dependencies(group_name)
+            import_builtin_node_group_by_item_identifier(self.item)
             return {'FINISHED'}
 
 classes = (



More information about the Bf-blender-cvs mailing list