From 6b131222ad251c4025e0506c3b8c96d1f8c5ef6c Mon Sep 17 00:00:00 2001
From: Robert Meerman <meermanr>
Date: Mon, 7 Feb 2022 10:46:06 +1300
Subject: [PATCH] Corrected "Change Bolt" removes linked duplicates

Patch D10034

Prior to this patch, attempting to modify an existing bolt would
*replace* the object's mesh data block with a new one and remove the
original. This removal would essentially delete all linked duplicates.

Now the original mesh data block is modified in place, which avoids
having to copy anything between new and old object data blocks
(materials, names, etc), and causes all linked duplicates to reflect
the changes as users would expect.
---
 add_mesh_BoltFactory/Boltfactory.py | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/add_mesh_BoltFactory/Boltfactory.py b/add_mesh_BoltFactory/Boltfactory.py
index f26467044..58a7ae011 100644
--- a/add_mesh_BoltFactory/Boltfactory.py
+++ b/add_mesh_BoltFactory/Boltfactory.py
@@ -18,6 +18,7 @@
 
 
 import bpy
+import bmesh
 from mathutils import Matrix
 from bpy.types import Operator
 from bpy_extras.object_utils import AddObjectHelper
@@ -431,20 +432,21 @@ class add_mesh_bolt(Operator, AddObjectHelper):
                 (context.active_object.data is not None) and ('Bolt' in context.active_object.data.keys()) and \
                 (self.change == True):
                 obj = context.active_object
-                oldmesh = obj.data
-                oldmeshname = obj.data.name
                 mesh = createMesh.Create_New_Mesh(self, context)
-                obj.data = mesh
+
+                # Modify existing mesh data object by replacing geometry (but leaving materials etc)
+                bm = bmesh.new()
+                bm.from_mesh(mesh)
+                bm.to_mesh(obj.data)
+                bm.free()
+
+                bpy.data.meshes.remove(mesh)
+
                 try:
                     bpy.ops.object.vertex_group_remove(all=True)
                 except:
                     pass
 
-                for material in oldmesh.materials:
-                    obj.data.materials.append(material)
-
-                bpy.data.meshes.remove(oldmesh)
-                obj.data.name = oldmeshname
             else:
                 mesh = createMesh.Create_New_Mesh(self, context)
                 obj = object_utils.object_data_add(context, mesh, operator=self)
-- 
GitLab