From 89a46539dbf209f45b2f04ae3331d17745079754 Mon Sep 17 00:00:00 2001 From: illua1 Date: Mon, 6 Mar 2023 18:17:15 +0300 Subject: [PATCH 01/30] init commit, currently crashed --- .../blenloader/intern/versioning_300.cc | 4 + .../blenloader/intern/versioning_common.cc | 76 +++++++++++++++++++ .../blenloader/intern/versioning_common.h | 4 + 3 files changed, 84 insertions(+) diff --git a/source/blender/blenloader/intern/versioning_300.cc b/source/blender/blenloader/intern/versioning_300.cc index 0eb71f7d7c8..6687bbcfce1 100644 --- a/source/blender/blenloader/intern/versioning_300.cc +++ b/source/blender/blenloader/intern/versioning_300.cc @@ -4003,6 +4003,10 @@ void blo_do_versions_300(FileData *fd, Library * /*lib*/, Main *bmain) } } + if (!MAIN_VERSION_ATLEAST(bmain, 306, 1)) { + remove_legacy_instances_on(bmain, bmain->objects); + } + /** * Versioning code until next subversion bump goes here. * diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 80df0a2ba74..4d8d637cace 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -9,12 +9,17 @@ #include #include "DNA_node_types.h" +#include "DNA_object_types.h" #include "DNA_screen_types.h" #include "BLI_listbase.h" #include "BLI_map.hh" +#include "BLI_multi_value_map.hh" +#include "BLI_set.hh" +#include "BLI_span.hh" #include "BLI_string.h" #include "BLI_string_ref.hh" +#include "BLI_vector.hh" #include "BKE_animsys.h" #include "BKE_lib_id.h" @@ -264,3 +269,74 @@ void node_tree_relink_with_socket_id_map(bNodeTree &ntree, } } } + +namespace replace_legacy_instances { + +using namespace blender; + +bNode *childrens_combine_node_group(Main *bmain, const Span, const char *name) +{ + std::stringstream tree_name_stream; + tree_name_stream << name << "childrens"; + const std::string tree_name = tree_name_stream.str(); + + bNodeTree *node_tree = ntreeAddTree(bmain, tree_name.c_str(), "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Childrens"); + + return nullptr; +} +bNode *buildin_instancing_node_group(Main *bmain, const bool on_vertices) +{ + return nullptr; +} +bNodeTree *instances_node_group(Main *bmain, bNode *children_combine_node, bNode *instancer_node) +{ + return nullptr; +} + +void object_push_instances_modifier(Main *bmain, Object *object, bNodeTree *node_tree) +{ +} + +} // namespace replace_legacy_instances + +void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) +{ + using namespace blender; + + Vector objects(lb_objects); + + MultiValueMap parents_map; + + for (Object *object : objects) { + Object *emitter = object->parent; + if (emitter == nullptr) { + continue; + } + /* Object, that may have children, but that not an instance emitter. */ + if (emitter->transflag == 0) { + continue; + } + parents_map.add(emitter, object); + } + + // Map + + for (auto &&[parent, objects] : parents_map.items()) { + + /* Or on faces (OB_DUPLIFACES). */ + const bool on_vertices = (parent->transflag & OB_DUPLIVERTS) != 0; + + using namespace replace_legacy_instances; + bNode *childrens_combine_node = childrens_combine_node_group(bmain, objects, parent->id.name); + bNode *instancer_node = buildin_instancing_node_group(bmain, on_vertices); + bNodeTree *geometry_node_instances_group = instances_node_group( + bmain, childrens_combine_node, instancer_node); + object_push_instances_modifier(bmain, parent, geometry_node_instances_group); + + // parent->transflag = 0; + } + + printf("HELLO!\n"); +} \ No newline at end of file diff --git a/source/blender/blenloader/intern/versioning_common.h b/source/blender/blenloader/intern/versioning_common.h index 40f383a27b2..464bcdc6917 100644 --- a/source/blender/blenloader/intern/versioning_common.h +++ b/source/blender/blenloader/intern/versioning_common.h @@ -8,6 +8,7 @@ #ifdef __cplusplus # include "BLI_map.hh" +# include "BLI_span.hh" #endif struct ARegion; @@ -103,4 +104,7 @@ void node_tree_relink_with_socket_id_map(bNodeTree &ntree, bNode &old_node, bNode &new_node, const blender::Map &map); + +void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects); + #endif -- 2.30.2 From ba4da5bbeca5edd8be028eabc7bddd99781663fe Mon Sep 17 00:00:00 2001 From: illua1 Date: Tue, 7 Mar 2023 19:42:56 +0300 Subject: [PATCH 02/30] trying to debug that --- source/blender/blenloader/intern/versioning_common.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 4d8d637cace..acf40b6c7d9 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -310,6 +310,9 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) MultiValueMap parents_map; for (Object *object : objects) { + + printf("Name: %s, Pointer: %p, Parent: %p;\n", object->id.name, object, object->parent); + continue; Object *emitter = object->parent; if (emitter == nullptr) { continue; -- 2.30.2 From 2e3b4275423c248fa031a50f37da75cea2e3bfc0 Mon Sep 17 00:00:00 2001 From: illua1 Date: Tue, 7 Mar 2023 20:46:57 +0300 Subject: [PATCH 03/30] progress fix crash, create object info nodes --- .../blenloader/intern/versioning_300.cc | 8 ++-- .../blenloader/intern/versioning_common.cc | 41 +++++++++++-------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_300.cc b/source/blender/blenloader/intern/versioning_300.cc index 6687bbcfce1..d75f6038416 100644 --- a/source/blender/blenloader/intern/versioning_300.cc +++ b/source/blender/blenloader/intern/versioning_300.cc @@ -1213,6 +1213,10 @@ void do_versions_after_linking_300(FileData * /*fd*/, Main *bmain) FOREACH_NODETREE_END; } + if (!MAIN_VERSION_ATLEAST(bmain, 306, 1)) { + remove_legacy_instances_on(bmain, bmain->objects); + } + /** * Versioning code until next subversion bump goes here. * @@ -4003,10 +4007,6 @@ void blo_do_versions_300(FileData *fd, Library * /*lib*/, Main *bmain) } } - if (!MAIN_VERSION_ATLEAST(bmain, 306, 1)) { - remove_legacy_instances_on(bmain, bmain->objects); - } - /** * Versioning code until next subversion bump goes here. * diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index acf40b6c7d9..ab3bf0e07bd 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -274,23 +274,36 @@ namespace replace_legacy_instances { using namespace blender; -bNode *childrens_combine_node_group(Main *bmain, const Span, const char *name) +bNodeTree *childrens_combine_node_group(Main *bmain, + const Span objects, + const char *name) { - std::stringstream tree_name_stream; - tree_name_stream << name << "childrens"; - const std::string tree_name = tree_name_stream.str(); - + const std::string tree_name = std::string(name) + std::string("_childrens"); bNodeTree *node_tree = ntreeAddTree(bmain, tree_name.c_str(), "GeometryNodeTree"); ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Childrens"); - return nullptr; + bNode *join_objects = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_JOIN_GEOMETRY); + + bNodeSocket *to_join_objects = reinterpret_cast(join_objects->inputs.first); + + for (const Object *object : objects) { + bNode *object_info = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_OBJECT_INFO); + + bNodeSocket *from_object = reinterpret_cast(object_info->outputs.last); + + nodeAddLink(node_tree, object_info, from_object, join_objects, to_join_objects); + } + + return node_tree; } -bNode *buildin_instancing_node_group(Main *bmain, const bool on_vertices) +bNodeTree *buildin_instancing_node_group(Main *bmain, const bool on_vertices) { return nullptr; } -bNodeTree *instances_node_group(Main *bmain, bNode *children_combine_node, bNode *instancer_node) +bNodeTree *instances_node_group(Main *bmain, + bNodeTree *children_combine_node_group, + bNodeTree *instancer_node_group) { return nullptr; } @@ -305,14 +318,9 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) { using namespace blender; - Vector objects(lb_objects); - MultiValueMap parents_map; - for (Object *object : objects) { - - printf("Name: %s, Pointer: %p, Parent: %p;\n", object->id.name, object, object->parent); - continue; + LISTBASE_FOREACH (Object *, object, &lb_objects) { Object *emitter = object->parent; if (emitter == nullptr) { continue; @@ -332,8 +340,9 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) const bool on_vertices = (parent->transflag & OB_DUPLIVERTS) != 0; using namespace replace_legacy_instances; - bNode *childrens_combine_node = childrens_combine_node_group(bmain, objects, parent->id.name); - bNode *instancer_node = buildin_instancing_node_group(bmain, on_vertices); + bNodeTree *childrens_combine_node = childrens_combine_node_group( + bmain, objects, parent->id.name + 2); + bNodeTree *instancer_node = buildin_instancing_node_group(bmain, on_vertices); bNodeTree *geometry_node_instances_group = instances_node_group( bmain, childrens_combine_node, instancer_node); object_push_instances_modifier(bmain, parent, geometry_node_instances_group); -- 2.30.2 From b98ea8d40909cdd110436d142840560908eda2ae Mon Sep 17 00:00:00 2001 From: illua1 Date: Tue, 7 Mar 2023 20:59:42 +0300 Subject: [PATCH 04/30] progress delete redurant inclide --- source/blender/blenloader/intern/versioning_common.h | 1 - 1 file changed, 1 deletion(-) diff --git a/source/blender/blenloader/intern/versioning_common.h b/source/blender/blenloader/intern/versioning_common.h index 464bcdc6917..7b0a0282030 100644 --- a/source/blender/blenloader/intern/versioning_common.h +++ b/source/blender/blenloader/intern/versioning_common.h @@ -8,7 +8,6 @@ #ifdef __cplusplus # include "BLI_map.hh" -# include "BLI_span.hh" #endif struct ARegion; -- 2.30.2 From 2a68f6e154d3ff858991ffbf322836d2e996db40 Mon Sep 17 00:00:00 2001 From: illua1 Date: Fri, 7 Apr 2023 23:07:30 +0300 Subject: [PATCH 05/30] progress --- .../blenloader/intern/versioning_300.cc | 2 +- .../blenloader/intern/versioning_common.cc | 129 +++++++++++++----- .../nodes/geometry/nodes/node_geo_common.cc | 1 + 3 files changed, 96 insertions(+), 36 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_300.cc b/source/blender/blenloader/intern/versioning_300.cc index 7369da095a8..ca6ad045dfb 100644 --- a/source/blender/blenloader/intern/versioning_300.cc +++ b/source/blender/blenloader/intern/versioning_300.cc @@ -1348,7 +1348,7 @@ void do_versions_after_linking_300(FileData * /*fd*/, Main *bmain) FOREACH_NODETREE_END; } - if (!MAIN_VERSION_ATLEAST(bmain, 306, 1)) { + if (!MAIN_VERSION_ATLEAST(bmain, 306, 5)) { remove_legacy_instances_on(bmain, bmain->objects); } diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 5a2a0b4268e..d2b3a2430a5 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -8,6 +8,7 @@ #include +#include "DNA_modifier_types.h" #include "DNA_node_types.h" #include "DNA_object_types.h" #include "DNA_screen_types.h" @@ -21,10 +22,13 @@ #include "BLI_string_ref.hh" #include "BLI_vector.hh" +#include "NOD_socket.h" + #include "BKE_animsys.h" #include "BKE_lib_id.h" #include "BKE_main.h" #include "BKE_main_namemap.h" +#include "BKE_modifier.h" #include "BKE_node.h" #include "BKE_node_runtime.hh" @@ -295,42 +299,97 @@ namespace replace_legacy_instances { using namespace blender; -bNodeTree *childrens_combine_node_group(Main *bmain, - const Span objects, - const char *name) +static bNodeSocket &node_input_by_name(const StringRefNull name, bNode *node) { - const std::string tree_name = std::string(name) + std::string("_childrens"); - bNodeTree *node_tree = ntreeAddTree(bmain, tree_name.c_str(), "GeometryNodeTree"); - - ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Childrens"); - - bNode *join_objects = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_JOIN_GEOMETRY); - - bNodeSocket *to_join_objects = reinterpret_cast(join_objects->inputs.first); - - for (const Object *object : objects) { - bNode *object_info = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_OBJECT_INFO); - - bNodeSocket *from_object = reinterpret_cast(object_info->outputs.last); - - nodeAddLink(node_tree, object_info, from_object, join_objects, to_join_objects); + LISTBASE_FOREACH (bNodeSocket *, socket, &node->inputs) { + if (StringRefNull(socket->name) == name) { + return *socket; + } } + BLI_assert_unreachable(); + return *reinterpret_cast(node->inputs.first); +} + +static bNodeSocket &node_output_by_name(const StringRefNull name, bNode *node) +{ + LISTBASE_FOREACH (bNodeSocket *, socket, &node->outputs) { + if (StringRefNull(socket->name) == name) { + return *socket; + } + } + BLI_assert_unreachable(); + return *reinterpret_cast(node->outputs.first); +} + +static bNodeTree *builtin_instancing_node_group(Main *bmain, const bool /*on_vertices*/) +{ + bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Point Instances", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + + bke::node_field_inferencing::update_field_inferencing(*node_tree); return node_tree; } -bNodeTree *buildin_instancing_node_group(Main *bmain, const bool on_vertices) + +static bNodeTree *childrens_combine_node_group(Main *bmain, + bNodeTree *instancer_node_group, + const Span objects, + const StringRefNull name) { - return nullptr; -} -bNodeTree *instances_node_group(Main *bmain, - bNodeTree *children_combine_node_group, - bNodeTree *instancer_node_group) -{ - return nullptr; + const std::string tree_name = std::string(name) + std::string("_childrens"); + bNodeTree *node_tree = ntreeAddTree(bmain, tree_name.c_str(), "GeometryNodeTree"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Geometry"); + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Childrens"); + + bNode *group_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + bNodeSocket &input_geometry = node_output_by_name("Geometry", group_input); + + bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); + group->id = &instancer_node_group->id; + bke::node_field_inferencing::update_field_inferencing(*node_tree); + nodes::update_node_declaration_and_sockets(*node_tree, *group); + + bNodeSocket &instancer_geometry = node_input_by_name("Instancer", group); + nodeAddLink(node_tree, group_input, &input_geometry, group, &instancer_geometry); + + bNode *join_geometrys = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_JOIN_GEOMETRY); + + bNodeSocket &childrens_to_join = node_output_by_name("Geometry", join_geometrys); + bNodeSocket &group_instance_input = node_input_by_name("Instance", group); + nodeAddLink(node_tree, join_geometrys, &childrens_to_join, group, &group_instance_input); + + bNodeSocket &joined_childrens = node_input_by_name("Geometry", join_geometrys); + + for (Object *object : objects) { + bNode *object_info = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_OBJECT_INFO); + + bNodeSocket &object_input = node_input_by_name("Object", object_info); + bNodeSocket &as_instance = node_input_by_name("As Instance", object_info); + object_input.default_value_typed()->value = object; + as_instance.default_value_typed()->value = true; + + bNodeSocket &object_geometry = node_output_by_name("Geometry", object_info); + nodeAddLink(node_tree, object_info, &object_geometry, join_geometrys, &joined_childrens); + } + + bNode *group_output = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + bNodeSocket &instances = node_output_by_name("Instances", group); + bNodeSocket &childrens_output = node_input_by_name("Childrens", group_output); + nodeAddLink(node_tree, group_output, &childrens_output, group, &instances); + + // version_socket_update_is_used(node_tree); ?? + return node_tree; } -void object_push_instances_modifier(Main *bmain, Object *object, bNodeTree *node_tree) +static void object_push_instances_modifier(Main * /*bmain*/, Object *object, bNodeTree *node_tree) { + ModifierData *new_modifier = BKE_modifier_new(eModifierType_Nodes); + NodesModifierData &node_modifier = *reinterpret_cast(new_modifier); + node_modifier.node_group = node_tree; + BLI_addtail(&object->modifiers, new_modifier); } } // namespace replace_legacy_instances @@ -360,15 +419,15 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) /* Or on faces (OB_DUPLIFACES). */ const bool on_vertices = (parent->transflag & OB_DUPLIVERTS) != 0; - using namespace replace_legacy_instances; - bNodeTree *childrens_combine_node = childrens_combine_node_group( - bmain, objects, parent->id.name + 2); - bNodeTree *instancer_node = buildin_instancing_node_group(bmain, on_vertices); - bNodeTree *geometry_node_instances_group = instances_node_group( - bmain, childrens_combine_node, instancer_node); - object_push_instances_modifier(bmain, parent, geometry_node_instances_group); + const StringRefNull parent_name(parent->id.name + 2); - // parent->transflag = 0; + using namespace replace_legacy_instances; + bNodeTree *instancer_node_group = builtin_instancing_node_group(bmain, on_vertices); + bNodeTree *childrens_combine_node = childrens_combine_node_group( + bmain, instancer_node_group, objects, parent_name); + object_push_instances_modifier(bmain, parent, childrens_combine_node); + + parent->transflag = 0; } printf("HELLO!\n"); diff --git a/source/blender/nodes/geometry/nodes/node_geo_common.cc b/source/blender/nodes/geometry/nodes/node_geo_common.cc index 1803ac1f83d..667e2e15757 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_common.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_common.cc @@ -27,6 +27,7 @@ static void node_declare(const bNodeTree &node_tree, return; } + BLI_assert(group->runtime->field_inferencing_interface != nullptr); const FieldInferencingInterface &field_interface = *group->runtime->field_inferencing_interface; for (const int i : r_declaration.inputs.index_range()) { r_declaration.inputs[i]->input_field_type = field_interface.inputs[i]; -- 2.30.2 From 03040e1e5cf63ee8306b230b4a51bb4f032af4be Mon Sep 17 00:00:00 2001 From: illua1 Date: Sun, 16 Apr 2023 00:58:48 +0300 Subject: [PATCH 06/30] progress --- source/blender/blenlib/BLI_multi_value_map.hh | 5 + .../blenloader/intern/versioning_common.cc | 207 +++++++++++++++--- 2 files changed, 183 insertions(+), 29 deletions(-) diff --git a/source/blender/blenlib/BLI_multi_value_map.hh b/source/blender/blenlib/BLI_multi_value_map.hh index 99fc7dffe3b..004e0ba2e30 100644 --- a/source/blender/blenlib/BLI_multi_value_map.hh +++ b/source/blender/blenlib/BLI_multi_value_map.hh @@ -114,6 +114,11 @@ template class MultiValueMap { return {}; } + bool is_empty() const + { + return map_.is_empty(); + } + /** * Get the number of keys. */ diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index d2b3a2430a5..1917952778f 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -302,6 +302,9 @@ using namespace blender; static bNodeSocket &node_input_by_name(const StringRefNull name, bNode *node) { LISTBASE_FOREACH (bNodeSocket *, socket, &node->inputs) { + if (!socket->is_available()) { + continue; + } if (StringRefNull(socket->name) == name) { return *socket; } @@ -313,6 +316,9 @@ static bNodeSocket &node_input_by_name(const StringRefNull name, bNode *node) static bNodeSocket &node_output_by_name(const StringRefNull name, bNode *node) { LISTBASE_FOREACH (bNodeSocket *, socket, &node->outputs) { + if (!socket->is_available()) { + continue; + } if (StringRefNull(socket->name) == name) { return *socket; } @@ -321,65 +327,202 @@ static bNodeSocket &node_output_by_name(const StringRefNull name, bNode *node) return *reinterpret_cast(node->outputs.first); } -static bNodeTree *builtin_instancing_node_group(Main *bmain, const bool /*on_vertices*/) +struct RegularNodeTrees { + bNodeTree *instances_on_points = nullptr; + bNodeTree *instances_on_faces = nullptr; + + bNodeTree *view_geometry = nullptr; + + bNodeTree *face_scale = nullptr; + bNodeTree *face_aling = nullptr; +}; + +static bNodeTree *builtin_instancing_node_group(Main *bmain, + const bool /*on_vertices*/, + RegularNodeTrees &cached_node_trees) { + if (cached_node_trees.instances_on_points != nullptr) { + return cached_node_trees.instances_on_points; + } + bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Point Instances", "GeometryNodeTree"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Aling to Vertex Normal"); + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + bNode *normal_input = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_NORMAL); + + bNode *aling_y_z = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ALIGN_EULER_TO_VECTOR); + aling_y_z->custom1 = FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_Y; + aling_y_z->custom2 = FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_Z; + connect(normal_input, "Normal", aling_y_z, "Vector"); + + bNode *aling_y_x = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ALIGN_EULER_TO_VECTOR); + aling_y_x->custom1 = FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_Y; + aling_y_x->custom2 = FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_X; + connect(aling_y_z, "Rotation", aling_y_x, "Rotation"); + connect(normal_input, "Normal", aling_y_x, "Vector"); + + bNode *aling_selection_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *switch_vector = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SWITCH); + NodeSwitch &switch_data = *reinterpret_cast(switch_vector->storage); + switch_data.input_type = SOCK_VECTOR; + switch_vector->typeinfo->updatefunc(node_tree, switch_vector); + connect(aling_selection_in, "Aling to Vertex Normal", switch_vector, "Switch"); + connect(aling_y_x, "Rotation", switch_vector, "True"); + + bNode *instance_on_point = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INSTANCE_ON_POINTS); + connect(switch_vector, "Output", instance_on_point, "Rotation"); + + bNode *parent_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + connect(parent_in, "Instancer", instance_on_point, "Points"); + + bNode *geometry_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + connect(geometry_in, "Instance", instance_on_point, "Instance"); + + bNode *instances_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(instance_on_point, "Instances", instances_out, "Instances"); + bke::node_field_inferencing::update_field_inferencing(*node_tree); + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.instances_on_points = node_tree; + return node_tree; +} + +static bNodeTree *builtin_view_geometry_group(Main *bmain, RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.view_geometry != nullptr) { + return cached_node_trees.view_geometry; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, "ViewGeometry", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Geometry"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Viewport"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Render"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Geometry"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + bNode *is_viewport = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_IS_VIEWPORT); + + bNode *bool_invert = nodeAddStaticNode(nullptr, node_tree, FN_NODE_BOOLEAN_MATH); + bool_invert->custom1 = NODE_BOOLEAN_MATH_NOT; + bool_invert->typeinfo->updatefunc(node_tree, bool_invert); + connect(is_viewport, "Is Viewport", bool_invert, "Boolean"); + + bNode *viewport_render_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *viewport_render_switch = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SWITCH); + NodeSwitch &switch_data_vr = *reinterpret_cast(viewport_render_switch->storage); + switch_data_vr.input_type = SOCK_BOOLEAN; + viewport_render_switch->typeinfo->updatefunc(node_tree, viewport_render_switch); + connect(bool_invert, "Boolean", viewport_render_switch, "Switch"); + connect(viewport_render_in, "Viewport", viewport_render_switch, "False"); + connect(viewport_render_in, "Render", viewport_render_switch, "True"); + + bNode *geometry_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *geometry_switch = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SWITCH); + NodeSwitch &switch_data_g = *reinterpret_cast(geometry_switch->storage); + switch_data_g.input_type = SOCK_GEOMETRY; + geometry_switch->typeinfo->updatefunc(node_tree, geometry_switch); + connect(viewport_render_switch, "Output", geometry_switch, "Switch"); + connect(geometry_in, "Geometry", geometry_switch, "True"); + + bNode *geometry_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(geometry_switch, "Output", geometry_out, "Geometry"); + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.view_geometry = node_tree; return node_tree; } static bNodeTree *childrens_combine_node_group(Main *bmain, bNodeTree *instancer_node_group, const Span objects, - const StringRefNull name) + const StringRefNull name, + RegularNodeTrees &cached_node_trees) { const std::string tree_name = std::string(name) + std::string("_childrens"); bNodeTree *node_tree = ntreeAddTree(bmain, tree_name.c_str(), "GeometryNodeTree"); ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Geometry"); - ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Childrens"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Viewport"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Render"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Aling to Vertex Normal"); - bNode *group_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - bNodeSocket &input_geometry = node_output_by_name("Geometry", group_input); + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Geometry"); - bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); - group->id = &instancer_node_group->id; - bke::node_field_inferencing::update_field_inferencing(*node_tree); - nodes::update_node_declaration_and_sockets(*node_tree, *group); + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; - bNodeSocket &instancer_geometry = node_input_by_name("Instancer", group); - nodeAddLink(node_tree, group_input, &input_geometry, group, &instancer_geometry); + const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { + bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); + group->id = &tree->id; + bke::node_field_inferencing::update_field_inferencing(*node_tree); + nodes::update_node_declaration_and_sockets(*node_tree, *group); + return group; + }; bNode *join_geometrys = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_JOIN_GEOMETRY); - bNodeSocket &childrens_to_join = node_output_by_name("Geometry", join_geometrys); - bNodeSocket &group_instance_input = node_input_by_name("Instance", group); - nodeAddLink(node_tree, join_geometrys, &childrens_to_join, group, &group_instance_input); - - bNodeSocket &joined_childrens = node_input_by_name("Geometry", join_geometrys); - for (Object *object : objects) { bNode *object_info = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_OBJECT_INFO); - bNodeSocket &object_input = node_input_by_name("Object", object_info); bNodeSocket &as_instance = node_input_by_name("As Instance", object_info); object_input.default_value_typed()->value = object; as_instance.default_value_typed()->value = true; - - bNodeSocket &object_geometry = node_output_by_name("Geometry", object_info); - nodeAddLink(node_tree, object_info, &object_geometry, join_geometrys, &joined_childrens); + connect(object_info, "Geometry", join_geometrys, "Geometry"); } - bNode *group_output = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); - bNodeSocket &instances = node_output_by_name("Instances", group); - bNodeSocket &childrens_output = node_input_by_name("Childrens", group_output); - nodeAddLink(node_tree, group_output, &childrens_output, group, &instances); + bNode *instansing_group = add_node_group(instancer_node_group); + connect(join_geometrys, "Geometry", instansing_group, "Instance"); + bNode *parent_aling_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + connect(parent_aling_input, "Geometry", instansing_group, "Instancer"); + connect( + parent_aling_input, "Aling to Vertex Normal", instansing_group, "Aling to Vertex Normal"); + + bNode *view_switch_group = add_node_group(builtin_view_geometry_group(bmain, cached_node_trees)); + bNode *geometry_viewport_render_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + connect(geometry_viewport_render_input, "Geometry", view_switch_group, "Geometry"); + connect(geometry_viewport_render_input, "Viewport", view_switch_group, "Viewport"); + connect(geometry_viewport_render_input, "Render", view_switch_group, "Render"); + + bNode *join_parent = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_JOIN_GEOMETRY); + connect(instansing_group, "Instances", join_parent, "Geometry"); + connect(view_switch_group, "Geometry", join_parent, "Geometry"); + + bNode *group_output = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(join_parent, "Geometry", group_output, "Geometry"); // version_socket_update_is_used(node_tree); ?? return node_tree; } @@ -412,7 +555,12 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) parents_map.add(emitter, object); } - // Map + if (parents_map.is_empty()) { + return; + } + + /* Not a static. Pointers are valid only during one project session. */ + replace_legacy_instances::RegularNodeTrees cached_node_trees; for (auto &&[parent, objects] : parents_map.items()) { @@ -422,9 +570,10 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) const StringRefNull parent_name(parent->id.name + 2); using namespace replace_legacy_instances; - bNodeTree *instancer_node_group = builtin_instancing_node_group(bmain, on_vertices); + bNodeTree *instancer_node_group = builtin_instancing_node_group( + bmain, on_vertices, cached_node_trees); bNodeTree *childrens_combine_node = childrens_combine_node_group( - bmain, instancer_node_group, objects, parent_name); + bmain, instancer_node_group, objects, parent_name, cached_node_trees); object_push_instances_modifier(bmain, parent, childrens_combine_node); parent->transflag = 0; -- 2.30.2 From 55dddf2bdffb267b42aa20f7c9efaceead099f67 Mon Sep 17 00:00:00 2001 From: illua1 Date: Sun, 16 Apr 2023 01:56:42 +0300 Subject: [PATCH 07/30] progress --- .../blenloader/intern/versioning_common.cc | 157 +++++++++++++++++- 1 file changed, 149 insertions(+), 8 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 1917952778f..798b08723f3 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -299,6 +299,101 @@ namespace replace_legacy_instances { using namespace blender; +class bNodeTreeBuilder { + private: + bNodeTree &node_tree_; + + // TODO: Cache for lookup node sockets + // Alias structure on created nodes. Add methods on it + + public: + bNodeTreeBuilder(bNodeTree &tree) : node_tree_(tree) {} + bNodeTreeBuilder(const StringRefNull type, const StringRefNull name, Main *bmain) + : bNodeTreeBuilder(*ntreeAddTree(bmain, type.data(), name.data())) + { + } + + bNode &tree_socket(const eNodeSocketInOut in_out, + const StringRefNull type, + const StringRefNull name) + { + ntreeAddSocketInterface(&node_tree_, in_out, type.data(), name.data()); + } + + bNode &new_node(const int type) + { + return *nodeAddStaticNode(nullptr, &node_tree_, type); + } + + template + bNode &new_node(const int type, Func for_storage) + { + bNode &node = this->new_node(type); + NodeCustomData &data = *reinterpret_cast(node.storage); + for_storage(data); + this->node_update(node); + } + + void node_update(bNode &node) + { + node.typeinfo->updatefunc(&node_tree_, &node); + } + + void node_refresh(bNode &node) + { + bke::node_field_inferencing::update_field_inferencing(node_tree_); + nodes::update_node_declaration_and_sockets(node_tree_, node); + } + + bNodeSocket &node_input_by_name(const StringRefNull name, bNode &node) + { + LISTBASE_FOREACH (bNodeSocket *, socket, &node.inputs) { + if (!socket->is_available()) { + continue; + } + if (StringRefNull(socket->name) == name) { + return *socket; + } + } + BLI_assert_unreachable(); + return *reinterpret_cast(node.inputs.first); + } + + bNodeSocket &node_output_by_name(const StringRefNull name, bNode &node) + { + LISTBASE_FOREACH (bNodeSocket *, socket, &node.outputs) { + if (!socket->is_available()) { + continue; + } + if (StringRefNull(socket->name) == name) { + return *socket; + } + } + BLI_assert_unreachable(); + return *reinterpret_cast(node.outputs.first); + } + + void connect(bNode &node_out, + const StringRefNull name_out, + bNode &node_in, + const StringRefNull name_in) + { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(&node_tree_, &node_out, &out, &node_in, &in); + } + + void node_offsets() + { + // TODO + } + + void hide_all_unuseds() + { + // TODO + } +}; + static bNodeSocket &node_input_by_name(const StringRefNull name, bNode *node) { LISTBASE_FOREACH (bNodeSocket *, socket, &node->inputs) { @@ -337,14 +432,8 @@ struct RegularNodeTrees { bNodeTree *face_aling = nullptr; }; -static bNodeTree *builtin_instancing_node_group(Main *bmain, - const bool /*on_vertices*/, - RegularNodeTrees &cached_node_trees) +static bNodeTree *builtin_instances_on_points(Main *bmain) { - if (cached_node_trees.instances_on_points != nullptr) { - return cached_node_trees.instances_on_points; - } - bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Point Instances", "GeometryNodeTree"); ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); @@ -399,10 +488,62 @@ static bNodeTree *builtin_instancing_node_group(Main *bmain, bke::node_field_inferencing::update_field_inferencing(*node_tree); bke::node_field_inferencing::update_field_inferencing(*node_tree); - cached_node_trees.instances_on_points = node_tree; return node_tree; } +static bNodeTree *builtin_instances_on_faces(Main *bmain) +{ + bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Point Instances", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Aling to Vertex Normal"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { + bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); + group->id = &tree->id; + bke::node_field_inferencing::update_field_inferencing(*node_tree); + nodes::update_node_declaration_and_sockets(*node_tree, *group); + return group; + }; + + return node_tree; +} + +static bNodeTree *builtin_instancing_node_group(Main *bmain, + const bool on_vertices, + RegularNodeTrees &cached_node_trees) +{ + if (on_vertices) { + if (cached_node_trees.instances_on_points != nullptr) { + return cached_node_trees.instances_on_points; + } + cached_node_trees.instances_on_points = builtin_instances_on_points(bmain); + return cached_node_trees.instances_on_points; + } + + { + if (cached_node_trees.instances_on_faces != nullptr) { + return cached_node_trees.instances_on_faces; + } + cached_node_trees.instances_on_faces = builtin_instances_on_faces(bmain); + return cached_node_trees.instances_on_faces; + } + + return nullptr; +} + static bNodeTree *builtin_view_geometry_group(Main *bmain, RegularNodeTrees &cached_node_trees) { if (cached_node_trees.view_geometry != nullptr) { -- 2.30.2 From 001414ffc60682fadc8f13199cfe3f1c5170bc2d Mon Sep 17 00:00:00 2001 From: illua1 Date: Sun, 16 Apr 2023 22:08:25 +0300 Subject: [PATCH 08/30] progress --- .../blenloader/intern/versioning_common.cc | 591 +++++++++++------- 1 file changed, 359 insertions(+), 232 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 798b08723f3..4125701119c 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -25,6 +25,7 @@ #include "NOD_socket.h" #include "BKE_animsys.h" +#include "BKE_attribute.h" #include "BKE_lib_id.h" #include "BKE_main.h" #include "BKE_main_namemap.h" @@ -299,101 +300,6 @@ namespace replace_legacy_instances { using namespace blender; -class bNodeTreeBuilder { - private: - bNodeTree &node_tree_; - - // TODO: Cache for lookup node sockets - // Alias structure on created nodes. Add methods on it - - public: - bNodeTreeBuilder(bNodeTree &tree) : node_tree_(tree) {} - bNodeTreeBuilder(const StringRefNull type, const StringRefNull name, Main *bmain) - : bNodeTreeBuilder(*ntreeAddTree(bmain, type.data(), name.data())) - { - } - - bNode &tree_socket(const eNodeSocketInOut in_out, - const StringRefNull type, - const StringRefNull name) - { - ntreeAddSocketInterface(&node_tree_, in_out, type.data(), name.data()); - } - - bNode &new_node(const int type) - { - return *nodeAddStaticNode(nullptr, &node_tree_, type); - } - - template - bNode &new_node(const int type, Func for_storage) - { - bNode &node = this->new_node(type); - NodeCustomData &data = *reinterpret_cast(node.storage); - for_storage(data); - this->node_update(node); - } - - void node_update(bNode &node) - { - node.typeinfo->updatefunc(&node_tree_, &node); - } - - void node_refresh(bNode &node) - { - bke::node_field_inferencing::update_field_inferencing(node_tree_); - nodes::update_node_declaration_and_sockets(node_tree_, node); - } - - bNodeSocket &node_input_by_name(const StringRefNull name, bNode &node) - { - LISTBASE_FOREACH (bNodeSocket *, socket, &node.inputs) { - if (!socket->is_available()) { - continue; - } - if (StringRefNull(socket->name) == name) { - return *socket; - } - } - BLI_assert_unreachable(); - return *reinterpret_cast(node.inputs.first); - } - - bNodeSocket &node_output_by_name(const StringRefNull name, bNode &node) - { - LISTBASE_FOREACH (bNodeSocket *, socket, &node.outputs) { - if (!socket->is_available()) { - continue; - } - if (StringRefNull(socket->name) == name) { - return *socket; - } - } - BLI_assert_unreachable(); - return *reinterpret_cast(node.outputs.first); - } - - void connect(bNode &node_out, - const StringRefNull name_out, - bNode &node_in, - const StringRefNull name_in) - { - bNodeSocket &out = node_output_by_name(name_out, node_out); - bNodeSocket &in = node_input_by_name(name_in, node_in); - nodeAddLink(&node_tree_, &node_out, &out, &node_in, &in); - } - - void node_offsets() - { - // TODO - } - - void hide_all_unuseds() - { - // TODO - } -}; - static bNodeSocket &node_input_by_name(const StringRefNull name, bNode *node) { LISTBASE_FOREACH (bNodeSocket *, socket, &node->inputs) { @@ -430,121 +336,10 @@ struct RegularNodeTrees { bNodeTree *face_scale = nullptr; bNodeTree *face_aling = nullptr; + bNodeTree *first_face_tris_points = nullptr; }; -static bNodeTree *builtin_instances_on_points(Main *bmain) -{ - bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Point Instances", "GeometryNodeTree"); - - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Aling to Vertex Normal"); - - ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); - - const auto connect = [node_tree](bNode *node_out, - const StringRefNull name_out, - bNode *node_in, - const StringRefNull name_in) { - bNodeSocket &out = node_output_by_name(name_out, node_out); - bNodeSocket &in = node_input_by_name(name_in, node_in); - nodeAddLink(node_tree, node_out, &out, node_in, &in); - }; - - bNode *normal_input = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_NORMAL); - - bNode *aling_y_z = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ALIGN_EULER_TO_VECTOR); - aling_y_z->custom1 = FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_Y; - aling_y_z->custom2 = FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_Z; - connect(normal_input, "Normal", aling_y_z, "Vector"); - - bNode *aling_y_x = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ALIGN_EULER_TO_VECTOR); - aling_y_x->custom1 = FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_Y; - aling_y_x->custom2 = FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_X; - connect(aling_y_z, "Rotation", aling_y_x, "Rotation"); - connect(normal_input, "Normal", aling_y_x, "Vector"); - - bNode *aling_selection_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - - bNode *switch_vector = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SWITCH); - NodeSwitch &switch_data = *reinterpret_cast(switch_vector->storage); - switch_data.input_type = SOCK_VECTOR; - switch_vector->typeinfo->updatefunc(node_tree, switch_vector); - connect(aling_selection_in, "Aling to Vertex Normal", switch_vector, "Switch"); - connect(aling_y_x, "Rotation", switch_vector, "True"); - - bNode *instance_on_point = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INSTANCE_ON_POINTS); - connect(switch_vector, "Output", instance_on_point, "Rotation"); - - bNode *parent_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - connect(parent_in, "Instancer", instance_on_point, "Points"); - - bNode *geometry_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - connect(geometry_in, "Instance", instance_on_point, "Instance"); - - bNode *instances_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); - connect(instance_on_point, "Instances", instances_out, "Instances"); - - bke::node_field_inferencing::update_field_inferencing(*node_tree); - - bke::node_field_inferencing::update_field_inferencing(*node_tree); - return node_tree; -} - -static bNodeTree *builtin_instances_on_faces(Main *bmain) -{ - bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Point Instances", "GeometryNodeTree"); - - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Aling to Vertex Normal"); - - ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); - - const auto connect = [node_tree](bNode *node_out, - const StringRefNull name_out, - bNode *node_in, - const StringRefNull name_in) { - bNodeSocket &out = node_output_by_name(name_out, node_out); - bNodeSocket &in = node_input_by_name(name_in, node_in); - nodeAddLink(node_tree, node_out, &out, node_in, &in); - }; - - const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { - bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); - group->id = &tree->id; - bke::node_field_inferencing::update_field_inferencing(*node_tree); - nodes::update_node_declaration_and_sockets(*node_tree, *group); - return group; - }; - - return node_tree; -} - -static bNodeTree *builtin_instancing_node_group(Main *bmain, - const bool on_vertices, - RegularNodeTrees &cached_node_trees) -{ - if (on_vertices) { - if (cached_node_trees.instances_on_points != nullptr) { - return cached_node_trees.instances_on_points; - } - cached_node_trees.instances_on_points = builtin_instances_on_points(bmain); - return cached_node_trees.instances_on_points; - } - - { - if (cached_node_trees.instances_on_faces != nullptr) { - return cached_node_trees.instances_on_faces; - } - cached_node_trees.instances_on_faces = builtin_instances_on_faces(bmain); - return cached_node_trees.instances_on_faces; - } - - return nullptr; -} - -static bNodeTree *builtin_view_geometry_group(Main *bmain, RegularNodeTrees &cached_node_trees) +static bNodeTree *view_geometry_tree(Main *bmain, RegularNodeTrees &cached_node_trees) { if (cached_node_trees.view_geometry != nullptr) { return cached_node_trees.view_geometry; @@ -601,11 +396,279 @@ static bNodeTree *builtin_view_geometry_group(Main *bmain, RegularNodeTrees &cac return node_tree; } -static bNodeTree *childrens_combine_node_group(Main *bmain, - bNodeTree *instancer_node_group, - const Span objects, - const StringRefNull name, - RegularNodeTrees &cached_node_trees) +static bNodeTree *instances_on_points_tree(Main *bmain, RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.instances_on_points != nullptr) { + return cached_node_trees.instances_on_points; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Point Instances", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Aling to Vertex Normal"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + bNode *normal_input = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_NORMAL); + + bNode *aling_y_z = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ALIGN_EULER_TO_VECTOR); + aling_y_z->custom1 = FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_Y; + aling_y_z->custom2 = FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_Z; + connect(normal_input, "Normal", aling_y_z, "Vector"); + + bNode *aling_y_x = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ALIGN_EULER_TO_VECTOR); + aling_y_x->custom1 = FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_Y; + aling_y_x->custom2 = FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_X; + connect(aling_y_z, "Rotation", aling_y_x, "Rotation"); + connect(normal_input, "Normal", aling_y_x, "Vector"); + + bNode *aling_selection_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *switch_vector = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SWITCH); + NodeSwitch &switch_data = *reinterpret_cast(switch_vector->storage); + switch_data.input_type = SOCK_VECTOR; + switch_vector->typeinfo->updatefunc(node_tree, switch_vector); + connect(aling_selection_in, "Aling to Vertex Normal", switch_vector, "Switch"); + connect(aling_y_x, "Rotation", switch_vector, "True"); + + bNode *instance_on_point = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INSTANCE_ON_POINTS); + connect(switch_vector, "Output", instance_on_point, "Rotation"); + + bNode *parent_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + connect(parent_in, "Instancer", instance_on_point, "Points"); + + bNode *geometry_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + connect(geometry_in, "Instance", instance_on_point, "Instance"); + + bNode *instances_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(instance_on_point, "Instances", instances_out, "Instances"); + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.instances_on_points = node_tree; + return node_tree; +} + +static bNodeTree *first_face_tris_points_tree(Main *bmain, RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.first_face_tris_points != nullptr) { + return cached_node_trees.first_face_tris_points; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, ".First Face Tris Points", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketInt", "Vertex Index 1"); + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketInt", "Vertex Index 2"); + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketInt", "Vertex Index 3"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + bNode *out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + + bNode *verfex_of_corner_1 = nodeAddStaticNode( + nullptr, node_tree, GEO_NODE_MESH_TOPOLOGY_VERTEX_OF_CORNER); + connect(verfex_of_corner_1, "Vertex Index", out, "Vertex Index 1"); + bNode *corner_of_face_1 = nodeAddStaticNode( + nullptr, node_tree, GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_FACE); + connect(corner_of_face_1, "Corner Index", verfex_of_corner_1, "Corner Index"); + node_input_by_name("Sort Index", corner_of_face_1) + .default_value_typed() + ->value = 0; + + bNode *verfex_of_corner_2 = nodeAddStaticNode( + nullptr, node_tree, GEO_NODE_MESH_TOPOLOGY_VERTEX_OF_CORNER); + connect(verfex_of_corner_2, "Vertex Index", out, "Vertex Index 2"); + bNode *corner_of_face_2 = nodeAddStaticNode( + nullptr, node_tree, GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_FACE); + connect(corner_of_face_2, "Corner Index", verfex_of_corner_2, "Corner Index"); + node_input_by_name("Sort Index", corner_of_face_2) + .default_value_typed() + ->value = 1; + + bNode *verfex_of_corner_3 = nodeAddStaticNode( + nullptr, node_tree, GEO_NODE_MESH_TOPOLOGY_VERTEX_OF_CORNER); + connect(verfex_of_corner_3, "Vertex Index", out, "Vertex Index 3"); + bNode *corner_of_face_3 = nodeAddStaticNode( + nullptr, node_tree, GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_FACE); + connect(corner_of_face_3, "Corner Index", verfex_of_corner_3, "Corner Index"); + node_input_by_name("Sort Index", corner_of_face_3) + .default_value_typed() + ->value = 2; + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.first_face_tris_points = node_tree; + return node_tree; +} + +static bNodeTree *face_aling_tree(Main *bmain, RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.face_aling != nullptr) { + return cached_node_trees.face_aling; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Face Instances", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { + bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); + group->id = &tree->id; + bke::node_field_inferencing::update_field_inferencing(*node_tree); + nodes::update_node_declaration_and_sockets(*node_tree, *group); + return group; + }; + + bNode *triangle_indices_group = add_node_group( + first_face_tris_points_tree(bmain, cached_node_trees)); + + bNode *position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); + + bNode *at_index_1 = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_EVALUATE_AT_INDEX); + at_index_1->custom1 = ATTR_DOMAIN_POINT; + at_index_1->custom2 = CD_PROP_FLOAT3; + at_index_1->typeinfo->updatefunc(node_tree, at_index_1); + connect(triangle_indices_group, "Vertex Index 1", at_index_1, "Index"); + connect(position, "Position", at_index_1, "Value"); + + bNode *at_index_2 = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_EVALUATE_AT_INDEX); + at_index_2->custom1 = ATTR_DOMAIN_POINT; + at_index_2->custom2 = CD_PROP_FLOAT3; + at_index_2->typeinfo->updatefunc(node_tree, at_index_2); + connect(triangle_indices_group, "Vertex Index 2", at_index_2, "Index"); + connect(position, "Position", at_index_2, "Value"); + + bNode *at_index_3 = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_EVALUATE_AT_INDEX); + at_index_3->custom1 = ATTR_DOMAIN_POINT; + at_index_3->custom2 = CD_PROP_FLOAT3; + at_index_3->typeinfo->updatefunc(node_tree, at_index_3); + connect(triangle_indices_group, "Vertex Index 3", at_index_3, "Index"); + connect(position, "Position", at_index_3, "Value"); + + bNode *vector_substruct_1 = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + vector_substruct_1->custom1 = NODE_VECTOR_MATH_SUBTRACT; + vector_substruct_1->typeinfo->updatefunc(node_tree, vector_substruct_1); + // connect(at_index_2, "Value", at_index_3, "Index"); + // connect(at_index_3, "Value", at_index_3, "Index"); + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.face_aling = node_tree; + return node_tree; +} + +static bNodeTree *faces_scale_tree(Main *bmain, RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.face_scale != nullptr) { + return cached_node_trees.face_scale; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Face Instances", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.face_scale = node_tree; + return node_tree; +} + +static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.instances_on_faces != nullptr) { + return cached_node_trees.instances_on_faces; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Face Instances", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { + bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); + group->id = &tree->id; + bke::node_field_inferencing::update_field_inferencing(*node_tree); + nodes::update_node_declaration_and_sockets(*node_tree, *group); + return group; + }; + + bNode *triangle_indices_group = add_node_group(face_aling_tree(bmain, cached_node_trees)); + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.instances_on_faces = node_tree; + return node_tree; +} + +static bNode *join_objects_as_instances(const Span objects, bNodeTree *node_tree) +{ + bNode *join_geometrys = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_JOIN_GEOMETRY); + bNodeSocket &in = node_input_by_name("Geometry", join_geometrys); + + for (Object *object : objects) { + bNode *object_info = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_OBJECT_INFO); + bNodeSocket &object_input = node_input_by_name("Object", object_info); + bNodeSocket &as_instance = node_input_by_name("As Instance", object_info); + object_input.default_value_typed()->value = object; + as_instance.default_value_typed()->value = true; + + bNodeSocket &out = node_output_by_name("Geometry", object_info); + nodeAddLink(node_tree, object_info, &out, join_geometrys, &in); + } + + return join_geometrys; +} + +static bNodeTree *instances_on_points(const Span objects, + const StringRefNull name, + Main *bmain, + RegularNodeTrees &cached_node_trees) { const std::string tree_name = std::string(name) + std::string("_childrens"); bNodeTree *node_tree = ntreeAddTree(bmain, tree_name.c_str(), "GeometryNodeTree"); @@ -633,18 +696,9 @@ static bNodeTree *childrens_combine_node_group(Main *bmain, return group; }; - bNode *join_geometrys = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_JOIN_GEOMETRY); + bNode *join_geometrys = join_objects_as_instances(objects, node_tree); - for (Object *object : objects) { - bNode *object_info = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_OBJECT_INFO); - bNodeSocket &object_input = node_input_by_name("Object", object_info); - bNodeSocket &as_instance = node_input_by_name("As Instance", object_info); - object_input.default_value_typed()->value = object; - as_instance.default_value_typed()->value = true; - connect(object_info, "Geometry", join_geometrys, "Geometry"); - } - - bNode *instansing_group = add_node_group(instancer_node_group); + bNode *instansing_group = add_node_group(instances_on_points_tree(bmain, cached_node_trees)); connect(join_geometrys, "Geometry", instansing_group, "Instance"); bNode *parent_aling_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); @@ -652,7 +706,7 @@ static bNodeTree *childrens_combine_node_group(Main *bmain, connect( parent_aling_input, "Aling to Vertex Normal", instansing_group, "Aling to Vertex Normal"); - bNode *view_switch_group = add_node_group(builtin_view_geometry_group(bmain, cached_node_trees)); + bNode *view_switch_group = add_node_group(view_geometry_tree(bmain, cached_node_trees)); bNode *geometry_viewport_render_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); connect(geometry_viewport_render_input, "Geometry", view_switch_group, "Geometry"); connect(geometry_viewport_render_input, "Viewport", view_switch_group, "Viewport"); @@ -662,14 +716,84 @@ static bNodeTree *childrens_combine_node_group(Main *bmain, connect(instansing_group, "Instances", join_parent, "Geometry"); connect(view_switch_group, "Geometry", join_parent, "Geometry"); + bNode *realize_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_REALIZE_INSTANCES); + connect(join_parent, "Geometry", realize_instances, "Geometry"); + bNode *group_output = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); - connect(join_parent, "Geometry", group_output, "Geometry"); - // version_socket_update_is_used(node_tree); ?? + connect(realize_instances, "Geometry", group_output, "Geometry"); + + return node_tree; +} + +static bNodeTree *instances_on_faces(const Span objects, + const StringRefNull name, + Main *bmain, + RegularNodeTrees &cached_node_trees) +{ + const std::string tree_name = std::string(name) + std::string("_childrens"); + bNodeTree *node_tree = ntreeAddTree(bmain, tree_name.c_str(), "GeometryNodeTree"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Geometry"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Viewport"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Render"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Scale by Face Size"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketFloat", "Factor"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Geometry"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { + bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); + group->id = &tree->id; + bke::node_field_inferencing::update_field_inferencing(*node_tree); + nodes::update_node_declaration_and_sockets(*node_tree, *group); + return group; + }; + + bNode *join_geometrys = join_objects_as_instances(objects, node_tree); + + bNode *scale_factor_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *scale_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SCALE_INSTANCES); + connect(join_geometrys, "Geometry", scale_instances, "Instances"); + connect(scale_factor_input, "Scale by Face Size", scale_instances, "Selection"); + connect(scale_factor_input, "Factor", scale_instances, "Scale"); + + bNode *instansing_group = add_node_group(instances_on_faces_tree(bmain, cached_node_trees)); + connect(scale_instances, "Instances", instansing_group, "Instance"); + + bNode *geometry_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + connect(geometry_input, "Geometry", instansing_group, "Instancer"); + + bNode *geometry_viewport_render_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *view_switch_group = add_node_group(view_geometry_tree(bmain, cached_node_trees)); + connect(geometry_viewport_render_input, "Geometry", view_switch_group, "Geometry"); + connect(geometry_viewport_render_input, "Viewport", view_switch_group, "Viewport"); + connect(geometry_viewport_render_input, "Render", view_switch_group, "Render"); + + bNode *join_parent = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_JOIN_GEOMETRY); + connect(instansing_group, "Instances", join_parent, "Geometry"); + connect(view_switch_group, "Geometry", join_parent, "Geometry"); + + bNode *realize_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_REALIZE_INSTANCES); + connect(join_parent, "Geometry", realize_instances, "Geometry"); + + bNode *group_output = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(realize_instances, "Geometry", group_output, "Geometry"); return node_tree; } static void object_push_instances_modifier(Main * /*bmain*/, Object *object, bNodeTree *node_tree) { + /* TODO: Move setings from object properties in modifier panel. */ ModifierData *new_modifier = BKE_modifier_new(eModifierType_Nodes); NodesModifierData &node_modifier = *reinterpret_cast(new_modifier); node_modifier.node_group = node_tree; @@ -711,11 +835,14 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) const StringRefNull parent_name(parent->id.name + 2); using namespace replace_legacy_instances; - bNodeTree *instancer_node_group = builtin_instancing_node_group( - bmain, on_vertices, cached_node_trees); - bNodeTree *childrens_combine_node = childrens_combine_node_group( - bmain, instancer_node_group, objects, parent_name, cached_node_trees); - object_push_instances_modifier(bmain, parent, childrens_combine_node); + bNodeTree *instances_node_tree; + if (on_vertices) { + instances_node_tree = instances_on_points(objects, parent_name, bmain, cached_node_trees); + } + else { + instances_node_tree = instances_on_faces(objects, parent_name, bmain, cached_node_trees); + } + object_push_instances_modifier(bmain, parent, instances_node_tree); parent->transflag = 0; } -- 2.30.2 From 05eddb8acb2a54256cae83297ca9f447e1f93862 Mon Sep 17 00:00:00 2001 From: illua1 Date: Sun, 16 Apr 2023 23:16:17 +0300 Subject: [PATCH 09/30] progress --- .../blenloader/intern/versioning_common.cc | 127 ++++++++++++++++-- 1 file changed, 116 insertions(+), 11 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 4125701119c..b37de5c96a9 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -310,6 +310,14 @@ static bNodeSocket &node_input_by_name(const StringRefNull name, bNode *node) return *socket; } } + LISTBASE_FOREACH (bNodeSocket *, socket, &node->inputs) { + if (!socket->is_available()) { + continue; + } + if (StringRefNull(socket->identifier) == name) { + return *socket; + } + } BLI_assert_unreachable(); return *reinterpret_cast(node->inputs.first); } @@ -324,6 +332,14 @@ static bNodeSocket &node_output_by_name(const StringRefNull name, bNode *node) return *socket; } } + LISTBASE_FOREACH (bNodeSocket *, socket, &node->outputs) { + if (!socket->is_available()) { + continue; + } + if (StringRefNull(socket->identifier) == name) { + return *socket; + } + } BLI_assert_unreachable(); return *reinterpret_cast(node->outputs.first); } @@ -521,12 +537,12 @@ static bNodeTree *face_aling_tree(Main *bmain, RegularNodeTrees &cached_node_tre return cached_node_trees.face_aling; } - bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Face Instances", "GeometryNodeTree"); + bNodeTree *node_tree = ntreeAddTree(bmain, "Face Aling", "GeometryNodeTree"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Geometry"); - ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Geometry"); + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketVector", "Rotation"); const auto connect = [node_tree](bNode *node_out, const StringRefNull name_out, @@ -574,8 +590,52 @@ static bNodeTree *face_aling_tree(Main *bmain, RegularNodeTrees &cached_node_tre bNode *vector_substruct_1 = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); vector_substruct_1->custom1 = NODE_VECTOR_MATH_SUBTRACT; vector_substruct_1->typeinfo->updatefunc(node_tree, vector_substruct_1); - // connect(at_index_2, "Value", at_index_3, "Index"); - // connect(at_index_3, "Value", at_index_3, "Index"); + connect(at_index_2, "Value", vector_substruct_1, "Vector_001"); + connect(at_index_3, "Value", vector_substruct_1, "Vector"); + + bNode *vector_substruct_2 = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + vector_substruct_2->custom1 = NODE_VECTOR_MATH_SUBTRACT; + vector_substruct_2->typeinfo->updatefunc(node_tree, vector_substruct_2); + connect(at_index_1, "Value", vector_substruct_2, "Vector_001"); + connect(at_index_2, "Value", vector_substruct_2, "Vector"); + + bNode *vector_cros_prod_1 = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + vector_cros_prod_1->custom1 = NODE_VECTOR_MATH_CROSS_PRODUCT; + vector_cros_prod_1->typeinfo->updatefunc(node_tree, vector_cros_prod_1); + connect(vector_substruct_2, "Vector", vector_cros_prod_1, "Vector"); + connect(vector_substruct_1, "Vector", vector_cros_prod_1, "Vector_001"); + + bNode *vector_cros_prod_2 = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + vector_cros_prod_2->custom1 = NODE_VECTOR_MATH_CROSS_PRODUCT; + vector_cros_prod_2->typeinfo->updatefunc(node_tree, vector_cros_prod_2); + connect(vector_cros_prod_1, "Vector", vector_cros_prod_2, "Vector"); + connect(vector_substruct_2, "Vector", vector_cros_prod_2, "Vector_001"); + + bNode *aling_z_auto = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ALIGN_EULER_TO_VECTOR); + aling_z_auto->custom1 = FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_Z; + aling_z_auto->custom2 = FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_AUTO; + connect(vector_cros_prod_1, "Vector", aling_z_auto, "Vector"); + + bNode *aling_y_auto = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ALIGN_EULER_TO_VECTOR); + aling_y_auto->custom1 = FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_Y; + aling_y_auto->custom2 = FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_AUTO; + connect(aling_z_auto, "Rotation", aling_y_auto, "Rotation"); + connect(vector_cros_prod_2, "Vector", aling_y_auto, "Vector"); + + bNode *in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *capture_result = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_CAPTURE_ATTRIBUTE); + NodeGeometryAttributeCapture &capture_storage = + *reinterpret_cast(capture_result->storage); + capture_storage.domain = ATTR_DOMAIN_FACE; + capture_storage.data_type = CD_PROP_FLOAT3; + capture_result->typeinfo->updatefunc(node_tree, capture_result); + connect(in, "Geometry", capture_result, "Geometry"); + connect(aling_y_auto, "Rotation", capture_result, "Value"); + + bNode *out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(capture_result, "Geometry", out, "Geometry"); + connect(capture_result, "Attribute", out, "Rotation"); bke::node_field_inferencing::update_field_inferencing(*node_tree); cached_node_trees.face_aling = node_tree; @@ -588,12 +648,12 @@ static bNodeTree *faces_scale_tree(Main *bmain, RegularNodeTrees &cached_node_tr return cached_node_trees.face_scale; } - bNodeTree *node_tree = ntreeAddTree(bmain, "Legacy Face Instances", "GeometryNodeTree"); + bNodeTree *node_tree = ntreeAddTree(bmain, "Face Size", "GeometryNodeTree"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instance"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instancer"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Geometry"); - ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Geometry"); + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketFloat", "Size"); const auto connect = [node_tree](bNode *node_out, const StringRefNull name_out, @@ -604,6 +664,28 @@ static bNodeTree *faces_scale_tree(Main *bmain, RegularNodeTrees &cached_node_tr nodeAddLink(node_tree, node_out, &out, node_in, &in); }; + bNode *face_area_input = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_MESH_FACE_AREA); + + bNode *math_sqrt = nodeAddStaticNode(nullptr, node_tree, SH_NODE_MATH); + math_sqrt->custom1 = NODE_MATH_SQRT; + math_sqrt->typeinfo->updatefunc(node_tree, math_sqrt); + connect(face_area_input, "Area", math_sqrt, "Value"); + + bNode *in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *capture_result = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_CAPTURE_ATTRIBUTE); + NodeGeometryAttributeCapture &capture_storage = + *reinterpret_cast(capture_result->storage); + capture_storage.domain = ATTR_DOMAIN_FACE; + capture_storage.data_type = CD_PROP_FLOAT; + capture_result->typeinfo->updatefunc(node_tree, capture_result); + connect(in, "Geometry", capture_result, "Geometry"); + connect(math_sqrt, "Value", capture_result, "Value"); + + bNode *out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(capture_result, "Geometry", out, "Geometry"); + connect(capture_result, "Attribute", out, "Size"); + bke::node_field_inferencing::update_field_inferencing(*node_tree); cached_node_trees.face_scale = node_tree; return node_tree; @@ -639,7 +721,30 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ return group; }; - bNode *triangle_indices_group = add_node_group(face_aling_tree(bmain, cached_node_trees)); + bNode *instancer_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *face_size_group = add_node_group(faces_scale_tree(bmain, cached_node_trees)); + connect(instancer_in, "Instancer", face_size_group, "Geometry"); + + bNode *face_aling_group = add_node_group(face_aling_tree(bmain, cached_node_trees)); + connect(face_size_group, "Geometry", face_aling_group, "Geometry"); + + bNode *mesh_to_points = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_MESH_TO_POINTS); + NodeGeometryMeshToPoints &points_storage = *reinterpret_cast( + mesh_to_points->storage); + points_storage.mode = GEO_NODE_MESH_TO_POINTS_FACES; + connect(face_aling_group, "Geometry", mesh_to_points, "Mesh"); + + bNode *instance_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *instance_on_points = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INSTANCE_ON_POINTS); + connect(mesh_to_points, "Points", instance_on_points, "Points"); + connect(instance_in, "Instance", instance_on_points, "Instance"); + connect(face_aling_group, "Rotation", instance_on_points, "Rotation"); + connect(face_size_group, "Size", instance_on_points, "Scale"); + + bNode *out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(instance_on_points, "Instances", out, "Instances"); bke::node_field_inferencing::update_field_inferencing(*node_tree); cached_node_trees.instances_on_faces = node_tree; -- 2.30.2 From b5ac935726fb3c10c32f3fcf8f59d8d334cd49e8 Mon Sep 17 00:00:00 2001 From: illua1 Date: Mon, 17 Apr 2023 18:10:52 +0300 Subject: [PATCH 10/30] progress --- .../blenloader/intern/versioning_common.cc | 48 +++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index b37de5c96a9..cee1bda44ce 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -22,6 +22,8 @@ #include "BLI_string_ref.hh" #include "BLI_vector.hh" +#include "BLT_translation.h" + #include "NOD_socket.h" #include "BKE_animsys.h" @@ -32,6 +34,7 @@ #include "BKE_modifier.h" #include "BKE_node.h" #include "BKE_node_runtime.hh" +#include "BKE_object.h" #include "MEM_guardedalloc.h" @@ -556,6 +559,7 @@ static bNodeTree *face_aling_tree(Main *bmain, RegularNodeTrees &cached_node_tre const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); group->id = &tree->id; + id_us_plus(&tree->id); bke::node_field_inferencing::update_field_inferencing(*node_tree); nodes::update_node_declaration_and_sockets(*node_tree, *group); return group; @@ -716,6 +720,7 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); group->id = &tree->id; + id_us_plus(&tree->id); bke::node_field_inferencing::update_field_inferencing(*node_tree); nodes::update_node_declaration_and_sockets(*node_tree, *group); return group; @@ -775,8 +780,8 @@ static bNodeTree *instances_on_points(const Span objects, Main *bmain, RegularNodeTrees &cached_node_trees) { - const std::string tree_name = std::string(name) + std::string("_childrens"); - bNodeTree *node_tree = ntreeAddTree(bmain, tree_name.c_str(), "GeometryNodeTree"); + bNodeTree *node_tree = ntreeAddTree(bmain, name.c_str(), "GeometryNodeTree"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Geometry"); ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Viewport"); ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Render"); @@ -796,6 +801,7 @@ static bNodeTree *instances_on_points(const Span objects, const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); group->id = &tree->id; + id_us_plus(&tree->id); bke::node_field_inferencing::update_field_inferencing(*node_tree); nodes::update_node_declaration_and_sockets(*node_tree, *group); return group; @@ -835,8 +841,8 @@ static bNodeTree *instances_on_faces(const Span objects, Main *bmain, RegularNodeTrees &cached_node_trees) { - const std::string tree_name = std::string(name) + std::string("_childrens"); - bNodeTree *node_tree = ntreeAddTree(bmain, tree_name.c_str(), "GeometryNodeTree"); + bNodeTree *node_tree = ntreeAddTree(bmain, name.c_str(), "GeometryNodeTree"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Geometry"); ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Viewport"); ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketBool", "Render"); @@ -857,6 +863,7 @@ static bNodeTree *instances_on_faces(const Span objects, const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); group->id = &tree->id; + id_us_plus(&tree->id); bke::node_field_inferencing::update_field_inferencing(*node_tree); nodes::update_node_declaration_and_sockets(*node_tree, *group); return group; @@ -896,13 +903,22 @@ static bNodeTree *instances_on_faces(const Span objects, return node_tree; } -static void object_push_instances_modifier(Main * /*bmain*/, Object *object, bNodeTree *node_tree) +static void object_push_instances_modifier(const StringRefNull name, + Object &object, + bNodeTree &node_tree) { /* TODO: Move setings from object properties in modifier panel. */ ModifierData *new_modifier = BKE_modifier_new(eModifierType_Nodes); NodesModifierData &node_modifier = *reinterpret_cast(new_modifier); - node_modifier.node_group = node_tree; - BLI_addtail(&object->modifiers, new_modifier); + + node_modifier.node_group = &node_tree; + id_us_plus(&node_tree.id); + + STRNCPY(node_modifier.modifier.name, name.c_str()); + BKE_modifier_unique_name(&object.modifiers, new_modifier); + + BLI_addtail(&object.modifiers, new_modifier); + BKE_object_modifier_set_active(&object, new_modifier); } } // namespace replace_legacy_instances @@ -938,16 +954,28 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) const bool on_vertices = (parent->transflag & OB_DUPLIVERTS) != 0; const StringRefNull parent_name(parent->id.name + 2); + const StringRefNull domain_name = on_vertices ? TIP_("Points") : TIP_("Faces"); + + char tree_name[64]; + BLI_snprintf(tree_name, + sizeof(tree_name), + TIP_("Instances on %s of %s"), + domain_name.c_str(), + parent_name.c_str()); + + char modifier_name[64]; + BLI_snprintf( + modifier_name, sizeof(modifier_name), TIP_("Instances on %s 3.6"), domain_name.c_str()); using namespace replace_legacy_instances; bNodeTree *instances_node_tree; if (on_vertices) { - instances_node_tree = instances_on_points(objects, parent_name, bmain, cached_node_trees); + instances_node_tree = instances_on_points(objects, tree_name, bmain, cached_node_trees); } else { - instances_node_tree = instances_on_faces(objects, parent_name, bmain, cached_node_trees); + instances_node_tree = instances_on_faces(objects, tree_name, bmain, cached_node_trees); } - object_push_instances_modifier(bmain, parent, instances_node_tree); + object_push_instances_modifier(modifier_name, *parent, *instances_node_tree); parent->transflag = 0; } -- 2.30.2 From 94a69148feeb28bda3e7340499e55904246a3d39 Mon Sep 17 00:00:00 2001 From: illua1 Date: Mon, 17 Apr 2023 20:54:42 +0300 Subject: [PATCH 11/30] progress --- source/blender/blenloader/CMakeLists.txt | 1 + .../blenloader/intern/versioning_common.cc | 35 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/source/blender/blenloader/CMakeLists.txt b/source/blender/blenloader/CMakeLists.txt index 8c63c499fe7..e65f31406f8 100644 --- a/source/blender/blenloader/CMakeLists.txt +++ b/source/blender/blenloader/CMakeLists.txt @@ -15,6 +15,7 @@ set(INC ../makesdna ../makesrna ../nodes + ../modifiers ../render ../sequencer ../windowmanager diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index cee1bda44ce..afa150d19a5 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -26,8 +26,13 @@ #include "NOD_socket.h" +#include "MOD_nodes.h" + +#include "DEG_depsgraph.h" + #include "BKE_animsys.h" #include "BKE_attribute.h" +#include "BKE_idprop.hh" #include "BKE_lib_id.h" #include "BKE_main.h" #include "BKE_main_namemap.h" @@ -908,21 +913,35 @@ static void object_push_instances_modifier(const StringRefNull name, bNodeTree &node_tree) { /* TODO: Move setings from object properties in modifier panel. */ - ModifierData *new_modifier = BKE_modifier_new(eModifierType_Nodes); - NodesModifierData &node_modifier = *reinterpret_cast(new_modifier); + ModifierData *md = BKE_modifier_new(eModifierType_Nodes); + NodesModifierData &nmd = *reinterpret_cast(md); - node_modifier.node_group = &node_tree; + nmd.node_group = &node_tree; id_us_plus(&node_tree.id); - STRNCPY(node_modifier.modifier.name, name.c_str()); - BKE_modifier_unique_name(&object.modifiers, new_modifier); + STRNCPY(nmd.modifier.name, name.c_str()); + BKE_modifier_unique_name(&object.modifiers, md); - BLI_addtail(&object.modifiers, new_modifier); - BKE_object_modifier_set_active(&object, new_modifier); + BLI_addtail(&object.modifiers, md); + BKE_object_modifier_set_active(&object, md); + + MOD_nodes_update_interface(&object, &nmd); + + int index = 0; + LISTBASE_FOREACH_INDEX (bNodeSocket *, socket, &node_tree.inputs, index) { + if (index == 0) { + continue; + } + /* TODO */ + // IDProperty *new_prop = IDP_GetPropertyFromGroup(nmd.settings.properties, + // socket->identifier); BLI_assert(old_prop != nullptr); IDP_CopyPropertyContent(new_prop, + // old_prop); + } } } // namespace replace_legacy_instances +/* TODO: objects in parent object space or in global? */ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) { using namespace blender; @@ -978,6 +997,8 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) object_push_instances_modifier(modifier_name, *parent, *instances_node_tree); parent->transflag = 0; + + DEG_id_tag_update(&parent->id, ID_RECALC_GEOMETRY); } printf("HELLO!\n"); -- 2.30.2 From 2c6735041f16d1367115a2fe4d8035a83db619fb Mon Sep 17 00:00:00 2001 From: illua1 Date: Tue, 25 Apr 2023 21:47:30 +0300 Subject: [PATCH 12/30] progress --- .../blenloader/intern/versioning_common.cc | 223 ++++++++++++++++-- 1 file changed, 201 insertions(+), 22 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index afa150d19a5..a3d14dd772b 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -358,11 +358,194 @@ struct RegularNodeTrees { bNodeTree *view_geometry = nullptr; + bNodeTree *reset_instances_transform = nullptr; + bNodeTree *sample_apply_instances_transform = nullptr; + bNodeTree *face_scale = nullptr; bNodeTree *face_aling = nullptr; bNodeTree *first_face_tris_points = nullptr; }; +template static void for_node_storage(bNodeTree *tree, bNode *node, Func &func) +{ + switch (node->type) { + case GEO_NODE_SAMPLE_INDEX: + func(*reinterpret_cast(node->storage)); + break; + case GEO_NODE_SWITCH: + func(*reinterpret_cast(node->storage)); + break; + case GEO_NODE_CAPTURE_ATTRIBUTE: + func(*reinterpret_cast(node->storage)); + break; + default: + BLI_assert_unreachable(); + return; + } + if (node->typeinfo->updatefunc != nullptr) { + node->typeinfo->updatefunc(tree, node); + } +} + +static bNodeTree *reset_instances_transform_tree(Main *bmain, RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.reset_instances_transform != nullptr) { + return cached_node_trees.reset_instances_transform; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, ".Reset Instances Trasform", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instances"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + bNode *instances_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *scale_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SCALE_INSTANCES); + connect(instances_in, "Instances", scale_instances, "Instances"); + node_input_by_name("Local Space", scale_instances) + ->default_value_typed() + ->value = true; + + bNode *rotate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); + connect(scale_instances, "Instances", rotate_instances, "Instances"); + + bNode *translate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_TRANSLATE_INSTANCES); + connect(rotate_instances, "Instances", translate_instances, "Instances"); + + bNode *geometry_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(translate_instances, "Instances", geometry_out, "Instances"); + + bNode *invert_size = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + invert_size->custom1 = NODE_VECTOR_MATH_DIVIDE; + invert_size->typeinfo->updatefunc(node_tree, invert_size); + connect(invert_size, "Vector", scale_instances, "Scale"); + { + float &size[3] = node_input_by_name("Vector", invert_size) + ->default_value_typed() + ->value; + size[0] = -1.0f; + size[1] = -1.0f; + size[2] = -1.0f; + } + + bNode *flip_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + flip_rotation->custom1 = NODE_VECTOR_MATH_SCALE; + flip_rotation->typeinfo->updatefunc(node_tree, flip_rotation); + connect(flip_rotation, "Vector", rotate_instances, "Rotation"); + node_input_by_name("Scale", flip_rotation)->default_value_typed()->value = + -1.0f; + + bNode *go_back_position = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + go_back_position->custom1 = NODE_VECTOR_MATH_SCALE; + go_back_position->typeinfo->updatefunc(node_tree, go_back_position); + connect(go_back_position, "Vector", translate_instances, "Translation"); + node_input_by_name("Scale", go_back_position) + ->default_value_typed() + ->value = -1.0f; + + bNode *set_scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_SCALE); + connect(set_scale, "Scale", invert_size, "Vector"); + bNode *set_rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_ROTATION); + connect(set_rotation, "Rotation", flip_rotation, "Vector"); + bNode *set_position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); + connect(set_position, "Position", go_back_position, "Vector"); + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.reset_instances_transform = node_tree; + return node_tree; +} + +static bNodeTree *sample_apply_instances_transform_tree(Main *bmain, + RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.sample_apply_instances_transform != nullptr) { + return cached_node_trees.sample_apply_instances_transform; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, ".Reset Instances Trasform", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instances"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Transform Source"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketInt", "Instances Index"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + bNode *instances_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *scale_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SCALE_INSTANCES); + connect(instances_in, "Instances", scale_instances, "Instances"); + node_input_by_name("Local Space", scale_instances) + ->default_value_typed() + ->value = true; + + bNode *rotate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); + connect(scale_instances, "Instances", rotate_instances, "Instances"); + + bNode *translate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_TRANSLATE_INSTANCES); + connect(rotate_instances, "Instances", translate_instances, "Instances"); + + bNode *geometry_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(translate_instances, "Instances", geometry_out, "Instances"); + + bNode *source_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + bNode *indices_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_ROTATION); + bNode *sample_rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SAMPLE_INDEX); + for_node_storage(node_tree, sample_rotation, [&](auto &storage) { + storage.data_type = CD_PROP_FLOAT3; + storage.domain = ATTR_DOMAIN_INSTANCE; + }); + connect(source_in, "Transform Source", sample_rotation, "Geometry"); + connect(rotation, "Rotation", sample_rotation, "Value"); + connect(indices_in, "Instances Index", sample_rotation, "Indices"); + connect(indices_in, "Instances Index", sample_scale, "Indices"); + + bNode *scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_SCALE); + bNode *sample_scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SAMPLE_INDEX); + for_node_storage(node_tree, sample_scale, [&](auto &storage) { + storage.data_type = CD_PROP_FLOAT3; + storage.domain = ATTR_DOMAIN_INSTANCE; + }); + connect(source_in, "Transform Source", sample_scale, "Geometry"); + connect(scale, "Scale", sample_scale, "Value"); + connect(indices_in, "Instances Index", sample_scale, "Indices"); + + bNode *position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); + bNode *sample_position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SAMPLE_INDEX); + for_node_storage(node_tree, sample_position, [&](auto &storage) { + storage.data_type = CD_PROP_FLOAT3; + storage.domain = ATTR_DOMAIN_INSTANCE; + }); + connect(source_in, "Transform Source", sample_position, "Geometry"); + connect(position, "Position", sample_position, "Value"); + connect(indices_in, "Instances Index", sample_position, "Indices"); + + bNode *instances_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.sample_apply_instances_transform = node_tree; + return node_tree; +} + static bNodeTree *view_geometry_tree(Main *bmain, RegularNodeTrees &cached_node_trees) { if (cached_node_trees.view_geometry != nullptr) { @@ -396,9 +579,9 @@ static bNodeTree *view_geometry_tree(Main *bmain, RegularNodeTrees &cached_node_ bNode *viewport_render_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); bNode *viewport_render_switch = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SWITCH); - NodeSwitch &switch_data_vr = *reinterpret_cast(viewport_render_switch->storage); - switch_data_vr.input_type = SOCK_BOOLEAN; - viewport_render_switch->typeinfo->updatefunc(node_tree, viewport_render_switch); + for_node_storage(node_tree, viewport_render_switch, [&](auto &storage) { + storage.input_type = SOCK_BOOLEAN; + }); connect(bool_invert, "Boolean", viewport_render_switch, "Switch"); connect(viewport_render_in, "Viewport", viewport_render_switch, "False"); connect(viewport_render_in, "Render", viewport_render_switch, "True"); @@ -406,9 +589,8 @@ static bNodeTree *view_geometry_tree(Main *bmain, RegularNodeTrees &cached_node_ bNode *geometry_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); bNode *geometry_switch = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SWITCH); - NodeSwitch &switch_data_g = *reinterpret_cast(geometry_switch->storage); - switch_data_g.input_type = SOCK_GEOMETRY; - geometry_switch->typeinfo->updatefunc(node_tree, geometry_switch); + for_node_storage( + node_tree, geometry_switch, [&](auto &storage) { storage.input_type = SOCK_GEOMETRY; }); connect(viewport_render_switch, "Output", geometry_switch, "Switch"); connect(geometry_in, "Geometry", geometry_switch, "True"); @@ -459,9 +641,8 @@ static bNodeTree *instances_on_points_tree(Main *bmain, RegularNodeTrees &cached bNode *aling_selection_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); bNode *switch_vector = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SWITCH); - NodeSwitch &switch_data = *reinterpret_cast(switch_vector->storage); - switch_data.input_type = SOCK_VECTOR; - switch_vector->typeinfo->updatefunc(node_tree, switch_vector); + for_node_storage( + node_tree, switch_vector, [&](auto &storage) { storage.input_type = SOCK_VECTOR; }); connect(aling_selection_in, "Aling to Vertex Normal", switch_vector, "Switch"); connect(aling_y_x, "Rotation", switch_vector, "True"); @@ -634,11 +815,10 @@ static bNodeTree *face_aling_tree(Main *bmain, RegularNodeTrees &cached_node_tre bNode *in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); bNode *capture_result = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_CAPTURE_ATTRIBUTE); - NodeGeometryAttributeCapture &capture_storage = - *reinterpret_cast(capture_result->storage); - capture_storage.domain = ATTR_DOMAIN_FACE; - capture_storage.data_type = CD_PROP_FLOAT3; - capture_result->typeinfo->updatefunc(node_tree, capture_result); + for_node_storage(node_tree, capture_result, [&](auto &storage) { + storage.domain = ATTR_DOMAIN_FACE; + storage.data_type = CD_PROP_FLOAT3; + }); connect(in, "Geometry", capture_result, "Geometry"); connect(aling_y_auto, "Rotation", capture_result, "Value"); @@ -683,11 +863,10 @@ static bNodeTree *faces_scale_tree(Main *bmain, RegularNodeTrees &cached_node_tr bNode *in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); bNode *capture_result = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_CAPTURE_ATTRIBUTE); - NodeGeometryAttributeCapture &capture_storage = - *reinterpret_cast(capture_result->storage); - capture_storage.domain = ATTR_DOMAIN_FACE; - capture_storage.data_type = CD_PROP_FLOAT; - capture_result->typeinfo->updatefunc(node_tree, capture_result); + for_node_storage(node_tree, capture_result, [&](auto &storage) { + storage.domain = ATTR_DOMAIN_FACE; + storage.data_type = CD_PROP_FLOAT; + }); connect(in, "Geometry", capture_result, "Geometry"); connect(math_sqrt, "Value", capture_result, "Value"); @@ -740,9 +919,9 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ connect(face_size_group, "Geometry", face_aling_group, "Geometry"); bNode *mesh_to_points = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_MESH_TO_POINTS); - NodeGeometryMeshToPoints &points_storage = *reinterpret_cast( - mesh_to_points->storage); - points_storage.mode = GEO_NODE_MESH_TO_POINTS_FACES; + for_node_storage(node_tree, mesh_to_points, [&](auto &storage) { + storage.mode = GEO_NODE_MESH_TO_POINTS_FACES; + }); connect(face_aling_group, "Geometry", mesh_to_points, "Mesh"); bNode *instance_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); -- 2.30.2 From b241c7e3baaaace6deee6500d247437599d81d30 Mon Sep 17 00:00:00 2001 From: illua1 Date: Wed, 26 Apr 2023 18:36:58 +0300 Subject: [PATCH 13/30] progress --- .../blenloader/intern/versioning_300.cc | 10 +- .../blenloader/intern/versioning_common.cc | 370 +++++++++--------- 2 files changed, 195 insertions(+), 185 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_300.cc b/source/blender/blenloader/intern/versioning_300.cc index 3dc21386f4c..a962762909f 100644 --- a/source/blender/blenloader/intern/versioning_300.cc +++ b/source/blender/blenloader/intern/versioning_300.cc @@ -1349,11 +1349,6 @@ void do_versions_after_linking_300(FileData * /*fd*/, Main *bmain) FOREACH_NODETREE_END; } - if (!MAIN_VERSION_ATLEAST(bmain, 306, 5)) { - /* TODO: (bmain, 400, 0). */ - remove_legacy_instances_on(bmain, bmain->objects); - } - if (!MAIN_VERSION_ATLEAST(bmain, 306, 6)) { LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) { Editing *ed = SEQ_editing_get(scene); @@ -1366,6 +1361,11 @@ void do_versions_after_linking_300(FileData * /*fd*/, Main *bmain) } } + if (!MAIN_VERSION_ATLEAST(bmain, 306, 7)) { + /* TODO: (bmain, 400, 0). */ + remove_legacy_instances_on(bmain, bmain->objects); + } + /** * Versioning code until next subversion bump goes here. * diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 9f2f0c30f63..8e815d6386c 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -378,167 +378,6 @@ static void for_node_storage(bNodeTree *tree, } } -/* -static bNodeTree *reset_instances_transform_tree(Main *bmain, RegularNodeTrees &cached_node_trees) -{ - if (cached_node_trees.reset_instances_transform != nullptr) { - return cached_node_trees.reset_instances_transform; - } - - bNodeTree *node_tree = ntreeAddTree(bmain, ".Reset Instances Trasform", "GeometryNodeTree"); - - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instances"); - - ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); - - const auto connect = [node_tree](bNode *node_out, - const StringRefNull name_out, - bNode *node_in, - const StringRefNull name_in) { - bNodeSocket &out = node_output_by_name(name_out, node_out); - bNodeSocket &in = node_input_by_name(name_in, node_in); - nodeAddLink(node_tree, node_out, &out, node_in, &in); - }; - bNode *instances_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - - bNode *scale_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SCALE_INSTANCES); - connect(instances_in, "Instances", scale_instances, "Instances"); - node_input_by_name("Local Space", scale_instances) - ->default_value_typed() - ->value = true; - - bNode *rotate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); - connect(scale_instances, "Instances", rotate_instances, "Instances"); - - bNode *translate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_TRANSLATE_INSTANCES); - connect(rotate_instances, "Instances", translate_instances, "Instances"); - - bNode *geometry_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); - connect(translate_instances, "Instances", geometry_out, "Instances"); - - bNode *invert_size = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); - invert_size->custom1 = NODE_VECTOR_MATH_DIVIDE; - invert_size->typeinfo->updatefunc(node_tree, invert_size); - connect(invert_size, "Vector", scale_instances, "Scale"); - { - float &size[3] = node_input_by_name("Vector", invert_size) - ->default_value_typed() - ->value; - size[0] = -1.0f; - size[1] = -1.0f; - size[2] = -1.0f; - } - - bNode *flip_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); - flip_rotation->custom1 = NODE_VECTOR_MATH_SCALE; - flip_rotation->typeinfo->updatefunc(node_tree, flip_rotation); - connect(flip_rotation, "Vector", rotate_instances, "Rotation"); - node_input_by_name("Scale", flip_rotation)->default_value_typed()->value = - -1.0f; - - bNode *go_back_position = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); - go_back_position->custom1 = NODE_VECTOR_MATH_SCALE; - go_back_position->typeinfo->updatefunc(node_tree, go_back_position); - connect(go_back_position, "Vector", translate_instances, "Translation"); - node_input_by_name("Scale", go_back_position) - ->default_value_typed() - ->value = -1.0f; - - bNode *set_scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_SCALE); - connect(set_scale, "Scale", invert_size, "Vector"); - bNode *set_rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_ROTATION); - connect(set_rotation, "Rotation", flip_rotation, "Vector"); - bNode *set_position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); - connect(set_position, "Position", go_back_position, "Vector"); - - bke::node_field_inferencing::update_field_inferencing(*node_tree); - cached_node_trees.reset_instances_transform = node_tree; - return node_tree; -} - -static bNodeTree *sample_apply_instances_transform_tree(Main *bmain, - RegularNodeTrees &cached_node_trees) -{ - if (cached_node_trees.sample_apply_instances_transform != nullptr) { - return cached_node_trees.sample_apply_instances_transform; - } - - bNodeTree *node_tree = ntreeAddTree(bmain, ".Reset Instances Trasform", "GeometryNodeTree"); - - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instances"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Transform Source"); - ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketInt", "Instances Index"); - - ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); - - const auto connect = [node_tree](bNode *node_out, - const StringRefNull name_out, - bNode *node_in, - const StringRefNull name_in) { - bNodeSocket &out = node_output_by_name(name_out, node_out); - bNodeSocket &in = node_input_by_name(name_in, node_in); - nodeAddLink(node_tree, node_out, &out, node_in, &in); - }; - - bNode *instances_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - - bNode *scale_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SCALE_INSTANCES); - connect(instances_in, "Instances", scale_instances, "Instances"); - node_input_by_name("Local Space", scale_instances) - ->default_value_typed() - ->value = true; - - bNode *rotate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); - connect(scale_instances, "Instances", rotate_instances, "Instances"); - - bNode *translate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_TRANSLATE_INSTANCES); - connect(rotate_instances, "Instances", translate_instances, "Instances"); - - bNode *geometry_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); - connect(translate_instances, "Instances", geometry_out, "Instances"); - - bNode *source_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - bNode *indices_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - - bNode *rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_ROTATION); - bNode *sample_rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SAMPLE_INDEX); - for_node_storage(node_tree, sample_rotation, [&](auto &storage) { - storage.data_type = CD_PROP_FLOAT3; - storage.domain = ATTR_DOMAIN_INSTANCE; - }); - connect(source_in, "Transform Source", sample_rotation, "Geometry"); - connect(rotation, "Rotation", sample_rotation, "Value"); - connect(indices_in, "Instances Index", sample_rotation, "Indices"); - connect(indices_in, "Instances Index", sample_scale, "Indices"); - - bNode *scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_SCALE); - bNode *sample_scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SAMPLE_INDEX); - for_node_storage(node_tree, sample_scale, [&](auto &storage) { - storage.data_type = CD_PROP_FLOAT3; - storage.domain = ATTR_DOMAIN_INSTANCE; - }); - connect(source_in, "Transform Source", sample_scale, "Geometry"); - connect(scale, "Scale", sample_scale, "Value"); - connect(indices_in, "Instances Index", sample_scale, "Indices"); - - bNode *position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); - bNode *sample_position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SAMPLE_INDEX); - for_node_storage(node_tree, sample_position, [&](auto &storage) { - storage.data_type = CD_PROP_FLOAT3; - storage.domain = ATTR_DOMAIN_INSTANCE; - }); - connect(source_in, "Transform Source", sample_position, "Geometry"); - connect(position, "Position", sample_position, "Value"); - connect(indices_in, "Instances Index", sample_position, "Indices"); - - bNode *instances_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); - - bke::node_field_inferencing::update_field_inferencing(*node_tree); - cached_node_trees.sample_apply_instances_transform = node_tree; - return node_tree; -} -*/ - static bNodeTree *view_geometry_tree(Main *bmain, RegularNodeTrees &cached_node_trees) { if (cached_node_trees.view_geometry != nullptr) { @@ -595,6 +434,193 @@ static bNodeTree *view_geometry_tree(Main *bmain, RegularNodeTrees &cached_node_ return node_tree; } +static bNode *join_objects_as_instances(const Span objects, bNodeTree *node_tree) +{ + bNode *join_geometrys = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_JOIN_GEOMETRY); + bNodeSocket &in = node_input_by_name("Geometry", join_geometrys); + + for (Object *object : objects) { + bNode *object_info = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_OBJECT_INFO); + bNodeSocket &object_input = node_input_by_name("Object", object_info); + bNodeSocket &as_instance = node_input_by_name("As Instance", object_info); + object_input.default_value_typed()->value = object; + as_instance.default_value_typed()->value = true; + + bNodeSocket &out = node_output_by_name("Geometry", object_info); + nodeAddLink(node_tree, object_info, &out, join_geometrys, &in); + } + + return join_geometrys; +} + +static bNodeTree *reset_instances_transform_tree(Main *bmain, RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.reset_instances_transform != nullptr) { + return cached_node_trees.reset_instances_transform; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, ".Reset Instances Trasform", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instances"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + bNode *instances_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *scale_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SCALE_INSTANCES); + connect(instances_in, "Instances", scale_instances, "Instances"); + node_input_by_name("Local Space", scale_instances) + .default_value_typed() + ->value = true; + + bNode *rotate_z_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); + connect(scale_instances, "Instances", rotate_z_instances, "Instances"); + bNode *rotate_y_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); + connect(rotate_z_instances, "Instances", rotate_y_instances, "Instances"); + bNode *rotate_x_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); + connect(rotate_y_instances, "Instances", rotate_x_instances, "Instances"); + + bNode *translate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_TRANSLATE_INSTANCES); + connect(rotate_x_instances, "Instances", translate_instances, "Instances"); + + bNode *geometry_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(translate_instances, "Instances", geometry_out, "Instances"); + + bNode *invert_size = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + invert_size->custom1 = NODE_VECTOR_MATH_DIVIDE; + invert_size->typeinfo->updatefunc(node_tree, invert_size); + float(&size)[3] = node_input_by_name("Vector", invert_size) + .default_value_typed() + ->value; + size[0] = -1.0f; + size[1] = -1.0f; + size[2] = -1.0f; + connect(invert_size, "Vector", scale_instances, "Scale"); + + bNode *flip_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + flip_rotation->custom1 = NODE_VECTOR_MATH_SCALE; + flip_rotation->typeinfo->updatefunc(node_tree, flip_rotation); + + bNode *separate_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_SEPXYZ); + connect(flip_rotation, "Vector", separate_rotation, "Vector"); + + bNode *combine_z_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_COMBXYZ); + connect(separate_rotation, "Z", combine_z_rotation, "Z"); + connect(combine_z_rotation, "Vector", rotate_z_instances, "Rotation"); + bNode *combine_y_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_COMBXYZ); + connect(separate_rotation, "Y", combine_y_rotation, "Y"); + connect(combine_y_rotation, "Vector", rotate_y_instances, "Rotation"); + bNode *combine_x_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_COMBXYZ); + connect(separate_rotation, "Z", combine_x_rotation, "Z"); + connect(combine_x_rotation, "Vector", rotate_x_instances, "Rotation"); + + bNode *go_back_position = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + go_back_position->custom1 = NODE_VECTOR_MATH_SCALE; + go_back_position->typeinfo->updatefunc(node_tree, go_back_position); + connect(go_back_position, "Vector", translate_instances, "Translation"); + node_input_by_name("Scale", go_back_position) + .default_value_typed() + ->value = -1.0f; + + bNode *set_scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_SCALE); + connect(set_scale, "Scale", invert_size, "Vector"); + bNode *set_rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_ROTATION); + connect(set_rotation, "Rotation", flip_rotation, "Vector"); + bNode *set_position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); + connect(set_position, "Position", go_back_position, "Vector"); + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.reset_instances_transform = node_tree; + return node_tree; +} + +static bNodeTree *sample_apply_instances_transform_tree(Main *bmain, + RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.sample_apply_instances_transform != nullptr) { + return cached_node_trees.sample_apply_instances_transform; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, ".Reset Instances Trasform", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instances"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Transform Source"); + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketInt", "Instances Index"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketGeometry", "Instances"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + bNode *instances_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *scale_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SCALE_INSTANCES); + connect(instances_in, "Instances", scale_instances, "Instances"); + + bNode *rotate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); + connect(scale_instances, "Instances", rotate_instances, "Instances"); + + bNode *translate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_TRANSLATE_INSTANCES); + connect(rotate_instances, "Instances", translate_instances, "Instances"); + + bNode *geometry_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + connect(translate_instances, "Instances", geometry_out, "Instances"); + + bNode *source_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + bNode *indices_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_SCALE); + bNode *sample_scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SAMPLE_INDEX); + for_node_storage(node_tree, sample_scale, [&](auto &storage) { + storage.data_type = CD_PROP_FLOAT3; + storage.domain = ATTR_DOMAIN_INSTANCE; + }); + connect(source_in, "Transform Source", sample_scale, "Geometry"); + connect(scale, "Scale", sample_scale, "Value"); + connect(indices_in, "Instances Index", sample_scale, "Index"); + connect(sample_scale, "Value", scale_instances, "Scale"); + + bNode *rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_ROTATION); + bNode *sample_rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SAMPLE_INDEX); + for_node_storage(node_tree, sample_rotation, [&](auto &storage) { + storage.data_type = CD_PROP_FLOAT3; + storage.domain = ATTR_DOMAIN_INSTANCE; + }); + connect(source_in, "Transform Source", sample_rotation, "Geometry"); + connect(rotation, "Rotation", sample_rotation, "Value"); + connect(indices_in, "Instances Index", sample_rotation, "Index"); + connect(sample_rotation, "Value", rotate_instances, "Rotation"); + + bNode *position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); + bNode *sample_position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SAMPLE_INDEX); + for_node_storage(node_tree, sample_position, [&](auto &storage) { + storage.data_type = CD_PROP_FLOAT3; + storage.domain = ATTR_DOMAIN_INSTANCE; + }); + connect(source_in, "Transform Source", sample_position, "Geometry"); + connect(position, "Position", sample_position, "Value"); + connect(indices_in, "Instances Index", sample_position, "Index"); + connect(sample_position, "Value", translate_instances, "Translation"); + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.sample_apply_instances_transform = node_tree; + return node_tree; +} + static bNodeTree *instances_on_points_tree(Main *bmain, RegularNodeTrees &cached_node_trees) { if (cached_node_trees.instances_on_points != nullptr) { @@ -905,6 +931,9 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ bNode *instancer_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + add_node_group(reset_instances_transform_tree(bmain, cached_node_trees)); + add_node_group(sample_apply_instances_transform_tree(bmain, cached_node_trees)); + bNode *face_size_group = add_node_group(faces_scale_tree(bmain, cached_node_trees)); connect(instancer_in, "Instancer", face_size_group, "Geometry"); @@ -933,25 +962,6 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ return node_tree; } -static bNode *join_objects_as_instances(const Span objects, bNodeTree *node_tree) -{ - bNode *join_geometrys = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_JOIN_GEOMETRY); - bNodeSocket &in = node_input_by_name("Geometry", join_geometrys); - - for (Object *object : objects) { - bNode *object_info = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_OBJECT_INFO); - bNodeSocket &object_input = node_input_by_name("Object", object_info); - bNodeSocket &as_instance = node_input_by_name("As Instance", object_info); - object_input.default_value_typed()->value = object; - as_instance.default_value_typed()->value = true; - - bNodeSocket &out = node_output_by_name("Geometry", object_info); - nodeAddLink(node_tree, object_info, &out, join_geometrys, &in); - } - - return join_geometrys; -} - static bNodeTree *instances_on_points(const Span objects, const StringRefNull name, Main *bmain, -- 2.30.2 From a04e92fa2372cda297b9bde3c3112df7a109c7bc Mon Sep 17 00:00:00 2001 From: illua1 Date: Wed, 26 Apr 2023 22:37:48 +0300 Subject: [PATCH 14/30] progress --- .../blenloader/intern/versioning_common.cc | 65 +++++++++++++++---- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 8e815d6386c..371e1af722d 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -441,6 +441,9 @@ static bNode *join_objects_as_instances(const Span objects, bNodeTree for (Object *object : objects) { bNode *object_info = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_OBJECT_INFO); + for_node_storage(node_tree, object_info, [&](auto &storage) { + storage.transform_space = GEO_NODE_TRANSFORM_SPACE_RELATIVE; + }); bNodeSocket &object_input = node_input_by_name("Object", object_info); bNodeSocket &as_instance = node_input_by_name("As Instance", object_info); object_input.default_value_typed()->value = object; @@ -473,23 +476,48 @@ static bNodeTree *reset_instances_transform_tree(Main *bmain, RegularNodeTrees & bNodeSocket &in = node_input_by_name(name_in, node_in); nodeAddLink(node_tree, node_out, &out, node_in, &in); }; + + const auto set_in = [](bNode *node, StringRefNull input_name, auto value) { + if constexpr (std::is_same_v) { + node_input_by_name(input_name, node).default_value_typed()->value = + value; + } + else if constexpr (std::is_same_v) { + node_input_by_name(input_name, node).default_value_typed()->value = + value; + } + else if constexpr (std::is_same_v) { + float(&values)[3] = node_input_by_name(input_name, node) + .default_value_typed() + ->value; + values[0] = value[0]; + values[1] = value[1]; + values[2] = value[2]; + } + else { + static_assert(true); + } + }; + bNode *instances_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); bNode *scale_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SCALE_INSTANCES); connect(instances_in, "Instances", scale_instances, "Instances"); - node_input_by_name("Local Space", scale_instances) - .default_value_typed() - ->value = true; + set_in(scale_instances, "Local Space", false); bNode *rotate_z_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); connect(scale_instances, "Instances", rotate_z_instances, "Instances"); + set_in(rotate_z_instances, "Local Space", false); bNode *rotate_y_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); connect(rotate_z_instances, "Instances", rotate_y_instances, "Instances"); + set_in(rotate_y_instances, "Local Space", false); bNode *rotate_x_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); connect(rotate_y_instances, "Instances", rotate_x_instances, "Instances"); + set_in(rotate_x_instances, "Local Space", false); bNode *translate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_TRANSLATE_INSTANCES); connect(rotate_x_instances, "Instances", translate_instances, "Instances"); + set_in(translate_instances, "Local Space", false); bNode *geometry_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); connect(translate_instances, "Instances", geometry_out, "Instances"); @@ -497,17 +525,13 @@ static bNodeTree *reset_instances_transform_tree(Main *bmain, RegularNodeTrees & bNode *invert_size = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); invert_size->custom1 = NODE_VECTOR_MATH_DIVIDE; invert_size->typeinfo->updatefunc(node_tree, invert_size); - float(&size)[3] = node_input_by_name("Vector", invert_size) - .default_value_typed() - ->value; - size[0] = -1.0f; - size[1] = -1.0f; - size[2] = -1.0f; + set_in(invert_size, "Vector", float3{1.0f}); connect(invert_size, "Vector", scale_instances, "Scale"); bNode *flip_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); flip_rotation->custom1 = NODE_VECTOR_MATH_SCALE; flip_rotation->typeinfo->updatefunc(node_tree, flip_rotation); + set_in(flip_rotation, "Scale", -1.0f); bNode *separate_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_SEPXYZ); connect(flip_rotation, "Vector", separate_rotation, "Vector"); @@ -526,12 +550,10 @@ static bNodeTree *reset_instances_transform_tree(Main *bmain, RegularNodeTrees & go_back_position->custom1 = NODE_VECTOR_MATH_SCALE; go_back_position->typeinfo->updatefunc(node_tree, go_back_position); connect(go_back_position, "Vector", translate_instances, "Translation"); - node_input_by_name("Scale", go_back_position) - .default_value_typed() - ->value = -1.0f; + set_in(go_back_position, "Scale", -1.0f); bNode *set_scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_SCALE); - connect(set_scale, "Scale", invert_size, "Vector"); + connect(set_scale, "Scale", invert_size, "Vector_001"); bNode *set_rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_ROTATION); connect(set_rotation, "Rotation", flip_rotation, "Vector"); bNode *set_position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); @@ -566,16 +588,33 @@ static bNodeTree *sample_apply_instances_transform_tree(Main *bmain, nodeAddLink(node_tree, node_out, &out, node_in, &in); }; + const auto set_in = [](bNode *node, StringRefNull input_name, auto value) { + if constexpr (std::is_same_v) { + node_input_by_name(input_name, node).default_value_typed()->value = + value; + } + else { + static_assert(true); + } + }; + bNode *instances_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + bNode *pivot_porition = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); + bNode *scale_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SCALE_INSTANCES); connect(instances_in, "Instances", scale_instances, "Instances"); + connect(pivot_porition, "Position", scale_instances, "Center"); + set_in(scale_instances, "Local Space", false); bNode *rotate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); connect(scale_instances, "Instances", rotate_instances, "Instances"); + connect(pivot_porition, "Position", rotate_instances, "Pivot Point"); + set_in(rotate_instances, "Local Space", false); bNode *translate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_TRANSLATE_INSTANCES); connect(rotate_instances, "Instances", translate_instances, "Instances"); + set_in(translate_instances, "Local Space", false); bNode *geometry_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); connect(translate_instances, "Instances", geometry_out, "Instances"); -- 2.30.2 From c7eefd5e00aa3d1f2535cd2d50716b6340c32412 Mon Sep 17 00:00:00 2001 From: illua1 Date: Wed, 26 Apr 2023 23:12:09 +0300 Subject: [PATCH 15/30] progress --- .../blenloader/intern/versioning_common.cc | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 371e1af722d..ea4d2fcfd7a 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -32,6 +32,7 @@ #include "BKE_animsys.h" #include "BKE_attribute.h" +#include "BKE_geometry_set.h" #include "BKE_idprop.hh" #include "BKE_lib_id.h" #include "BKE_main.h" @@ -571,7 +572,7 @@ static bNodeTree *sample_apply_instances_transform_tree(Main *bmain, return cached_node_trees.sample_apply_instances_transform; } - bNodeTree *node_tree = ntreeAddTree(bmain, ".Reset Instances Trasform", "GeometryNodeTree"); + bNodeTree *node_tree = ntreeAddTree(bmain, ".Apply Instances Trasform", "GeometryNodeTree"); ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Instances"); ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketGeometry", "Transform Source"); @@ -970,8 +971,11 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ bNode *instancer_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - add_node_group(reset_instances_transform_tree(bmain, cached_node_trees)); - add_node_group(sample_apply_instances_transform_tree(bmain, cached_node_trees)); + bNode *instance_total_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + bNode *instance_total = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ATTRIBUTE_DOMAIN_SIZE); + instance_total->custom1 = int(GEO_COMPONENT_TYPE_INSTANCES); + instance_total->typeinfo->updatefunc(node_tree, instance_total); + connect(instance_total_in, "Instance", instance_total, "Geometry"); bNode *face_size_group = add_node_group(faces_scale_tree(bmain, cached_node_trees)); connect(instancer_in, "Instancer", face_size_group, "Geometry"); @@ -985,16 +989,30 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ }); connect(face_aling_group, "Geometry", mesh_to_points, "Mesh"); + bNode *duplicate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_DUPLICATE_ELEMENTS); + connect(mesh_to_points, "Points", duplicate_instances, "Geometry"); + connect(instance_total, "Instance Count", duplicate_instances, "Amount"); + bNode *instance_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + bNode *reset_transform = add_node_group( + reset_instances_transform_tree(bmain, cached_node_trees)); + connect(instance_in, "Instance", reset_transform, "Instances"); bNode *instance_on_points = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INSTANCE_ON_POINTS); - connect(mesh_to_points, "Points", instance_on_points, "Points"); - connect(instance_in, "Instance", instance_on_points, "Instance"); + connect(duplicate_instances, "Geometry", instance_on_points, "Points"); + connect(reset_transform, "Instances", instance_on_points, "Instance"); connect(face_aling_group, "Rotation", instance_on_points, "Rotation"); connect(face_size_group, "Size", instance_on_points, "Scale"); + bNode *instances_source_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + bNode *apply_transform = add_node_group( + sample_apply_instances_transform_tree(bmain, cached_node_trees)); + connect(instance_on_points, "Instances", apply_transform, "Instances"); + connect(instances_source_in, "Instance", apply_transform, "Transform Source"); + connect(duplicate_instances, "Duplicate Index", apply_transform, "Instances Index"); + bNode *out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); - connect(instance_on_points, "Instances", out, "Instances"); + connect(apply_transform, "Instances", out, "Instances"); bke::node_field_inferencing::update_field_inferencing(*node_tree); cached_node_trees.instances_on_faces = node_tree; -- 2.30.2 From 6f83eaa51459d47690c7a7ccf7d3d766b6c09754 Mon Sep 17 00:00:00 2001 From: illua1 Date: Wed, 26 Apr 2023 23:17:03 +0300 Subject: [PATCH 16/30] progress --- .../blenloader/intern/versioning_common.cc | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index ea4d2fcfd7a..03bc3532746 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -801,7 +801,10 @@ static bNodeTree *face_aling_tree(Main *bmain, RegularNodeTrees &cached_node_tre nodeAddLink(node_tree, node_out, &out, node_in, &in); }; - const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { + const auto add_node_group = + [&](const FunctionRef + tree_func) -> bNode * { + bNodeTree *tree = tree_func(bmain, cached_node_trees); bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); group->id = &tree->id; id_us_plus(&tree->id); @@ -810,8 +813,7 @@ static bNodeTree *face_aling_tree(Main *bmain, RegularNodeTrees &cached_node_tre return group; }; - bNode *triangle_indices_group = add_node_group( - first_face_tris_points_tree(bmain, cached_node_trees)); + bNode *triangle_indices_group = add_node_group(first_face_tris_points_tree); bNode *position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); @@ -960,7 +962,10 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ nodeAddLink(node_tree, node_out, &out, node_in, &in); }; - const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { + const auto add_node_group = + [&](const FunctionRef + tree_func) -> bNode * { + bNodeTree *tree = tree_func(bmain, cached_node_trees); bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); group->id = &tree->id; id_us_plus(&tree->id); @@ -977,10 +982,10 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ instance_total->typeinfo->updatefunc(node_tree, instance_total); connect(instance_total_in, "Instance", instance_total, "Geometry"); - bNode *face_size_group = add_node_group(faces_scale_tree(bmain, cached_node_trees)); + bNode *face_size_group = add_node_group(faces_scale_tree); connect(instancer_in, "Instancer", face_size_group, "Geometry"); - bNode *face_aling_group = add_node_group(face_aling_tree(bmain, cached_node_trees)); + bNode *face_aling_group = add_node_group(face_aling_tree); connect(face_size_group, "Geometry", face_aling_group, "Geometry"); bNode *mesh_to_points = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_MESH_TO_POINTS); @@ -994,8 +999,7 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ connect(instance_total, "Instance Count", duplicate_instances, "Amount"); bNode *instance_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - bNode *reset_transform = add_node_group( - reset_instances_transform_tree(bmain, cached_node_trees)); + bNode *reset_transform = add_node_group(reset_instances_transform_tree); connect(instance_in, "Instance", reset_transform, "Instances"); bNode *instance_on_points = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INSTANCE_ON_POINTS); @@ -1005,8 +1009,7 @@ static bNodeTree *instances_on_faces_tree(Main *bmain, RegularNodeTrees &cached_ connect(face_size_group, "Size", instance_on_points, "Scale"); bNode *instances_source_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - bNode *apply_transform = add_node_group( - sample_apply_instances_transform_tree(bmain, cached_node_trees)); + bNode *apply_transform = add_node_group(sample_apply_instances_transform_tree); connect(instance_on_points, "Instances", apply_transform, "Instances"); connect(instances_source_in, "Instance", apply_transform, "Transform Source"); connect(duplicate_instances, "Duplicate Index", apply_transform, "Instances Index"); @@ -1042,7 +1045,10 @@ static bNodeTree *instances_on_points(const Span objects, nodeAddLink(node_tree, node_out, &out, node_in, &in); }; - const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { + const auto add_node_group = + [&](const FunctionRef + tree_func) -> bNode * { + bNodeTree *tree = tree_func(bmain, cached_node_trees); bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); group->id = &tree->id; id_us_plus(&tree->id); @@ -1053,7 +1059,7 @@ static bNodeTree *instances_on_points(const Span objects, bNode *join_geometrys = join_objects_as_instances(objects, node_tree); - bNode *instansing_group = add_node_group(instances_on_points_tree(bmain, cached_node_trees)); + bNode *instansing_group = add_node_group(instances_on_points_tree); connect(join_geometrys, "Geometry", instansing_group, "Instance"); bNode *parent_aling_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); @@ -1061,7 +1067,7 @@ static bNodeTree *instances_on_points(const Span objects, connect( parent_aling_input, "Aling to Vertex Normal", instansing_group, "Aling to Vertex Normal"); - bNode *view_switch_group = add_node_group(view_geometry_tree(bmain, cached_node_trees)); + bNode *view_switch_group = add_node_group(view_geometry_tree); bNode *geometry_viewport_render_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); connect(geometry_viewport_render_input, "Geometry", view_switch_group, "Geometry"); connect(geometry_viewport_render_input, "Viewport", view_switch_group, "Viewport"); @@ -1104,7 +1110,10 @@ static bNodeTree *instances_on_faces(const Span objects, nodeAddLink(node_tree, node_out, &out, node_in, &in); }; - const auto add_node_group = [node_tree](bNodeTree *tree) -> bNode * { + const auto add_node_group = + [&](const FunctionRef + tree_func) -> bNode * { + bNodeTree *tree = tree_func(bmain, cached_node_trees); bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); group->id = &tree->id; id_us_plus(&tree->id); @@ -1122,7 +1131,7 @@ static bNodeTree *instances_on_faces(const Span objects, connect(scale_factor_input, "Scale by Face Size", scale_instances, "Selection"); connect(scale_factor_input, "Factor", scale_instances, "Scale"); - bNode *instansing_group = add_node_group(instances_on_faces_tree(bmain, cached_node_trees)); + bNode *instansing_group = add_node_group(instances_on_faces_tree); connect(scale_instances, "Instances", instansing_group, "Instance"); bNode *geometry_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); @@ -1130,7 +1139,7 @@ static bNodeTree *instances_on_faces(const Span objects, bNode *geometry_viewport_render_input = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - bNode *view_switch_group = add_node_group(view_geometry_tree(bmain, cached_node_trees)); + bNode *view_switch_group = add_node_group(view_geometry_tree); connect(geometry_viewport_render_input, "Geometry", view_switch_group, "Geometry"); connect(geometry_viewport_render_input, "Viewport", view_switch_group, "Viewport"); connect(geometry_viewport_render_input, "Render", view_switch_group, "Render"); -- 2.30.2 From 44ae5bea2c871f77d5d8e1f6fe1efb36ae89f0c9 Mon Sep 17 00:00:00 2001 From: illua1 Date: Wed, 26 Apr 2023 23:49:22 +0300 Subject: [PATCH 17/30] progress --- .../blenloader/intern/versioning_common.cc | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 03bc3532746..958d1e9f6fd 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -684,6 +684,18 @@ static bNodeTree *instances_on_points_tree(Main *bmain, RegularNodeTrees &cached nodeAddLink(node_tree, node_out, &out, node_in, &in); }; + const auto add_node_group = + [&](const FunctionRef + tree_func) -> bNode * { + bNodeTree *tree = tree_func(bmain, cached_node_trees); + bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); + group->id = &tree->id; + id_us_plus(&tree->id); + bke::node_field_inferencing::update_field_inferencing(*node_tree); + nodes::update_node_declaration_and_sockets(*node_tree, *group); + return group; + }; + bNode *normal_input = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_NORMAL); bNode *aling_y_z = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ALIGN_EULER_TO_VECTOR); @@ -706,16 +718,42 @@ static bNodeTree *instances_on_points_tree(Main *bmain, RegularNodeTrees &cached connect(aling_y_x, "Rotation", switch_vector, "True"); bNode *instance_on_point = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INSTANCE_ON_POINTS); - connect(switch_vector, "Output", instance_on_point, "Rotation"); bNode *parent_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - connect(parent_in, "Instancer", instance_on_point, "Points"); + bNode *capture_result = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_CAPTURE_ATTRIBUTE); + for_node_storage( + node_tree, capture_result, [&](auto &storage) { storage.data_type = CD_PROP_FLOAT3; }); + connect(parent_in, "Instancer", capture_result, "Geometry"); + connect(switch_vector, "Output", capture_result, "Value"); + connect(capture_result, "Attribute", instance_on_point, "Rotation"); + + bNode *mesh_to_points = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_MESH_TO_POINTS); + connect(capture_result, "Geometry", mesh_to_points, "Mesh"); + + bNode *duplicate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_DUPLICATE_ELEMENTS); + connect(mesh_to_points, "Points", duplicate_instances, "Geometry"); + connect(duplicate_instances, "Geometry", instance_on_point, "Points"); + + bNode *instance_total_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + bNode *instance_total = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ATTRIBUTE_DOMAIN_SIZE); + instance_total->custom1 = int(GEO_COMPONENT_TYPE_INSTANCES); + instance_total->typeinfo->updatefunc(node_tree, instance_total); + connect(instance_total_in, "Instance", instance_total, "Geometry"); + connect(instance_total, "Instance Count", duplicate_instances, "Amount"); bNode *geometry_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); - connect(geometry_in, "Instance", instance_on_point, "Instance"); + bNode *reset_transform = add_node_group(reset_instances_transform_tree); + connect(geometry_in, "Instance", reset_transform, "Instances"); + connect(reset_transform, "Instances", instance_on_point, "Instance"); + + bNode *instances_source_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + bNode *apply_transform = add_node_group(sample_apply_instances_transform_tree); + connect(instance_on_point, "Instances", apply_transform, "Instances"); + connect(instances_source_in, "Instance", apply_transform, "Transform Source"); + connect(duplicate_instances, "Duplicate Index", apply_transform, "Instances Index"); bNode *instances_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); - connect(instance_on_point, "Instances", instances_out, "Instances"); + connect(apply_transform, "Instances", instances_out, "Instances"); bke::node_field_inferencing::update_field_inferencing(*node_tree); cached_node_trees.instances_on_points = node_tree; -- 2.30.2 From 966611e453956224ab356d3b2659a1b122427520 Mon Sep 17 00:00:00 2001 From: illua1 Date: Fri, 28 Apr 2023 20:13:09 +0300 Subject: [PATCH 18/30] plans... --- .../blenloader/intern/versioning_common.cc | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 958d1e9f6fd..52b042791f1 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -44,6 +44,9 @@ #include "MEM_guardedalloc.h" +#include "RNA_access.h" +#include "RNA_prototypes.h" + #include "versioning_common.h" using blender::Map; @@ -1195,6 +1198,7 @@ static bNodeTree *instances_on_faces(const Span objects, } static void object_push_instances_modifier(const StringRefNull name, + Main * /*bmain*/, Object &object, bNodeTree &node_tree) { @@ -1219,15 +1223,26 @@ static void object_push_instances_modifier(const StringRefNull name, continue; } /* TODO */ - // IDProperty *new_prop = IDP_GetPropertyFromGroup(nmd.settings.properties, - // socket->identifier); BLI_assert(old_prop != nullptr); IDP_CopyPropertyContent(new_prop, - // old_prop); + // copy DNA value in idprop + // change fcurve to remap this from object rna prop to modifier id property + // + + /* + IDProperty *object_prop_group = IDP_GetProperties(&object.id, false); + BLI_assert(object_prop_group != nullptr); + IDProperty *old_prop = IDP_GetPropertyFromGroup(object_prop_group, socket->identifier); + BLI_assert(old_prop != nullptr); + + IDProperty *new_prop = IDP_GetPropertyFromGroup(nmd.settings.properties, + socket->identifier); BLI_assert(new_prop != nullptr); + + IDP_CopyPropertyContent(new_prop, old_prop); + */ } } } // namespace replace_legacy_instances -/* TODO: objects in parent object space or in global? */ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) { using namespace blender; @@ -1280,11 +1295,9 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) else { instances_node_tree = instances_on_faces(objects, tree_name, bmain, cached_node_trees); } - object_push_instances_modifier(modifier_name, *parent, *instances_node_tree); + object_push_instances_modifier(bmain, modifier_name, *parent, *instances_node_tree); parent->transflag = 0; - - DEG_id_tag_update(&parent->id, ID_RECALC_GEOMETRY); } printf("HELLO!\n"); -- 2.30.2 From 444e8ed8c8d24db4e77e1fcee70516765bd3e384 Mon Sep 17 00:00:00 2001 From: illua1 Date: Sat, 29 Apr 2023 01:26:49 +0300 Subject: [PATCH 19/30] progress --- source/blender/blenloader/intern/versioning_common.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 52b042791f1..915d3aebbe3 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -1295,7 +1295,7 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) else { instances_node_tree = instances_on_faces(objects, tree_name, bmain, cached_node_trees); } - object_push_instances_modifier(bmain, modifier_name, *parent, *instances_node_tree); + object_push_instances_modifier(modifier_name, bmain, *parent, *instances_node_tree); parent->transflag = 0; } -- 2.30.2 From 462eca4dc146c1a7830f2aa24ff81469f5903d1e Mon Sep 17 00:00:00 2001 From: illua1 Date: Mon, 1 May 2023 23:01:08 +0300 Subject: [PATCH 20/30] progress --- .../blenloader/intern/versioning_common.cc | 54 ++++++++++++------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 915d3aebbe3..7e00f0b7634 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -1197,12 +1197,18 @@ static bNodeTree *instances_on_faces(const Span objects, return node_tree; } +static void move_rna_to_id_prop(PropertyRNA &src, IDProperty &dst, Main *bmain) +{ + // values = (float)RNA_property_boolean_get(ptr, prop); + + // BKE_animdata_fix_paths_rename_all +} + static void object_push_instances_modifier(const StringRefNull name, - Main * /*bmain*/, + Main *bmain, Object &object, bNodeTree &node_tree) { - /* TODO: Move setings from object properties in modifier panel. */ ModifierData *md = BKE_modifier_new(eModifierType_Nodes); NodesModifierData &nmd = *reinterpret_cast(md); @@ -1217,27 +1223,35 @@ static void object_push_instances_modifier(const StringRefNull name, MOD_nodes_update_interface(&object, &nmd); - int index = 0; - LISTBASE_FOREACH_INDEX (bNodeSocket *, socket, &node_tree.inputs, index) { - if (index == 0) { - continue; - } - /* TODO */ - // copy DNA value in idprop - // change fcurve to remap this from object rna prop to modifier id property - // + PointerRNA object_ptr; + RNA_pointer_create(&object.id, &RNA_Object, &object, &object_ptr); - /* - IDProperty *object_prop_group = IDP_GetProperties(&object.id, false); - BLI_assert(object_prop_group != nullptr); - IDProperty *old_prop = IDP_GetPropertyFromGroup(object_prop_group, socket->identifier); - BLI_assert(old_prop != nullptr); + static const Map socket_legacy_name_maping = []() -> auto + { + Map name_mapping; - IDProperty *new_prop = IDP_GetPropertyFromGroup(nmd.settings.properties, - socket->identifier); BLI_assert(new_prop != nullptr); + name_mapping.add("Viewport", "show_instancer_for_viewport"); + name_mapping.add("Render", "show_instancer_for_render"); - IDP_CopyPropertyContent(new_prop, old_prop); - */ + name_mapping.add("Scale by Face Size", "use_instance_faces_scale"); + name_mapping.add("Factor", "instance_faces_scale"); + + name_mapping.add("Aling to Vertex Normal", "use_instance_vertices_rotation"); + return name_mapping; + } + (); + + node_tree.ensure_topology_cache(); + for (const bNodeSocket *socket : node_tree.interface_inputs().drop_front(1)) { + + IDProperty *dst = IDP_GetPropertyFromGroup(nmd.settings.properties, socket->identifier); + BLI_assert(dst != nullptr); + + const StringRef legacy_prop_name = socket_legacy_name_maping.lookup(socket->name); + PropertyRNA *src = RNA_struct_find_property(&object_ptr, legacy_prop_name.data()); + BLI_assert(src != nullptr); + + move_rna_to_id_prop(*src, *dst, bmain); } } -- 2.30.2 From 79ca64b5970482bbd6f9e1e359c743a82b2181e9 Mon Sep 17 00:00:00 2001 From: illua1 Date: Mon, 8 May 2023 15:07:42 +0300 Subject: [PATCH 21/30] progress --- .../blenloader/intern/versioning_300.cc | 2 +- .../blenloader/intern/versioning_common.cc | 38 ++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_300.cc b/source/blender/blenloader/intern/versioning_300.cc index 42e4a3df2cd..b46f542e362 100644 --- a/source/blender/blenloader/intern/versioning_300.cc +++ b/source/blender/blenloader/intern/versioning_300.cc @@ -1367,7 +1367,7 @@ void do_versions_after_linking_300(FileData * /*fd*/, Main *bmain) } } - if (!MAIN_VERSION_ATLEAST(bmain, 306, 7)) { + if (!MAIN_VERSION_ATLEAST(bmain, 306, 8)) { /* TODO: (bmain, 400, 0). */ remove_legacy_instances_on(bmain, bmain->objects); } diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 3dfc9270598..0e3629a869a 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -1198,11 +1198,36 @@ static bNodeTree *instances_on_faces(const Span objects, return node_tree; } -static void move_rna_to_id_prop(PropertyRNA &src, IDProperty &dst, Main *bmain) +static void move_rna_to_id_prop(PropertyRNA &src_prop, + PointerRNA &src_ptr, + IDProperty &dst, + Object &object) { - // values = (float)RNA_property_boolean_get(ptr, prop); + switch (IDP_ui_data_type(&dst)) { + case IDP_UI_DATA_TYPE_INT: { + const int values = RNA_property_int_get(&src_ptr, &src_prop); + *reinterpret_cast(&IDP_Int(&dst)) = values; + break; + } + case IDP_UI_DATA_TYPE_FLOAT: { + const int values = RNA_property_float_get(&src_ptr, &src_prop); + *reinterpret_cast(&IDP_Float(&dst)) = values; + break; + } + case IDP_UI_DATA_TYPE_BOOLEAN: { + const bool values = RNA_property_boolean_get(&src_ptr, &src_prop); + *reinterpret_cast(&IDP_Bool(&dst)) = values; + break; + } + case IDP_UI_DATA_TYPE_UNSUPPORTED: + case IDP_UI_DATA_TYPE_STRING: + case IDP_UI_DATA_TYPE_ID: + BLI_assert_unreachable(); + break; + } - // BKE_animdata_fix_paths_rename_all + // BKE_animdata_fix_paths_rename_all(&object.id, "modifiers['Instances on Points 3.6'][", + // "use_instance_vertices_rotation", "Input_3"); } static void object_push_instances_modifier(const StringRefNull name, @@ -1249,10 +1274,11 @@ static void object_push_instances_modifier(const StringRefNull name, BLI_assert(dst != nullptr); const StringRef legacy_prop_name = socket_legacy_name_maping.lookup(socket->name); - PropertyRNA *src = RNA_struct_find_property(&object_ptr, legacy_prop_name.data()); - BLI_assert(src != nullptr); - move_rna_to_id_prop(*src, *dst, bmain); + PropertyRNA *obkect_prop = RNA_struct_find_property(&object_ptr, legacy_prop_name.data()); + BLI_assert(obkect_prop != nullptr); + + move_rna_to_id_prop(*obkect_prop, object_ptr, *dst, object); } } -- 2.30.2 From 0a715bcc870fb1610e9d7e138394d16f58ec289b Mon Sep 17 00:00:00 2001 From: Iliya Katueshenock Date: Sun, 21 May 2023 02:34:31 +0300 Subject: [PATCH 22/30] progress --- .../blenloader/intern/versioning_common.cc | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 34dcf202fd7..fc91eb8c066 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -1262,9 +1262,7 @@ static void copy_value_rna_to_id(PropertyRNA &src_prop, PointerRNA &src_ptr, IDP *reinterpret_cast(&IDP_Bool(&dst)) = values; break; } - case IDP_UI_DATA_TYPE_UNSUPPORTED: - case IDP_UI_DATA_TYPE_STRING: - case IDP_UI_DATA_TYPE_ID: + case default: BLI_assert_unreachable(); break; } @@ -1320,17 +1318,16 @@ static void object_push_instances_modifier(const StringRefNull name, copy_value_rna_to_id(*obkect_prop, object_ptr, *dst); - std::string rna_path_to_new = "modifiers[\"" + std::string(name) + "\"][\"" + - std::string(socket->identifier) + "\"]"; + std::string new_rna_path = "modifiers[\"" + std::string(name) + "\"][\"" + + std::string(socket->identifier) + "\"]"; AnimationBasePathChange animation_to_move; animation_to_move.src_basepath = legacy_prop_name.data(); - animation_to_move.dst_basepath = rna_path_to_new.c_str(); - legacy_rna_paths.append(std::move(rna_path_to_new)); + animation_to_move.dst_basepath = new_rna_path.c_str(); + legacy_rna_paths.append(std::move(new_rna_path)); animation_data_move.append(animation_to_move); } ListBase change_list = {nullptr, nullptr}; - ; for (AnimationBasePathChange &animation_path : animation_data_move) { BLI_addtail(&change_list, &animation_path); } @@ -1396,6 +1393,4 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) parent->transflag = 0; } - - printf("HELLO!\n"); } -- 2.30.2 From 2880c1971348c3b2c1fba21c68743529bf6fbcca Mon Sep 17 00:00:00 2001 From: Iliya Katueshenock Date: Sat, 27 May 2023 18:26:54 +0300 Subject: [PATCH 23/30] progress --- source/blender/blenkernel/BKE_idprop.h | 2 +- source/blender/blenlib/BLI_index_range.hh | 3 +++ source/blender/blenlib/BLI_listbase.h | 16 ++++++++++++++++ source/blender/blenlib/intern/sort.c | 3 +++ .../blenloader/intern/versioning_common.cc | 17 +++++++---------- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/source/blender/blenkernel/BKE_idprop.h b/source/blender/blenkernel/BKE_idprop.h index 3af5703dcb1..f60223c4451 100644 --- a/source/blender/blenkernel/BKE_idprop.h +++ b/source/blender/blenkernel/BKE_idprop.h @@ -247,7 +247,7 @@ void IDP_ClearProperty(struct IDProperty *prop); void IDP_Reset(struct IDProperty *prop, const struct IDProperty *reference); #define IDP_Int(prop) ((prop)->data.val) -#define IDP_Bool(prop) ((prop)->data.val) +#define IDP_Bool(prop) (*(bool *)&(prop)->data.val) #define IDP_Array(prop) ((prop)->data.pointer) /* C11 const correctness for casts */ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) diff --git a/source/blender/blenlib/BLI_index_range.hh b/source/blender/blenlib/BLI_index_range.hh index 7f1266c6716..e72447fe648 100644 --- a/source/blender/blenlib/BLI_index_range.hh +++ b/source/blender/blenlib/BLI_index_range.hh @@ -1,3 +1,6 @@ +#if !defined(NOMINMAX) +# define NOMINMAX +#endif /* SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once diff --git a/source/blender/blenlib/BLI_listbase.h b/source/blender/blenlib/BLI_listbase.h index 3a8756c921d..66c9b8c70e5 100644 --- a/source/blender/blenlib/BLI_listbase.h +++ b/source/blender/blenlib/BLI_listbase.h @@ -9,6 +9,11 @@ #include "BLI_compiler_attrs.h" #include "BLI_utildefines.h" + +#ifdef __cplusplus +# include "BLI_span.hh" +#endif + #include "DNA_listBase.h" // struct ListBase; // struct LinkData; @@ -389,4 +394,15 @@ BLI_INLINE bool operator==(const ListBase &a, const ListBase &b) { return BLI_listbase_equal(&a, &b); } +namespace blender { + +template void span_to_list(MutableSpan &src, ListBase &dst) +{ + for (ItemT &item : src) { + BLI_addtail(&dst, &item); + } +} + +} // namespace blender + #endif diff --git a/source/blender/blenlib/intern/sort.c b/source/blender/blenlib/intern/sort.c index 4284c10ceac..1992584f393 100644 --- a/source/blender/blenlib/intern/sort.c +++ b/source/blender/blenlib/intern/sort.c @@ -164,6 +164,9 @@ loop: } } +#undef min +#undef swapcode + /* clang-format on */ #endif /* __GLIBC__ */ diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index fc91eb8c066..65e220fffa8 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -1244,25 +1244,25 @@ static bNodeTree *instances_on_faces(const Span objects, return node_tree; } -static void copy_value_rna_to_id(PropertyRNA &src_prop, PointerRNA &src_ptr, IDProperty &dst) +static void copy_rna_to_id(PropertyRNA &src_prop, PointerRNA &src_ptr, IDProperty &dst) { switch (IDP_ui_data_type(&dst)) { case IDP_UI_DATA_TYPE_INT: { const int values = RNA_property_int_get(&src_ptr, &src_prop); - *reinterpret_cast(&IDP_Int(&dst)) = values; + IDP_Int(&dst) = values; break; } case IDP_UI_DATA_TYPE_FLOAT: { const int values = RNA_property_float_get(&src_ptr, &src_prop); - *reinterpret_cast(&IDP_Float(&dst)) = values; + IDP_Float(&dst) = values; break; } case IDP_UI_DATA_TYPE_BOOLEAN: { const bool values = RNA_property_boolean_get(&src_ptr, &src_prop); - *reinterpret_cast(&IDP_Bool(&dst)) = values; + IDP_Bool(&dst) = values; break; } - case default: + default: BLI_assert_unreachable(); break; } @@ -1316,7 +1316,7 @@ static void object_push_instances_modifier(const StringRefNull name, PropertyRNA *obkect_prop = RNA_struct_find_property(&object_ptr, legacy_prop_name.data()); BLI_assert(obkect_prop != nullptr); - copy_value_rna_to_id(*obkect_prop, object_ptr, *dst); + copy_rna_to_id(*obkect_prop, object_ptr, *dst); std::string new_rna_path = "modifiers[\"" + std::string(name) + "\"][\"" + std::string(socket->identifier) + "\"]"; @@ -1328,10 +1328,7 @@ static void object_push_instances_modifier(const StringRefNull name, } ListBase change_list = {nullptr, nullptr}; - for (AnimationBasePathChange &animation_path : animation_data_move) { - BLI_addtail(&change_list, &animation_path); - } - + span_to_list(animation_data_move, change_list); BKE_animdata_transfer_by_basepath(bmain, &object.id, &object.id, &change_list); } -- 2.30.2 From 9d08f77845436fcf6f17a78ddde391e67392632b Mon Sep 17 00:00:00 2001 From: Iliya Katueshenock Date: Sun, 28 May 2023 00:13:12 +0300 Subject: [PATCH 24/30] progress --- source/blender/blenlib/BLI_winstuff.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/blender/blenlib/BLI_winstuff.h b/source/blender/blenlib/BLI_winstuff.h index 5ed64128a26..d3fb7d597ad 100644 --- a/source/blender/blenlib/BLI_winstuff.h +++ b/source/blender/blenlib/BLI_winstuff.h @@ -18,6 +18,8 @@ #include +#undef min +#undef max #undef rad #undef rad1 #undef rad2 -- 2.30.2 From c48bc8223c49d8076b023caae994dc2715ad4af3 Mon Sep 17 00:00:00 2001 From: Iliya Katueshenock Date: Sun, 28 May 2023 00:27:25 +0300 Subject: [PATCH 25/30] progress --- source/blender/blenlib/BLI_listbase.h | 2 +- source/blender/blenloader/intern/versioning_common.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/blender/blenlib/BLI_listbase.h b/source/blender/blenlib/BLI_listbase.h index 66c9b8c70e5..3aff052bf16 100644 --- a/source/blender/blenlib/BLI_listbase.h +++ b/source/blender/blenlib/BLI_listbase.h @@ -396,7 +396,7 @@ BLI_INLINE bool operator==(const ListBase &a, const ListBase &b) } namespace blender { -template void span_to_list(MutableSpan &src, ListBase &dst) +template void span_to_list(MutableSpan src, ListBase &dst) { for (ItemT &item : src) { BLI_addtail(&dst, &item); diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 65e220fffa8..a87243e1da8 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -1328,7 +1328,7 @@ static void object_push_instances_modifier(const StringRefNull name, } ListBase change_list = {nullptr, nullptr}; - span_to_list(animation_data_move, change_list); + blender::span_to_list(animation_data_move.as_mutable_span(), change_list); BKE_animdata_transfer_by_basepath(bmain, &object.id, &object.id, &change_list); } -- 2.30.2 From 2206f49e8fde0feb64757566131456e8f5bcb8cb Mon Sep 17 00:00:00 2001 From: Iliya Katueshenock Date: Sun, 28 May 2023 02:00:06 +0300 Subject: [PATCH 26/30] progress --- source/blender/blenlib/BLI_index_range.hh | 3 --- source/blender/blenlib/BLI_utildefines.h | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/source/blender/blenlib/BLI_index_range.hh b/source/blender/blenlib/BLI_index_range.hh index e72447fe648..7f1266c6716 100644 --- a/source/blender/blenlib/BLI_index_range.hh +++ b/source/blender/blenlib/BLI_index_range.hh @@ -1,6 +1,3 @@ -#if !defined(NOMINMAX) -# define NOMINMAX -#endif /* SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index bfa5102f6b1..0d372d17e26 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -5,6 +5,8 @@ #ifndef __BLI_UTILDEFINES_H__ #define __BLI_UTILDEFINES_H__ + +#include /** \file * \ingroup bli */ @@ -870,3 +872,18 @@ extern bool BLI_memory_is_zero(const void *arr, size_t arr_size); #endif #endif /* __BLI_UTILDEFINES_H__ */ + +#undef min +#undef max +#undef rad +#undef rad1 +#undef rad2 +#undef rad3 +#undef vec +#undef rect +#undef rct1 +#undef rct2 +#undef near + +#undef small +#undef MEM_CacheLimiterHandle \ No newline at end of file -- 2.30.2 From 7d1e58f12b607c1321a114f7a668b4522d650af9 Mon Sep 17 00:00:00 2001 From: Iliya Katueshenock Date: Sun, 28 May 2023 02:05:04 +0300 Subject: [PATCH 27/30] revert last commits --- source/blender/blenkernel/BKE_idprop.h | 2 +- source/blender/blenlib/BLI_listbase.h | 16 ---------------- source/blender/blenlib/BLI_utildefines.h | 17 ----------------- source/blender/blenlib/BLI_winstuff.h | 2 -- source/blender/blenlib/intern/sort.c | 3 --- .../blenloader/intern/versioning_common.cc | 17 ++++++++++------- 6 files changed, 11 insertions(+), 46 deletions(-) diff --git a/source/blender/blenkernel/BKE_idprop.h b/source/blender/blenkernel/BKE_idprop.h index f60223c4451..3af5703dcb1 100644 --- a/source/blender/blenkernel/BKE_idprop.h +++ b/source/blender/blenkernel/BKE_idprop.h @@ -247,7 +247,7 @@ void IDP_ClearProperty(struct IDProperty *prop); void IDP_Reset(struct IDProperty *prop, const struct IDProperty *reference); #define IDP_Int(prop) ((prop)->data.val) -#define IDP_Bool(prop) (*(bool *)&(prop)->data.val) +#define IDP_Bool(prop) ((prop)->data.val) #define IDP_Array(prop) ((prop)->data.pointer) /* C11 const correctness for casts */ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) diff --git a/source/blender/blenlib/BLI_listbase.h b/source/blender/blenlib/BLI_listbase.h index 3aff052bf16..3a8756c921d 100644 --- a/source/blender/blenlib/BLI_listbase.h +++ b/source/blender/blenlib/BLI_listbase.h @@ -9,11 +9,6 @@ #include "BLI_compiler_attrs.h" #include "BLI_utildefines.h" - -#ifdef __cplusplus -# include "BLI_span.hh" -#endif - #include "DNA_listBase.h" // struct ListBase; // struct LinkData; @@ -394,15 +389,4 @@ BLI_INLINE bool operator==(const ListBase &a, const ListBase &b) { return BLI_listbase_equal(&a, &b); } -namespace blender { - -template void span_to_list(MutableSpan src, ListBase &dst) -{ - for (ItemT &item : src) { - BLI_addtail(&dst, &item); - } -} - -} // namespace blender - #endif diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index 0d372d17e26..bfa5102f6b1 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -5,8 +5,6 @@ #ifndef __BLI_UTILDEFINES_H__ #define __BLI_UTILDEFINES_H__ - -#include /** \file * \ingroup bli */ @@ -872,18 +870,3 @@ extern bool BLI_memory_is_zero(const void *arr, size_t arr_size); #endif #endif /* __BLI_UTILDEFINES_H__ */ - -#undef min -#undef max -#undef rad -#undef rad1 -#undef rad2 -#undef rad3 -#undef vec -#undef rect -#undef rct1 -#undef rct2 -#undef near - -#undef small -#undef MEM_CacheLimiterHandle \ No newline at end of file diff --git a/source/blender/blenlib/BLI_winstuff.h b/source/blender/blenlib/BLI_winstuff.h index d3fb7d597ad..5ed64128a26 100644 --- a/source/blender/blenlib/BLI_winstuff.h +++ b/source/blender/blenlib/BLI_winstuff.h @@ -18,8 +18,6 @@ #include -#undef min -#undef max #undef rad #undef rad1 #undef rad2 diff --git a/source/blender/blenlib/intern/sort.c b/source/blender/blenlib/intern/sort.c index 1992584f393..4284c10ceac 100644 --- a/source/blender/blenlib/intern/sort.c +++ b/source/blender/blenlib/intern/sort.c @@ -164,9 +164,6 @@ loop: } } -#undef min -#undef swapcode - /* clang-format on */ #endif /* __GLIBC__ */ diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index a87243e1da8..fc91eb8c066 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -1244,25 +1244,25 @@ static bNodeTree *instances_on_faces(const Span objects, return node_tree; } -static void copy_rna_to_id(PropertyRNA &src_prop, PointerRNA &src_ptr, IDProperty &dst) +static void copy_value_rna_to_id(PropertyRNA &src_prop, PointerRNA &src_ptr, IDProperty &dst) { switch (IDP_ui_data_type(&dst)) { case IDP_UI_DATA_TYPE_INT: { const int values = RNA_property_int_get(&src_ptr, &src_prop); - IDP_Int(&dst) = values; + *reinterpret_cast(&IDP_Int(&dst)) = values; break; } case IDP_UI_DATA_TYPE_FLOAT: { const int values = RNA_property_float_get(&src_ptr, &src_prop); - IDP_Float(&dst) = values; + *reinterpret_cast(&IDP_Float(&dst)) = values; break; } case IDP_UI_DATA_TYPE_BOOLEAN: { const bool values = RNA_property_boolean_get(&src_ptr, &src_prop); - IDP_Bool(&dst) = values; + *reinterpret_cast(&IDP_Bool(&dst)) = values; break; } - default: + case default: BLI_assert_unreachable(); break; } @@ -1316,7 +1316,7 @@ static void object_push_instances_modifier(const StringRefNull name, PropertyRNA *obkect_prop = RNA_struct_find_property(&object_ptr, legacy_prop_name.data()); BLI_assert(obkect_prop != nullptr); - copy_rna_to_id(*obkect_prop, object_ptr, *dst); + copy_value_rna_to_id(*obkect_prop, object_ptr, *dst); std::string new_rna_path = "modifiers[\"" + std::string(name) + "\"][\"" + std::string(socket->identifier) + "\"]"; @@ -1328,7 +1328,10 @@ static void object_push_instances_modifier(const StringRefNull name, } ListBase change_list = {nullptr, nullptr}; - blender::span_to_list(animation_data_move.as_mutable_span(), change_list); + for (AnimationBasePathChange &animation_path : animation_data_move) { + BLI_addtail(&change_list, &animation_path); + } + BKE_animdata_transfer_by_basepath(bmain, &object.id, &object.id, &change_list); } -- 2.30.2 From 7e6f7180c6d5a46901309c4d6b50a5eea87017b7 Mon Sep 17 00:00:00 2001 From: Iliya Katueshenock Date: Sun, 28 May 2023 02:38:19 +0300 Subject: [PATCH 28/30] progress --- source/blender/blenkernel/BKE_idprop.h | 2 +- .../blenloader/intern/versioning_common.cc | 30 +++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/source/blender/blenkernel/BKE_idprop.h b/source/blender/blenkernel/BKE_idprop.h index 3af5703dcb1..f60223c4451 100644 --- a/source/blender/blenkernel/BKE_idprop.h +++ b/source/blender/blenkernel/BKE_idprop.h @@ -247,7 +247,7 @@ void IDP_ClearProperty(struct IDProperty *prop); void IDP_Reset(struct IDProperty *prop, const struct IDProperty *reference); #define IDP_Int(prop) ((prop)->data.val) -#define IDP_Bool(prop) ((prop)->data.val) +#define IDP_Bool(prop) (*(bool *)&(prop)->data.val) #define IDP_Array(prop) ((prop)->data.pointer) /* C11 const correctness for casts */ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index fc91eb8c066..01c0428b25c 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -1244,30 +1244,37 @@ static bNodeTree *instances_on_faces(const Span objects, return node_tree; } -static void copy_value_rna_to_id(PropertyRNA &src_prop, PointerRNA &src_ptr, IDProperty &dst) +static void copy_rna_to_id(PropertyRNA &src_prop, PointerRNA &src_ptr, IDProperty &dst) { switch (IDP_ui_data_type(&dst)) { case IDP_UI_DATA_TYPE_INT: { const int values = RNA_property_int_get(&src_ptr, &src_prop); - *reinterpret_cast(&IDP_Int(&dst)) = values; + IDP_Int(&dst) = values; break; } case IDP_UI_DATA_TYPE_FLOAT: { const int values = RNA_property_float_get(&src_ptr, &src_prop); - *reinterpret_cast(&IDP_Float(&dst)) = values; + IDP_Float(&dst) = values; break; } case IDP_UI_DATA_TYPE_BOOLEAN: { const bool values = RNA_property_boolean_get(&src_ptr, &src_prop); - *reinterpret_cast(&IDP_Bool(&dst)) = values; + IDP_Bool(&dst) = values; break; } - case default: + default: BLI_assert_unreachable(); break; } } +template static void span_to_list(MutableSpan src, ListBase &dst) +{ + for (ItemT &item : src) { + BLI_addtail(&dst, &item); + } +} + static void object_push_instances_modifier(const StringRefNull name, Main *bmain, Object &object, @@ -1316,7 +1323,7 @@ static void object_push_instances_modifier(const StringRefNull name, PropertyRNA *obkect_prop = RNA_struct_find_property(&object_ptr, legacy_prop_name.data()); BLI_assert(obkect_prop != nullptr); - copy_value_rna_to_id(*obkect_prop, object_ptr, *dst); + copy_rna_to_id(*obkect_prop, object_ptr, *dst); std::string new_rna_path = "modifiers[\"" + std::string(name) + "\"][\"" + std::string(socket->identifier) + "\"]"; @@ -1328,10 +1335,7 @@ static void object_push_instances_modifier(const StringRefNull name, } ListBase change_list = {nullptr, nullptr}; - for (AnimationBasePathChange &animation_path : animation_data_move) { - BLI_addtail(&change_list, &animation_path); - } - + span_to_list(animation_data_move, change_list); BKE_animdata_transfer_by_basepath(bmain, &object.id, &object.id, &change_list); } @@ -1368,18 +1372,18 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) const bool on_vertices = (parent->transflag & OB_DUPLIVERTS) != 0; const StringRefNull parent_name(parent->id.name + 2); - const StringRefNull domain_name = on_vertices ? TIP_("Points") : TIP_("Faces"); + const StringRefNull domain_name = on_vertices ? N_("Points") : N_("Faces"); char tree_name[64]; BLI_snprintf(tree_name, sizeof(tree_name), - TIP_("Instances on %s of %s"), + N_("Instances on %s of %s"), domain_name.c_str(), parent_name.c_str()); char modifier_name[64]; BLI_snprintf( - modifier_name, sizeof(modifier_name), TIP_("Instances on %s 3.6"), domain_name.c_str()); + modifier_name, sizeof(modifier_name), N_("Instances on %s 3.6"), domain_name.c_str()); using namespace replace_legacy_instances; bNodeTree *instances_node_tree; -- 2.30.2 From 97c6cd26a2e6c480a88b400627c5ca9900e84e04 Mon Sep 17 00:00:00 2001 From: Iliya Katueshenock Date: Sun, 28 May 2023 18:18:04 +0300 Subject: [PATCH 29/30] progress --- .../blenloader/intern/versioning_common.cc | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index 01c0428b25c..f87ec3894a6 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -346,7 +346,7 @@ void add_realize_instances_before_socket(bNodeTree *ntree, } } -namespace replace_legacy_instances { +namespace replace_legacy_instances_imp { using namespace blender; @@ -399,6 +399,8 @@ struct RegularNodeTrees { bNodeTree *instances_on_faces = nullptr; bNodeTree *view_geometry = nullptr; + /* TODO */ + bNodeTree *invert_rotation = nullptr; bNodeTree *reset_instances_transform = nullptr; bNodeTree *sample_apply_instances_transform = nullptr; @@ -1275,7 +1277,7 @@ template static void span_to_list(MutableSpan src, ListBa } } -static void object_push_instances_modifier(const StringRefNull name, +static void object_push_instances_modifier(const StringRefNull modifier_name, Main *bmain, Object &object, bNodeTree &node_tree) @@ -1286,7 +1288,7 @@ static void object_push_instances_modifier(const StringRefNull name, nmd.node_group = &node_tree; id_us_plus(&node_tree.id); - STRNCPY(nmd.modifier.name, name.c_str()); + STRNCPY(nmd.modifier.name, modifier_name.c_str()); BKE_modifier_unique_name(&object.modifiers, md); BLI_addtail(&object.modifiers, md); @@ -1313,37 +1315,42 @@ static void object_push_instances_modifier(const StringRefNull name, (); Vector legacy_rna_paths; - Vector animation_data_move; + Vector animation_data; node_tree.ensure_topology_cache(); for (const bNodeSocket *socket : node_tree.interface_inputs().drop_front(1)) { - IDProperty *dst = IDP_GetPropertyFromGroup(nmd.settings.properties, socket->identifier); - BLI_assert(dst != nullptr); + IDProperty *modifier_prop = IDP_GetPropertyFromGroup(nmd.settings.properties, + socket->identifier); + BLI_assert(modifier_prop != nullptr); const StringRef legacy_prop_name = socket_legacy_name_maping.lookup(socket->name); - PropertyRNA *obkect_prop = RNA_struct_find_property(&object_ptr, legacy_prop_name.data()); - BLI_assert(obkect_prop != nullptr); + PropertyRNA *object_prop = RNA_struct_find_property(&object_ptr, legacy_prop_name.data()); + BLI_assert(object_prop != nullptr); - copy_rna_to_id(*obkect_prop, object_ptr, *dst); + copy_rna_to_id(*object_prop, object_ptr, *modifier_prop); + + std::stringstream new_rna_path_stream; + new_rna_path_stream << "modifiers[\"" << nmd.modifier.name << "\"][\"" << socket->identifier + << "\"]"; + std::string new_rna_path = new_rna_path_stream.str(); - std::string new_rna_path = "modifiers[\"" + std::string(name) + "\"][\"" + - std::string(socket->identifier) + "\"]"; AnimationBasePathChange animation_to_move; animation_to_move.src_basepath = legacy_prop_name.data(); animation_to_move.dst_basepath = new_rna_path.c_str(); legacy_rna_paths.append(std::move(new_rna_path)); - animation_data_move.append(animation_to_move); + animation_data.append(animation_to_move); } ListBase change_list = {nullptr, nullptr}; - span_to_list(animation_data_move, change_list); + span_to_list(animation_data, change_list); BKE_animdata_transfer_by_basepath(bmain, &object.id, &object.id, &change_list); } -} // namespace replace_legacy_instances +} // namespace replace_legacy_instances_imp void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) { using namespace blender; + using namespace replace_legacy_instances_imp; MultiValueMap parents_map; @@ -1364,7 +1371,7 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) } /* Not a static. Pointers are valid only during one project session. */ - replace_legacy_instances::RegularNodeTrees cached_node_trees; + RegularNodeTrees cached_node_trees; for (auto &&[parent, objects] : parents_map.items()) { @@ -1385,7 +1392,6 @@ void remove_legacy_instances_on(Main *bmain, ListBase &lb_objects) BLI_snprintf( modifier_name, sizeof(modifier_name), N_("Instances on %s 3.6"), domain_name.c_str()); - using namespace replace_legacy_instances; bNodeTree *instances_node_tree; if (on_vertices) { instances_node_tree = instances_on_points(objects, tree_name, bmain, cached_node_trees); -- 2.30.2 From 516d571da4fba480c9525503b063bda5bcd75038 Mon Sep 17 00:00:00 2001 From: Iliya Katueshenock Date: Thu, 1 Jun 2023 02:43:45 +0300 Subject: [PATCH 30/30] progress --- .../blenloader/intern/versioning_common.cc | 140 ++++++++++++------ 1 file changed, 92 insertions(+), 48 deletions(-) diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc index f87ec3894a6..dbaf0495b24 100644 --- a/source/blender/blenloader/intern/versioning_common.cc +++ b/source/blender/blenloader/intern/versioning_common.cc @@ -399,7 +399,6 @@ struct RegularNodeTrees { bNodeTree *instances_on_faces = nullptr; bNodeTree *view_geometry = nullptr; - /* TODO */ bNodeTree *invert_rotation = nullptr; bNodeTree *reset_instances_transform = nullptr; @@ -422,6 +421,67 @@ static void for_node_storage(bNodeTree *tree, } } +static bNodeTree *invert_rotation_tree(Main *bmain, RegularNodeTrees &cached_node_trees) +{ + if (cached_node_trees.invert_rotation != nullptr) { + return cached_node_trees.invert_rotation; + } + + bNodeTree *node_tree = ntreeAddTree(bmain, "InvertRotation", "GeometryNodeTree"); + + ntreeAddSocketInterface(node_tree, SOCK_IN, "NodeSocketVector", "Rotation"); + + ntreeAddSocketInterface(node_tree, SOCK_OUT, "NodeSocketVector", "Rotation"); + + const auto connect = [node_tree](bNode *node_out, + const StringRefNull name_out, + bNode *node_in, + const StringRefNull name_in) { + BLI_assert(node_out != node_in); + bNodeSocket &out = node_output_by_name(name_out, node_out); + bNodeSocket &in = node_input_by_name(name_in, node_in); + nodeAddLink(node_tree, node_out, &out, node_in, &in); + }; + + bNode *rotation_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); + + bNode *negative_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + negative_rotation->custom1 = NODE_VECTOR_MATH_SCALE; + negative_rotation->typeinfo->updatefunc(node_tree, negative_rotation); + node_input_by_name("Scale", negative_rotation) + .default_value_typed() + ->value = -1.0f; + connect(rotation_in, "Rotation", negative_rotation, "Vector"); + + bNode *separate_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_SEPXYZ); + connect(negative_rotation, "Vector", separate_rotation, "Vector"); + + bNode *combine_rotation_on_z = nodeAddStaticNode(nullptr, node_tree, SH_NODE_COMBXYZ); + connect(separate_rotation, "Z", combine_rotation_on_z, "Z"); + bNode *rotate_z = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ROTATE_EULER); + connect(combine_rotation_on_z, "Vector", rotate_z, "Rotate By"); + + bNode *combine_rotation_on_y = nodeAddStaticNode(nullptr, node_tree, SH_NODE_COMBXYZ); + connect(separate_rotation, "Y", combine_rotation_on_y, "Y"); + bNode *rotate_y = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ROTATE_EULER); + connect(combine_rotation_on_y, "Vector", rotate_y, "Rotate By"); + + bNode *combine_rotation_on_x = nodeAddStaticNode(nullptr, node_tree, SH_NODE_COMBXYZ); + connect(separate_rotation, "X", combine_rotation_on_x, "X"); + bNode *rotate_x = nodeAddStaticNode(nullptr, node_tree, FN_NODE_ROTATE_EULER); + connect(combine_rotation_on_x, "Vector", rotate_x, "Rotate By"); + + bNode *rotation_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); + + connect(rotate_z, "Rotation", rotate_y, "Rotation"); + connect(rotate_y, "Rotation", rotate_x, "Rotation"); + connect(rotate_x, "Rotation", rotation_out, "Rotation"); + + bke::node_field_inferencing::update_field_inferencing(*node_tree); + cached_node_trees.invert_rotation = node_tree; + return node_tree; +} + static bNodeTree *view_geometry_tree(Main *bmain, RegularNodeTrees &cached_node_trees) { if (cached_node_trees.view_geometry != nullptr) { @@ -516,11 +576,24 @@ static bNodeTree *reset_instances_transform_tree(Main *bmain, RegularNodeTrees & const StringRefNull name_out, bNode *node_in, const StringRefNull name_in) { + BLI_assert(node_out != node_in); bNodeSocket &out = node_output_by_name(name_out, node_out); bNodeSocket &in = node_input_by_name(name_in, node_in); nodeAddLink(node_tree, node_out, &out, node_in, &in); }; + const auto add_node_group = + [&](const FunctionRef + tree_func) -> bNode * { + bNodeTree *tree = tree_func(bmain, cached_node_trees); + bNode *group = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP); + group->id = &tree->id; + id_us_plus(&tree->id); + bke::node_field_inferencing::update_field_inferencing(*node_tree); + nodes::update_node_declaration_and_sockets(*node_tree, *group); + return group; + }; + const auto set_in = [](bNode *node, StringRefNull input_name, auto value) { if constexpr (std::is_same_v) { node_input_by_name(input_name, node).default_value_typed()->value = @@ -546,21 +619,15 @@ static bNodeTree *reset_instances_transform_tree(Main *bmain, RegularNodeTrees & bNode *instances_in = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_INPUT); bNode *scale_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_SCALE_INSTANCES); - connect(instances_in, "Instances", scale_instances, "Instances"); set_in(scale_instances, "Local Space", false); + connect(instances_in, "Instances", scale_instances, "Instances"); - bNode *rotate_z_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); - connect(scale_instances, "Instances", rotate_z_instances, "Instances"); - set_in(rotate_z_instances, "Local Space", false); - bNode *rotate_y_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); - connect(rotate_z_instances, "Instances", rotate_y_instances, "Instances"); - set_in(rotate_y_instances, "Local Space", false); - bNode *rotate_x_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); - connect(rotate_y_instances, "Instances", rotate_x_instances, "Instances"); - set_in(rotate_x_instances, "Local Space", false); + bNode *rotate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_ROTATE_INSTANCES); + set_in(rotate_instances, "Local Space", false); + connect(scale_instances, "Instances", rotate_instances, "Instances"); bNode *translate_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_TRANSLATE_INSTANCES); - connect(rotate_x_instances, "Instances", translate_instances, "Instances"); + connect(rotate_instances, "Instances", translate_instances, "Instances"); set_in(translate_instances, "Local Space", false); bNode *geometry_out = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); @@ -572,36 +639,21 @@ static bNodeTree *reset_instances_transform_tree(Main *bmain, RegularNodeTrees & set_in(invert_size, "Vector", float3{1.0f}); connect(invert_size, "Vector", scale_instances, "Scale"); - bNode *flip_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); - flip_rotation->custom1 = NODE_VECTOR_MATH_SCALE; - flip_rotation->typeinfo->updatefunc(node_tree, flip_rotation); - set_in(flip_rotation, "Scale", -1.0f); + bNode *invert_rotation = add_node_group(invert_rotation_tree); + connect(invert_rotation, "Rotation", rotate_instances, "Rotation"); - bNode *separate_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_SEPXYZ); - connect(flip_rotation, "Vector", separate_rotation, "Vector"); - - bNode *combine_z_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_COMBXYZ); - connect(separate_rotation, "Z", combine_z_rotation, "Z"); - connect(combine_z_rotation, "Vector", rotate_z_instances, "Rotation"); - bNode *combine_y_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_COMBXYZ); - connect(separate_rotation, "Y", combine_y_rotation, "Y"); - connect(combine_y_rotation, "Vector", rotate_y_instances, "Rotation"); - bNode *combine_x_rotation = nodeAddStaticNode(nullptr, node_tree, SH_NODE_COMBXYZ); - connect(separate_rotation, "Z", combine_x_rotation, "Z"); - connect(combine_x_rotation, "Vector", rotate_x_instances, "Rotation"); - - bNode *go_back_position = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); - go_back_position->custom1 = NODE_VECTOR_MATH_SCALE; - go_back_position->typeinfo->updatefunc(node_tree, go_back_position); - connect(go_back_position, "Vector", translate_instances, "Translation"); - set_in(go_back_position, "Scale", -1.0f); + bNode *invert_position = nodeAddStaticNode(nullptr, node_tree, SH_NODE_VECTOR_MATH); + invert_position->custom1 = NODE_VECTOR_MATH_SCALE; + invert_position->typeinfo->updatefunc(node_tree, invert_position); + set_in(invert_position, "Scale", -1.0f); + connect(invert_position, "Vector", translate_instances, "Translation"); bNode *set_scale = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_SCALE); connect(set_scale, "Scale", invert_size, "Vector_001"); bNode *set_rotation = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_INSTANCE_ROTATION); - connect(set_rotation, "Rotation", flip_rotation, "Vector"); + connect(set_rotation, "Rotation", invert_rotation, "Rotation"); bNode *set_position = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_INPUT_POSITION); - connect(set_position, "Position", go_back_position, "Vector"); + connect(set_position, "Position", invert_position, "Vector"); bke::node_field_inferencing::update_field_inferencing(*node_tree); cached_node_trees.reset_instances_transform = node_tree; @@ -1159,11 +1211,8 @@ static bNodeTree *instances_on_points(const Span objects, connect(instansing_group, "Instances", join_parent, "Geometry"); connect(view_switch_group, "Geometry", join_parent, "Geometry"); - bNode *realize_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_REALIZE_INSTANCES); - connect(join_parent, "Geometry", realize_instances, "Geometry"); - bNode *group_output = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); - connect(realize_instances, "Geometry", group_output, "Geometry"); + connect(join_parent, "Geometry", group_output, "Geometry"); return node_tree; } @@ -1238,11 +1287,8 @@ static bNodeTree *instances_on_faces(const Span objects, connect(scale_instances, "Instances", join_parent, "Geometry"); connect(view_switch_group, "Geometry", join_parent, "Geometry"); - bNode *realize_instances = nodeAddStaticNode(nullptr, node_tree, GEO_NODE_REALIZE_INSTANCES); - connect(join_parent, "Geometry", realize_instances, "Geometry"); - bNode *group_output = nodeAddStaticNode(nullptr, node_tree, NODE_GROUP_OUTPUT); - connect(realize_instances, "Geometry", group_output, "Geometry"); + connect(join_parent, "Geometry", group_output, "Geometry"); return node_tree; } @@ -1299,8 +1345,7 @@ static void object_push_instances_modifier(const StringRefNull modifier_name, PointerRNA object_ptr; RNA_pointer_create(&object.id, &RNA_Object, &object, &object_ptr); - static const Map socket_legacy_name_maping = []() -> auto - { + static const Map socket_legacy_name_maping = []() { Map name_mapping; name_mapping.add("Viewport", "show_instancer_for_viewport"); @@ -1311,8 +1356,7 @@ static void object_push_instances_modifier(const StringRefNull modifier_name, name_mapping.add("Aling to Vertex Normal", "use_instance_vertices_rotation"); return name_mapping; - } - (); + }(); Vector legacy_rna_paths; Vector animation_data; -- 2.30.2