Commit: a1ae04d15a9faaa9b4dc59e8a5bec02a7c69b255
Author: Campbell Barton
Date:   Wed Jan 30 09:36:38 2019 +1100
Branches: master
https://developer.blender.org/rBa1ae04d15a9faaa9b4dc59e8a5bec02a7c69b255

Versioning: add renaming utility function

Avoids accidents creating duplicate names.

Also ensure screens are sorted on rename.

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

M       source/blender/blenloader/intern/versioning_defaults.c

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

diff --git a/source/blender/blenloader/intern/versioning_defaults.c 
b/source/blender/blenloader/intern/versioning_defaults.c
index c5df6a6fb83..8217483cf35 100644
--- a/source/blender/blenloader/intern/versioning_defaults.c
+++ b/source/blender/blenloader/intern/versioning_defaults.c
@@ -114,6 +114,33 @@ void BLO_update_defaults_userpref_blend(void)
        BKE_keyconfig_pref_set_select_mouse(&U, 0, true);
 }
 
+
+/**
+ * Rename if the ID doesn't exist.
+ */
+static ID *rename_id_for_versioning(Main *bmain, const short id_type, const 
char *name_src, const char *name_dst)
+{
+       /* We can ignore libraries */
+       ListBase *lb = which_libbase(bmain, id_type);
+       ID *id = NULL;
+       for (ID *idtest = lb->first; idtest; idtest = idtest->next) {
+               if (idtest->lib == NULL) {
+                       if (STREQ(idtest->name + 2, name_src)) {
+                               id = idtest;
+                       }
+                       if (STREQ(idtest->name + 2, name_dst)) {
+                               return NULL;
+                       }
+               }
+       }
+       if (id != NULL) {
+               BLI_strncpy(id->name + 2, name_dst, sizeof(id->name) - 2);
+               /* We know it's unique, this just sorts. */
+               BLI_libblock_ensure_unique_name(bmain, id->name);
+       }
+       return id;
+}
+
 /**
  * Update defaults in startup.blend, without having to save and embed the file.
  * This function can be emptied each time the startup.blend is updated. */
@@ -176,6 +203,7 @@ void BLO_update_defaults_startup_blend(Main *bmain, const 
char *app_template)
                                WorkSpaceLayout *layout = 
BKE_workspace_hook_layout_for_workspace_get(win->workspace_hook, workspace);
                                bScreen *screen = layout->screen;
                                BLI_strncpy(screen->id.name + 2, 
workspace->id.name + 2, sizeof(screen->id.name) - 2);
+                               BLI_libblock_ensure_unique_name(bmain, 
screen->id.name);
                        }
                }
 
@@ -300,16 +328,8 @@ void BLO_update_defaults_startup_blend(Main *bmain, const 
char *app_template)
                }
 
                /* Rename lamp objects. */
-               for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
-                       if (STREQ(ob->id.name, "OBLamp")) {
-                               STRNCPY(ob->id.name, "OBLight");
-                       }
-               }
-               for (Lamp *lamp = bmain->lamp.first; lamp; lamp = 
lamp->id.next) {
-                       if (STREQ(lamp->id.name, "LALamp")) {
-                               STRNCPY(lamp->id.name, "LALight");
-                       }
-               }
+               rename_id_for_versioning(bmain, ID_OB, "Lamp", "Light");
+               rename_id_for_versioning(bmain, ID_LA, "Lamp", "Light");
 
                for (Mesh *mesh = bmain->mesh.first; mesh; mesh = 
mesh->id.next) {
                        /* Match default for new meshes. */

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to