-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathoperators.py
1491 lines (1198 loc) · 55.1 KB
/
operators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import bpy
from bpy.types import Operator
from bpy.props import (
StringProperty,
BoolProperty,
EnumProperty,
FloatProperty,
CollectionProperty
)
from .enum_values import *
from .functions import *
from .preferences import *
from types import SimpleNamespace
from math import radians
# -----------------------------------------------------------------------------
# operator classes
class VIEW3D_OT_materialutilities_assign_material_edit(bpy.types.Operator):
"""Assign a material to the current selection"""
bl_idname = 'view3d.materialutilities_assign_material_edit'
bl_label = "Assign Material (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
material_name: StringProperty(
name = "Material Name",
description = "Name of Material to assign to current selection",
default = "",
maxlen = 63
)
new_material: BoolProperty(
name = "",
description = "Add a new material, enter the name in the box",
default = False
)
show_dialog: BoolProperty(
name = "Show Dialog",
default = False
)
@classmethod
def poll(cls, context):
return context.active_object is not None
def invoke(self, context, event):
if event.shift or event.ctrl:
return bpy.ops.view3d.materialutilities_assign_pbr_material('INVOKE_DEFAULT')
if self.show_dialog:
return context.window_manager.invoke_props_dialog(self)
else:
return self.execute(context)
def draw(self, context):
mu_prefs = materialutilities_get_preferences(context)
factor = 0.85 if mu_prefs.add_pbr_import_to_assign_dlg else 0.9
layout = self.layout
col = layout.column()
row = col.split(factor=factor, align=True)
if self.new_material:
row.prop(self, 'material_name')
else:
row.prop_search(self, 'material_name', bpy.data, 'materials')
row.prop(self, 'new_material', expand = True, icon = 'ADD')
if mu_prefs.add_pbr_import_to_assign_dlg:
row.operator(VIEW3D_OT_materialutilities_assign_pbr_material.bl_idname,
text="", icon='FILE_IMAGE')
def execute(self, context):
material_name = self.material_name
if self.new_material:
material_name = mu_new_material_name(material_name)
elif material_name == "":
self.report({'WARNING'}, "No Material Name given!")
return {'CANCELLED'}
return mu_assign_material(self, material_name, 'APPEND_MATERIAL')
class VIEW3D_OT_materialutilities_assign_material_object(bpy.types.Operator):
"""Assign a material to the current selection
(See the operator panel [F9] for more options)"""
bl_idname = 'view3d.materialutilities_assign_material_object'
bl_label = "Assign Material (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
material_name: StringProperty(
name = "Material Name",
description = "Name of Material to assign to current selection",
default = "",
maxlen = 63
)
override_type: EnumProperty(
name = "Assignment method",
description = "",
items = mu_override_type_enums
)
new_material: BoolProperty(
name = "",
description = "Add a new material\nEnter a name for the new material in the box",
default = False
)
show_dialog: BoolProperty(
name = "Show Dialog",
default = False
)
@classmethod
def poll(cls, context):
return len(context.selected_editable_objects) > 0
def invoke(self, context, event):
if event.shift or event.ctrl:
return bpy.ops.view3d.materialutilities_assign_pbr_material('INVOKE_DEFAULT')
if self.show_dialog:
return context.window_manager.invoke_props_dialog(self)
else:
return self.execute(context)
def draw(self, context):
mu_prefs = materialutilities_get_preferences(context)
factor = 0.85 if mu_prefs.add_pbr_import_to_assign_dlg else 0.9
layout = self.layout
col = layout.column()
row = col.split(factor=factor, align = True)
if self.new_material:
row.prop(self, 'material_name')
else:
row.prop_search(self, 'material_name', bpy.data, 'materials')
row.prop(self, "new_material", expand = True, icon = 'ADD')
if mu_prefs.add_pbr_import_to_assign_dlg:
row.operator(VIEW3D_OT_materialutilities_assign_pbr_material.bl_idname,
text="", icon='FILE_IMAGE')
layout.prop(self, "override_type")
def execute(self, context):
material_name = self.material_name
override_type = self.override_type
if self.new_material:
material_name = mu_new_material_name(material_name)
elif material_name == "":
self.report({'WARNING'}, "No Material Name given!")
return {'CANCELLED'}
result = mu_assign_material(self, material_name, override_type)
return result
class VIEW3D_OT_materialutilities_select_by_material_name(bpy.types.Operator):
"""Select geometry that has the chosen material assigned to it
(See the operator panel [F9] for more options)"""
bl_idname = 'view3d.materialutilities_select_by_material_name'
bl_label = "Select By Material Name (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
extend_selection: BoolProperty(
name = "Extend Selection",
description = "Keeps the current selection and adds faces "
"with the material to the selection"
)
material_name: StringProperty(
name = "Material Name",
description = "Name of Material to find and Select",
maxlen = 63
)
show_dialog: BoolProperty(
name = "Show Dialog",
default = False
)
@classmethod
def poll(cls, context):
return len(context.visible_objects) > 0
def invoke(self, context, event):
if self.show_dialog:
return context.window_manager.invoke_props_dialog(self)
else:
return self.execute(context)
def draw(self, context):
layout = self.layout
layout.prop_search(self, 'material_name', bpy.data, 'materials')
layout.prop(self, 'extend_selection', icon = 'SELECT_EXTEND')
def execute(self, context):
material_name = self.material_name
ext = self.extend_selection
return mu_select_by_material_name(self, material_name, ext)
class VIEW3D_OT_materialutilities_copy_material_to_others(bpy.types.Operator):
"""Copy the material(s) of the active object to the other selected objects"""
bl_idname = 'view3d.materialutilities_copy_material_to_others'
bl_label = "Copy material(s) to others (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if context.active_object is None:
return False
# Only enable in Edit mode if Face selection is enabled
if context.active_object.mode == 'EDIT':
return bpy.context.scene.tool_settings.mesh_select_mode[2]
else:
return True
def execute(self, context):
return mu_copy_material_to_others(self)
class VIEW3D_OT_materialutilities_clean_material_slots(bpy.types.Operator):
"""Removes any material slots from the selected objects that are not used"""
bl_idname = 'view3d.materialutilities_clean_material_slots'
bl_label = "Clean Material Slots (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
only_active: BoolProperty(
name = "Only active object",
description = "Only remove the material slots for the active object "
"(otherwise do it for every selected object)",
default = False
)
affect: EnumProperty(
name = "Affect",
description = "Which object(s) to clean material slots on",
items = mu_affect_enums,
default = 'SELECTED'
)
selected_collection: StringProperty(
name = "Collection",
description = "Which collection with objects to clean material slots on"
)
show_dialog: BoolProperty(
name = "Show Dialog",
default = False
)
@classmethod
def poll(cls, context):
mu_prfs = context.preferences.addons[__package__].preferences
if mu_prfs.use_legacy_cleanmatslots_ui:
return len(context.selected_editable_objects) > 0
else:
return True
def invoke(self, context, event):
mu_prfs = context.preferences.addons[__package__].preferences
self.affect = mu_prfs.cleanmatslots_affect
self.only_active = mu_prfs.cleanmatslots_only_active
if event.shift or event.ctrl:
return self.execute(context)
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
mu_prfs = context.preferences.addons[__package__].preferences
layout = self.layout
if mu_prfs.use_legacy_cleanmatslots_ui:
layout.prop(self, 'only_active', icon = 'PIVOT_ACTIVE')
else:
layout.prop(self, 'affect')
if self.affect == 'SELECTED_COLLECTION':
layout.prop_search(self, 'selected_collection', bpy.data, 'collections')
def execute(self, context):
mu_prfs = context.preferences.addons[__package__].preferences
if mu_prfs.use_legacy_cleanmatslots_ui:
affect = 'ACTIVE' if self.only_active else 'SELECTED'
else:
affect = self.affect
return mu_cleanmatslots(self, affect, self.selected_collection)
class VIEW3D_OT_materialutilities_remove_material_slot(bpy.types.Operator):
"""Remove the active material slot from selected object(s)
(See the operator panel [F9] for more options)"""
bl_idname = 'view3d.materialutilities_remove_material_slot'
bl_label = "Remove Active Material Slot (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
only_active: BoolProperty(
name = "Only active object",
description = "Only remove the active material slot for the active object "
"(otherwise do it for every selected object)",
default = True
)
@classmethod
def poll(cls, context):
return (context.active_object is not None) and (context.active_object.mode != 'EDIT')
def draw(self, context):
layout = self.layout
layout.prop(self, 'only_active', icon = 'PIVOT_ACTIVE')
def execute(self, context):
return mu_remove_material(self, self.only_active)
class VIEW3D_OT_materialutilities_remove_all_material_slots(bpy.types.Operator):
"""Remove all material slots from selected object(s)
(See the operator panel [F9] for more options)"""
bl_idname = 'view3d.materialutilities_remove_all_material_slots'
bl_label = "Remove All Material Slots (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
only_active: BoolProperty(
name = "Only active object",
description = "Only remove the material slots for the active object "
"(otherwise do it for every selected object)",
default = True
)
@classmethod
def poll(cls, context):
return (context.active_object is not None) and (context.active_object.mode != 'EDIT')
def draw(self, context):
layout = self.layout
layout.prop(self, 'only_active', icon = 'PIVOT_ACTIVE')
def execute(self, context):
return mu_remove_all_materials(self, self.only_active)
class VIEW3D_OT_materialutilities_replace_material(bpy.types.Operator):
"""Replace a material by name"""
bl_idname = 'view3d.materialutilities_replace_material'
bl_label = "Replace Material (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
mat_org: StringProperty(
name = "Original",
description = "Material to find and replace",
maxlen = 63,
)
mat_rep: StringProperty(
name = "Replacement",
description = "Material that will be used instead of the Original material",
maxlen = 63,
)
all_objects: BoolProperty(
name = "All Objects",
description = "Replace for all objects in this blend file "
"(otherwise only selected objects)",
default = True,
)
update_selection: BoolProperty(
name = "Update Selection",
description = "Select affected objects and deselect unaffected",
default = True,
)
def draw(self, context):
layout = self.layout
layout.prop_search(self, 'mat_org', bpy.data, 'materials')
layout.prop_search(self, 'mat_rep', bpy.data, 'materials')
layout.separator()
layout.prop(self, 'all_objects', icon = 'BLANK1')
layout.prop(self, 'update_selection', icon = 'SELECT_INTERSECT')
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
return mu_replace_material(self, self.mat_org, self.mat_rep,
self.all_objects, self.update_selection)
class VIEW3D_OT_materialutilities_replace_multiple_materials(bpy.types.Operator):
"""Replace a list of material by names"""
bl_idname = 'view3d.materialutilities_replace_multiple_materials'
bl_label = "Replace Multiple Material (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
mats_org: StringProperty(
name = "Original",
description = "Materials to find and replace",
maxlen = 63
)
mats_rep: StringProperty(
name = "Replacement",
description = "Materials that will be used instead of the Original material",
maxlen = 63
)
all_objects: BoolProperty(
name = "All Objects",
description = "Replace for all objects in this blend file "
"(otherwise only selected objects)",
default = True,
)
update_selection: BoolProperty(
name = "Update Selection",
description = "Select affected objects and deselect unaffected",
default = True,
)
def draw(self, context):
layout = self.layout
layout.prop_search(self, 'mats_org', bpy.data, 'texts')
layout.prop_search(self, 'mats_rep', bpy.data, 'texts')
layout.separator()
layout.prop(self, 'all_objects', icon = 'BLANK1')
layout.prop(self, 'update_selection', icon = 'SELECT_INTERSECT')
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
return mu_replace_multiple_materials(self, self.mats_org, self.mats_rep,
self.all_objects, self.update_selection)
class VIEW3D_OT_materialutilities_fake_user_set(bpy.types.Operator):
"""Enable/disable fake user for materials"""
bl_idname = 'view3d.materialutilities_fake_user_set'
bl_label = "Set Fake User (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
fake_user: EnumProperty(
name = "Fake User",
description = "Turn fake user on or off",
items = mu_fake_user_set_enums,
default = 'TOGGLE'
)
affect: EnumProperty(
name = "Affect",
description = "Which object's materials to affect",
items = mu_fake_user_affect_enums,
default = 'UNUSED'
)
selected_collection: StringProperty(
name = "Collection",
description = "Affect materials of the objects in this selected collection",
default = ""
)
@classmethod
def poll(cls, context):
return (context.active_object is not None)
def draw(self, context):
layout = self.layout
layout.prop(self, 'fake_user', expand = True)
layout.separator()
layout.prop(self, 'affect')
if self.affect == 'SELECTED_COLLECTION':
layout.prop_search(self, 'selected_collection', bpy.data, 'collections')
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
return mu_set_fake_user(self, self.fake_user, self.affect, self.selected_collection)
class VIEW3D_OT_materialutilities_change_material_link(bpy.types.Operator):
"""Link the materials to Data or Object, while keeping materials assigned"""
bl_idname = 'view3d.materialutilities_change_material_link'
bl_label = "Change Material Linking (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
override: BoolProperty(
name = "Override Data material",
description = "Override the materials assigned to the object data/mesh "
"when switching to 'Linked to Data'\n"
"(WARNING: This will override the materials of other linked objects, "
"which have the materials linked to Data)",
default = False,
)
unlink_old: BoolProperty(
name = "Unlink Material From Old Link",
description = "Unlink the material from what it is currently linked to, "
"before it gets linked to the new option",
default = False,
)
link_to: EnumProperty(
name = "Link",
description = "What should the material be linked to",
items = mu_link_to_enums,
default = 'OBJECT'
)
affect: EnumProperty(
name = "Affect",
description = "Which materials of objects to affect",
items = mu_link_affect_enums,
default = 'SELECTED'
)
selected_collection: StringProperty(
name = "Collection",
description = "Affect materials of the objects in this selected collection",
default = ""
)
@classmethod
def poll(cls, context):
return (context.active_object is not None)
def draw(self, context):
layout = self.layout
layout.prop(self, 'link_to', expand = True)
layout.separator()
layout.prop(self, 'affect')
if self.affect == 'SELECTED_COLLECTION':
layout.prop_search(self, 'selected_collection', bpy.data, 'collections')
layout.separator()
layout.prop(self, 'override', icon = 'DECORATE_OVERRIDE')
layout.prop(self, 'unlink_old', icon = 'UNLINKED')
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
return mu_change_material_link(self, self.link_to, self.affect, self.override,
self.selected_collection, self.unlink_old)
class MATERIAL_OT_materialutilities_merge_base_names(bpy.types.Operator):
"""Merges materials that has the same base names but ends with .xxx (.001, .002 etc)"""
bl_idname = 'material.materialutilities_merge_base_names'
bl_label = "Merge Base Names"
bl_description = ("Merge materials that has the same base names "
"but ends with .xxx (.001, .002 etc)")
material_base_name: StringProperty(
name = "Material Base Name",
default = "",
description = "Base name for materials to merge "
"(e.g. \"Material\" is the base name "
"of \"Material.001\", \"Material.002\" etc.)"
)
use_new_name: BoolProperty(
name = "New name",
description = "Give the material a new name instead of "
"the original base name"
)
use_selected_material: BoolProperty(
name = "Use Selected Material",
default = False,
description = "Use the selected material (e.g. \"Material.004\") instead of the \"base material\" "
"(e.g. \"Material\") as basis for the merged material",
)
material_new_name: StringProperty(
name = "New Material Name",
default = "",
description = "Set a new name to use instead of "
"the original Material Base Name"
)
is_auto: BoolProperty(
name = "Auto Merge",
description = "Find all available duplicate materials "
"and Merge them"
)
pattern_to_use: EnumProperty(
name = "Pattern",
description = "Use another pattern than Material.xxx"
"for merging materials.""",
items = mu_merge_base_names_pattern_enums,
default = 'DEFAULT',
)
user_defined_pattern_simple: StringProperty(
name = "Delimiter",
description = "Delimiter to use instead of . (dot)",
default = "."
)
user_defined_pattern_regex: StringProperty(
name = "RegEx pattern",
description = "The pattern to use for search and replace. "
"Use (Python Style) RegEx. "
"Make sure you have two groups "
"(one for the name and one for the suffix)",
default = "^%BASE_NAME\.(\d{1,3})$"
)
is_not_undo = False
remove_unused = False
material_error = [] # collect materials for warning messages
merged_materials = []
def reg_material(self, material):
if not material.name in self.merged_materials:
self.merged_materials.append(material.name)
def replace_name(self, name = ""):
"""If the user chooses a material like 'Material.042',
clean it up to get a base name ('Material')"""
if name != "":
self.material_base_name = name
# use the chosen material as a base one, check if there is a name
self.check_no_name = False if self.material_base_name in {''} else True
delimiter = '.'
if self.pattern_to_use == 'REGEX' and self.check_no_name is False:
base, suffix = self.split_name_regex(self.material_base_name)
self.material_base_name = base
return
if self.pattern_to_use == 'SIMPLE':
delimiter = self.user_defined_pattern_simple
# No need to do this if it's already "clean"
# (Also lessens the potential of error given about the material with the Base name)
if delimiter not in self.material_base_name:
return
if self.check_no_name is True:
for mat in bpy.data.materials:
name = mat.name
if name == self.material_base_name:
try:
if self.pattern_to_use == 'REGEX':
base, suffix = self.split_name_regex(name)
else:
base, suffix = name.rsplit(delimiter, 1)
# trigger the exception
num = int(suffix, 10)
self.material_base_name = base
mat.name = self.material_base_name
return
except ValueError:
if name not in self.material_error:
self.material_error.append(name)
return
return
def split_name_regex(self, name):
"""Split the material name using Regular Expressions"""
import re
pattern = self.user_defined_pattern_regex
pattern = pattern.replace("%BASE_NAME", "(.*)")
matches = re.search(pattern, name)
if not matches:
print("Not matching regex:" + name)
return name, None
base = matches.group(1)
suffix = matches.group(2)
return base, suffix
def split_name(self, name, ignore_base_name=False):
"""Split the material name into a base and a suffix"""
delimiter = '.'
if self.pattern_to_use == 'REGEX':
return self.split_name_regex(name)
if self.pattern_to_use == 'SIMPLE':
delimiter = self.user_defined_pattern_simple
# No need to do this if it's already "clean"/there is no suffix
if delimiter not in name:
return name, None
base, suffix = name.rsplit(delimiter, 1)
try:
# trigger the exception
num = int(suffix, 10)
except ValueError:
# Not a numeric suffix
# Don't report on materials not actually included in the merge!
if self.is_auto or base == self.material_base_name \
and name not in self.material_error:
self.material_error.append(name)
return name, None
if self.is_auto is False and not ignore_base_name:
if base == self.material_base_name:
return base, suffix
else:
return name, None
return base, suffix
def fixup_slot(self, slot):
"""Fix material slots that was assigned to materials now removed"""
if not slot.material:
return
base, suffix = self.split_name(slot.material.name)
if suffix is None:
return
self.reg_material(slot.material)
try:
base_mat = bpy.data.materials[base]
except KeyError:
self.replace_name(slot.material.name)
try:
base_mat = bpy.data.materials[base]
except KeyError:
print("\n[Materials Utilities Specials]\nLink to base names\n"
"Error: Base material %r not found\n" % base)
return
slot.material = base_mat
def main_loop(self, context):
"""Loops through all objects and material slots to make sure
they are assigned to the right material"""
for obj in context.scene.objects:
for slot in obj.material_slots:
self.fixup_slot(slot)
@classmethod
def poll(self, context):
return (context.mode == 'OBJECT') and (len(context.visible_objects) > 0)
def draw(self, context):
layout = self.layout
box_1 = layout.box()
col = box_1.column()
row = col.split(factor = 0.875, align = True)
row.prop_search(self, 'material_base_name', bpy.data, 'materials')
row.prop(self, 'use_new_name', text = "", icon = 'SYNTAX_OFF')
row.prop(self, 'use_selected_material', text = "", icon = 'STYLUS_PRESSURE')
if self.use_new_name:
box_1.prop(self, 'material_new_name')
box_1.enabled = not self.is_auto
layout.separator()
box_2 = layout.box()
box_2.prop(self, 'pattern_to_use')
if self.pattern_to_use == 'SIMPLE':
box_2.prop(self, 'user_defined_pattern_simple')
elif self.pattern_to_use == 'REGEX':
box_2.prop(self, 'user_defined_pattern_regex')
layout.prop(self, 'is_auto', text = "Auto Rename/Replace", icon = 'SYNTAX_ON')
def invoke(self, context, event):
mu_prefs = materialutilities_get_preferences(context)
self.use_selected_material = mu_prefs.merge_use_selected_material
self.remove_unused = mu_prefs.merge_remove_unused
self.is_not_undo = True
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
# Reset Material errors, otherwise we risk reporting errors erroneously.
self.material_error = []
self.merged_materials = []
if not self.is_auto:
# Use the selected material (Could be Material.005, etc) instead of
# the material with the base name (Material) as a basis
if self.use_selected_material:
# We do this by simply renaming the selected material to the base material name
# (and renaming the existing material with the "base name", essentially swapping them)
selected_material = self.material_base_name
base_name = self.split_name(selected_material, ignore_base_name=True)[0]
# If there is no material with the base name, we just rename the selected material directly
if base_name not in bpy.data.materials.keys():
bpy.data.materials[selected_material].name = base_name
else:
# We need a temporary name, since we can't rename them simultaneously
temp_name = "_MU_" + selected_material + "_"
bpy.data.materials[selected_material].name = temp_name
bpy.data.materials[base_name].name = selected_material
bpy.data.materials[temp_name].name = base_name
self.material_base_name = base_name
self.replace_name()
if self.check_no_name:
self.main_loop(context)
# IF the user wants to change the name, do it now
if self.use_new_name:
bpy.data.materials[self.material_base_name].name = self.material_new_name
else:
self.report({'WARNING'}, "No Material Base Name given!")
self.is_not_undo = False
return {'CANCELLED'}
else:
self.main_loop(context)
self.material_base_name = ""
if self.remove_unused:
for mat in self.merged_materials:
print("Removing " + mat)
bpy.data.materials.remove(bpy.data.materials[mat])
if self.material_error:
materials = ", ".join(self.material_error)
if len(self.material_error) == 1:
waswere = " was"
suff_s = ""
else:
waswere = " were"
suff_s = "s"
self.report({'WARNING'},
materials + waswere + " not removed or set as Base" + suff_s)
self.is_not_undo = False
return {'FINISHED'}
class MATERIAL_OT_materialutilities_material_slot_move(bpy.types.Operator):
"""Move the active material slot"""
bl_idname = 'material.materialutilities_slot_move'
bl_label = "Move Slot"
bl_description = "Move the material slot"
bl_options = {'REGISTER', 'UNDO'}
movement: EnumProperty(
name = "Move",
description = "How to move the material slot",
items = mu_material_slot_move_enums
)
@classmethod
def poll(self, context):
# would prefer to access self.movement here, but can't..
obj = context.active_object
if not obj:
return False
if obj.active_material_index < 0 or len(obj.material_slots) <= 1:
return False
return True
def execute(self, context):
active_object = context.active_object
active_material = context.object.active_material
if self.movement == 'TOP':
dir = 'UP'
steps = active_object.active_material_index
else:
dir = 'DOWN'
last_slot_index = len(active_object.material_slots) - 1
steps = last_slot_index - active_object.active_material_index
if steps == 0:
self.report({'WARNING'},
active_material.name + " already at " + self.movement.lower() + "!")
else:
for i in range(steps):
bpy.ops.object.material_slot_move(direction = dir)
self.report({'INFO'},
active_material.name + ' moved to ' + self.movement.lower())
return {'FINISHED'}
class MATERIAL_OT_materialutilities_join_objects(bpy.types.Operator):
"""Join objects that have the same (selected) material(s)"""
bl_idname = 'material.materialutilities_join_objects'
bl_label = "Join by material (Material Utilities)"
bl_description = "Join objects that share the same material"
bl_options = {'REGISTER', 'UNDO'}
material_name: StringProperty(
name = "Material",
default = "",
description = 'Material to use to join objects'
)
is_auto: BoolProperty(
name = "Auto Join",
description = "Join objects for all materials"
)
is_not_undo = True
material_error = [] # collect material names for warning messages
@classmethod
def poll(self, context):
# This operator only works in Object mode
return (context.mode == 'OBJECT') and (len(context.visible_objects) > 0)
def draw(self, context):
layout = self.layout
box_1 = layout.box()
box_1.prop_search(self, 'material_name', bpy.data, 'materials')
box_1.enabled = not self.is_auto
layout.separator()
layout.prop(self, 'is_auto', text = "Auto Join", icon = 'SYNTAX_ON')
def invoke(self, context, event):
self.is_not_undo = True
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
# Reset Material errors, otherwise we risk reporting errors erroneously.
self.material_error = []
materials = []
if not self.is_auto:
if self.material_name == "":
self.report({'WARNING'}, "No Material Name given!")
self.is_not_undo = False
return {'CANCELLED'}
materials = [self.material_name]
else:
materials = bpy.data.materials.keys()
result = mu_join_objects(self, materials)
self.is_not_undo = False
return result
class MATERIAL_OT_materialutilities_auto_smooth_angle(bpy.types.Operator):
"""Set Auto smooth values for selected objects"""
# Inspired by colkai
bl_idname = 'view3d.materialutilities_auto_smooth_angle'
bl_label = "Set Auto Smooth Angle (Material Utilities)"
bl_options = {'REGISTER', 'UNDO'}
affect: EnumProperty(
name = "Affect",
description = "Which objects to affect",
items = mu_affect_enums,
default = 'SELECTED'