Clean GP material stack

1.1.3

- added: clean material stack in auto-build
This commit is contained in:
pullusb
2023-01-11 17:36:43 +01:00
parent fd91900328
commit b1cc4fa5d7
4 changed files with 80 additions and 6 deletions
+61 -1
View File
@@ -1576,4 +1576,64 @@ def set_layer_colors(skip_if_colored=False):
print(l.info, '->', color)
l.channel_color = color
bpy.context.preferences.edit.use_anim_channel_group_colors = True
bpy.context.preferences.edit.use_anim_channel_group_colors = True
def different_gp_mat(mata, matb):
'''return None if no difference (False), string describing color difference (True)'''
a = mata.grease_pencil
b = matb.grease_pencil
if a.color[:] != b.color[:]:
return f'{mata.name} and {matb.name} stroke color is different'
if a.fill_color[:] != b.fill_color[:]:
return f'{mata.name} and {matb.name} fill_color color is different'
if a.show_stroke != b.show_stroke:
return f'{mata.name} and {matb.name} stroke has different state'
if a.show_fill != b.show_fill:
return f'{mata.name} and {matb.name} fill has different state'
## Clean dups
def clean_mats_duplication(ob, skip_different_materials=True):
'''Clean object material stack of duplication
if a material is named "mat.001" and a "mat" exists, replace with the one with original name
:skip_different_materials: Don't replace a "mat.???" if orignal "mat" has different color
'''
import re
diff_ct = 0
todel = []
if ob.type != 'GPENCIL':
return
if not hasattr(ob, 'material_slots'):
return
for i, ms in enumerate(ob.material_slots):
mat = ms.material
if not mat:
continue
match = re.search(r'(.*)\.\d{3}$', mat.name)
if not match:
continue
basemat = bpy.data.materials.get(match.group(1))
if not basemat:
continue
diff = different_gp_mat(mat, basemat)
if diff:
print(f'! {ob.name} : {diff}')
diff_ct += 1
if skip_different_materials:
continue
if mat not in todel:
todel.append(mat)
ms.material = basemat
print(f'{ob.name} : slot {i} >> replaced {mat.name}')
mat.use_fake_user = False
### delete (only when using on all objects loop, else can delete another objects mat...)
## for m in reversed(todel):
## bpy.data.materials.remove(m)
if diff_ct:
print(f'{diff_ct} mat skipped >> same name but different color settings!')
# return ('INFO', f'{diff_ct} mat skipped >> same name but different color settings!')