@@ -132,7 +132,7 @@ def merge_shape_keys(obj, shape_key_names=["*"], target_shape_key_name=""):
132132 basis_shape_key_name = mesh .shape_keys .key_blocks [0 ].name
133133 target_shape_key_name = target_shape_key_name or basis_shape_key_name
134134 if target_shape_key_name not in mesh .shape_keys .key_blocks :
135- log (f"Can't merge shape keys, target shape key { target_shape_key_name } doesn't exist" )
135+ logd (f"Can't merge shape keys, target shape key { target_shape_key_name } doesn't exist" )
136136 return
137137
138138 # Store state
@@ -141,20 +141,30 @@ def merge_shape_keys(obj, shape_key_names=["*"], target_shape_key_name=""):
141141 # Mute all but the ones to be merged
142142 for sk in mesh .shape_keys .key_blocks [1 :]:
143143 if any (fnmatch (sk .name , s ) for s in shape_key_names ) or sk .name == target_shape_key_name :
144- if sk .mute :
145- # Delete candidate shapekeys that won't be used
146- # This ensures muted shapekeys don't unexpectedly return when objects are merged
144+ # Remove any drivers related to shape keys that will be deleted
145+ if mesh .shape_keys .animation_data :
146+ sk_data_path = f'key_blocks["{ sk .name } "]'
147+ for fc in mesh .shape_keys .animation_data .drivers :
148+ if fc .data_path .startswith (sk_data_path ):
149+ if fc .data_path .endswith ('.value' ):
150+ # Influence was being driven, assume user would want to merge it fully
151+ sk .value = sk .slider_max
152+ mesh .shape_keys .animation_data .drivers .remove (fc )
153+ if sk .mute or sk .value == 0.0 :
154+ # Muted candidates are handled as if merged at 0% and deleted
155+ # Removing ensures shape keys don't unexpectedly return when objects are merged
147156 obj .shape_key_remove (sk )
148157 else :
149158 sk .mute = True
150159
151- unmuted_shape_keys = [sk for sk in mesh .shape_keys .key_blocks [1 :] if not sk .mute ]
152- if unmuted_shape_keys :
153- log (f"Merging { len (unmuted_shape_keys )} shape keys to { target_shape_key_name } : " +
154- ", " .join (fmt_shape_key (sk ) for sk in unmuted_shape_keys ))
160+ source_shape_keys = [sk for sk in mesh .shape_keys .key_blocks [1 :]
161+ if not sk .mute and sk .name != target_shape_key_name ]
162+ if source_shape_keys :
163+ log (f"Merging { len (source_shape_keys )} shape keys to { target_shape_key_name } : " +
164+ ", " .join (fmt_shape_key (sk ) for sk in source_shape_keys ))
155165
156166 # Replace target shape key with mix. While the basis layer *does* exist in bmesh
157- # changing it doesn't seem to have any effect, hence the two code paths
167+ # changing it doesn't seem to have any effect, hence the split code path
158168 merged_sk = obj .shape_key_add (name = "__merged" , from_mix = True )
159169 bm = bmesh .new ()
160170 bm .from_mesh (mesh )
@@ -171,9 +181,8 @@ def merge_shape_keys(obj, shape_key_names=["*"], target_shape_key_name=""):
171181 obj .shape_key_remove (merged_sk )
172182
173183 # Remove the merged shapekeys
174- for sk in unmuted_shape_keys :
175- if sk .name != target_shape_key_name :
176- obj .shape_key_remove (sk )
184+ for sk in source_shape_keys :
185+ obj .shape_key_remove (sk )
177186
178187 # Restore state
179188 for sk in saved_unmuted_shape_keys :
0 commit comments