Skip to content

Commit a014b92

Browse files
committed
[WIP] Replicate modifiers
1 parent d6d9dac commit a014b92

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

versioning.py

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@ def recalc_pointers(scene):
3737
logger.debug("Update entity indices:" + msg)
3838

3939

40+
def copy_modifiers(source_obj, target_obj):
41+
"""Copy modifiers from source object to target object"""
42+
if not source_obj or not target_obj:
43+
return
44+
45+
# Clear existing modifiers on target
46+
while target_obj.modifiers:
47+
target_obj.modifiers.remove(target_obj.modifiers[0])
48+
49+
# Copy modifiers from source to target
50+
for mod in source_obj.modifiers:
51+
new_mod = target_obj.modifiers.new(name=mod.name, type=mod.type)
52+
# Copy attributes that are common to all modifier types
53+
for attr in dir(mod):
54+
if attr.startswith('__') or attr in {'rna_type', 'type', 'name', 'bl_rna'}:
55+
continue
56+
try:
57+
if hasattr(new_mod, attr):
58+
setattr(new_mod, attr, getattr(mod, attr))
59+
except (AttributeError, TypeError):
60+
# Skip attributes that can't be copied
61+
pass
62+
63+
4064
def do_versioning(self):
4165

4266
logger.debug("Check versioning")
@@ -128,27 +152,69 @@ def do_versioning(self):
128152
if version < (0, 28, 0):
129153
# Handle old 'MESH' and 'BEZIER' convertion types
130154
msg += "\n Update sketch conversion type to 'CURVE' for sketches:"
155+
156+
# Dictionary to temporarily store objects and their modifiers
157+
old_objects = {}
158+
131159
for sketch in context.scene.sketcher.entities.sketches:
132160
if sketch.convert_type == 'NONE':
133161
continue
134162

135-
# Delete previously converted objects
163+
# Store references to the old objects before deleting them
164+
sketch_id = str(sketch.slvs_index)
165+
old_objects[sketch_id] = {
166+
'mesh_obj': sketch.target_object,
167+
'curve_obj': sketch.target_curve_object
168+
}
169+
170+
# Clear links to objects but don't delete them yet
136171
if sketch.target_object:
137172
sketch.target_object.sketch_index = -1
138-
bpy.data.objects.remove(sketch.target_object, do_unlink=True)
139173
sketch.target_object = None
174+
140175
if sketch.target_curve_object:
141176
sketch.target_curve_object.sketch_index = -1
142-
bpy.data.objects.remove(sketch.target_curve_object, do_unlink=True)
143177
sketch.target_curve_object = None
144178

145179
# Change the conversion type
146180
sketch.convert_type = 'CURVE'
147181

148182
msg += " {}".format(str(sketch))
149183

150-
# Trigger the convertion with the new convert type
184+
# Trigger the conversion with the new convert type
151185
bpy.ops.view3d.slvs_update(solve=False)
152-
186+
187+
# Now copy modifiers from old objects to new objects
188+
for sketch in context.scene.sketcher.entities.sketches:
189+
sketch_id = str(sketch.slvs_index)
190+
if sketch_id not in old_objects:
191+
continue
192+
193+
if sketch.target_curve_object:
194+
# Try to copy from mesh object first, then curve object
195+
if old_objects[sketch_id]['mesh_obj']:
196+
copy_modifiers(old_objects[sketch_id]['mesh_obj'], sketch.target_curve_object)
197+
elif old_objects[sketch_id]['curve_obj']:
198+
copy_modifiers(old_objects[sketch_id]['curve_obj'], sketch.target_curve_object)
199+
200+
# Unlink and rename old objects instead of deleting them
201+
for sketch_id, objects in old_objects.items():
202+
# Process mesh object
203+
if objects['mesh_obj']:
204+
old_obj = objects['mesh_obj']
205+
# Unlink from all collections
206+
for collection in old_obj.users_collection:
207+
collection.objects.unlink(old_obj)
208+
# Rename to indicate it's an old version
209+
old_obj.name = f"OLD_{old_obj.name}"
210+
211+
# Process curve object
212+
if objects['curve_obj']:
213+
old_obj = objects['curve_obj']
214+
# Unlink from all collections
215+
for collection in old_obj.users_collection:
216+
collection.objects.unlink(old_obj)
217+
# Rename to indicate it's an old version
218+
old_obj.name = f"OLD_{old_obj.name}"
153219

154220
logger.warning(msg)

0 commit comments

Comments
 (0)