[Bf-blender-cvs] [d845ba481c6] master: Fix T91826: Inconsistent node socket name identifier separator

Hans Goudey noreply at git.blender.org
Fri Nov 12 19:22:52 CET 2021


Commit: d845ba481c6d2ef50bf87bec834555d5e1faf64c
Author: Hans Goudey
Date:   Fri Nov 12 12:22:43 2021 -0600
Branches: master
https://developer.blender.org/rBd845ba481c6d2ef50bf87bec834555d5e1faf64c

Fix T91826: Inconsistent node socket name identifier separator

Previously both `.` and `_` were used as separators when finding
a unique name for a socket. This removes the use of `.`, since `_`
was more common. It also does versioning for all of a file's node
trees to make sure that they all use the `_` convention.

Differential Revision: https://developer.blender.org/D13181

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

M	source/blender/blenkernel/intern/node.cc
M	source/blender/blenloader/intern/versioning_300.c
M	source/blender/blenloader/intern/versioning_common.cc
M	source/blender/blenloader/intern/versioning_common.h

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

diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc
index 984029a58b4..85ad233ab4f 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -1542,7 +1542,7 @@ static bNodeSocket *make_socket(bNodeTree *ntree,
   }
   /* make the identifier unique */
   BLI_uniquename_cb(
-      unique_identifier_check, lb, "socket", '.', auto_identifier, sizeof(auto_identifier));
+      unique_identifier_check, lb, "socket", '_', auto_identifier, sizeof(auto_identifier));
 
   bNodeSocket *sock = (bNodeSocket *)MEM_callocN(sizeof(bNodeSocket), "sock");
   sock->in_out = in_out;
diff --git a/source/blender/blenloader/intern/versioning_300.c b/source/blender/blenloader/intern/versioning_300.c
index 6d44017157c..5296a0b4f82 100644
--- a/source/blender/blenloader/intern/versioning_300.c
+++ b/source/blender/blenloader/intern/versioning_300.c
@@ -783,7 +783,6 @@ void do_versions_after_linking_300(Main *bmain, ReportList *UNUSED(reports))
    */
   {
     /* Keep this block, even when empty. */
-
   }
 }
 
@@ -1273,6 +1272,18 @@ static void version_geometry_nodes_set_position_node_offset(bNodeTree *ntree)
   }
 }
 
+static void version_node_tree_socket_id_delim(bNodeTree *ntree)
+{
+  LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
+    LISTBASE_FOREACH (bNodeSocket *, socket, &node->inputs) {
+      version_node_socket_id_delim(socket);
+    }
+    LISTBASE_FOREACH (bNodeSocket *, socket, &node->outputs) {
+      version_node_socket_id_delim(socket);
+    }
+  }
+}
+
 /* NOLINTNEXTLINE: readability-function-size */
 void blo_do_versions_300(FileData *fd, Library *UNUSED(lib), Main *bmain)
 {
@@ -2160,5 +2171,12 @@ void blo_do_versions_300(FileData *fd, Library *UNUSED(lib), Main *bmain)
    */
   {
     /* Keep this block, even when empty. */
+
+    FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
+      if (ntree->type != NTREE_CUSTOM) {
+        version_node_tree_socket_id_delim(ntree);
+      }
+    }
+    FOREACH_NODETREE_END;
   }
 }
diff --git a/source/blender/blenloader/intern/versioning_common.cc b/source/blender/blenloader/intern/versioning_common.cc
index 61119b80fdd..af765be619f 100644
--- a/source/blender/blenloader/intern/versioning_common.cc
+++ b/source/blender/blenloader/intern/versioning_common.cc
@@ -27,6 +27,7 @@
 
 #include "BLI_listbase.h"
 #include "BLI_string.h"
+#include "BLI_string_ref.hh"
 
 #include "BKE_animsys.h"
 #include "BKE_lib_id.h"
@@ -37,6 +38,8 @@
 
 #include "versioning_common.h"
 
+using blender::StringRef;
+
 ARegion *do_versions_add_region_if_not_found(ListBase *regionbase,
                                              int region_type,
                                              const char *name,
@@ -101,6 +104,30 @@ static void change_node_socket_name(ListBase *sockets, const char *old_name, con
   }
 }
 
+/**
+ * Convert `SocketName.001` unique name format to `SocketName_001`. Previously both were used.
+ */
+void version_node_socket_id_delim(bNodeSocket *socket)
+{
+  StringRef name = socket->name;
+  StringRef id = socket->identifier;
+
+  if (!id.startswith(name)) {
+    /* We only need to affect the case where the identifier starts with the name. */
+    return;
+  }
+
+  StringRef id_number = id.drop_known_prefix(name);
+  if (id_number.is_empty()) {
+    /* The name was already unique, and didn't need numbers at the end for the id. */
+    return;
+  }
+
+  if (id_number.startswith(".")) {
+    socket->identifier[name.size()] = '_';
+  }
+}
+
 void version_node_socket_name(bNodeTree *ntree,
                               const int node_type,
                               const char *old_name,
diff --git a/source/blender/blenloader/intern/versioning_common.h b/source/blender/blenloader/intern/versioning_common.h
index ed1cafdca33..7f179800ddd 100644
--- a/source/blender/blenloader/intern/versioning_common.h
+++ b/source/blender/blenloader/intern/versioning_common.h
@@ -62,6 +62,8 @@ void version_node_socket_index_animdata(
 
 void version_node_id(struct bNodeTree *ntree, const int node_type, const char *new_name);
 
+void version_node_socket_id_delim(bNodeSocket *socket);
+
 #ifdef __cplusplus
 }
 #endif



More information about the Bf-blender-cvs mailing list