1# Material Utilities v2.2.0-Beta
2#
3#  Usage: Shift + Q in the 3D viewport
4#
5# Ported from 2.6/2.7 to 2.8x by
6#    Christopher Hindefjord (chrishinde) 2019
7#
8# ## Port based on 2010 version by MichaelW with some code added from latest 2.7x version
9# ## Same code may be attributed to one of the following awesome people!
10#  (c) 2016 meta-androcto, parts based on work by Saidenka, lijenstina
11#  Materials Utils: by MichaleW, lijenstina,
12#       (some code thanks to: CoDEmanX, SynaGl0w, ideasman42)
13#  Link to base names: Sybren, Texture renamer: Yadoob
14# ###
15#
16#
17# ##### BEGIN GPL LICENSE BLOCK #####
18#
19#  This program is free software; you can redistribute it and/or
20#  modify it under the terms of the GNU General Public License
21#  as published by the Free Software Foundation; either version 2
22#  of the License, or (at your option) any later version.
23#
24#  This program is distributed in the hope that it will be useful,
25#  but WITHOUT ANY WARRANTY; without even the implied warranty of
26#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27#  GNU General Public License for more details.
28#
29#  You should have received a copy of the GNU General Public License
30#  along with this program; if not, write to the Free Software Foundation,
31#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32#
33# ##### END GPL LICENSE BLOCK #####
34
35bl_info = {
36    "name": "Material Utilities",
37    "author": "MichaleW, ChrisHinde",
38    "version": (2, 2, 0),
39    "blender": (2, 80, 0),
40    "location": "View3D > Shift + Q key",
41    "description": "Menu of material tools (assign, select..) in the 3D View",
42    "warning": "Beta",
43    "doc_url": "{BLENDER_MANUAL_URL}/addons/materials/material_utils.html",
44   "category": "Material"
45}
46
47"""
48This script has several functions and operators, grouped for convenience:
49
50* assign material:
51    offers the user a list of ALL the materials in the blend file and an
52    additional "new" entry the chosen material will be assigned to all the
53    selected objects in object mode.
54
55    in edit mode the selected polygons get the selected material applied.
56
57    if the user chose "new" the new material can be renamed using the
58    "last operator" section of the toolbox.
59
60
61* select by material
62    in object mode this offers the user a menu of all materials in the blend
63    file any objects using the selected material will become selected, any
64    objects without the material will be removed from selection.
65
66    in edit mode:  the menu offers only the materials attached to the current
67    object. It will select the polygons that use the material and deselect those
68    that do not.
69
70* clean material slots
71    for all selected objects any empty material slots or material slots with
72    materials that are not used by the mesh polygons or splines will be removed.
73
74* remove material slots
75    removes all material slots of the active (or selected) object(s).
76
77* replace materials
78    lets your replace one material by another. Optionally for all objects in
79    the blend, otherwise for selected editable objects only. An additional
80    option allows you to update object selection, to indicate which objects
81    were affected and which not.
82
83* set fake user
84    enable/disable fake user for materials. You can chose for which materials
85    it shall be set, materials of active / selected / objects in current scene
86    or used / unused / all materials.
87
88"""
89
90if "bpy" in locals():
91    import importlib
92    if "enum_values" in locals():
93        importlib.reload(enum_values)
94    if "functions" in locals():
95        importlib.reload(functions)
96    if "operators" in locals():
97        importlib.reload(operators)
98    if "menues" in locals():
99        importlib.reload(menus)
100    if "preferences" in locals():
101        importlib.reload(preferences)
102else:
103    from .enum_values import *
104    from .functions import *
105    from .operators import *
106    from .menus import *
107    from .preferences import *
108
109import bpy
110from bpy.props import (
111    PointerProperty,
112    )
113from bpy.types import (
114    AddonPreferences,
115    PropertyGroup,
116    )
117
118
119# All classes used by Material Utilities, that need to be registered
120classes = (
121    VIEW3D_OT_materialutilities_assign_material_object,
122    VIEW3D_OT_materialutilities_assign_material_edit,
123    VIEW3D_OT_materialutilities_select_by_material_name,
124    VIEW3D_OT_materialutilities_copy_material_to_others,
125
126    VIEW3D_OT_materialutilities_clean_material_slots,
127    VIEW3D_OT_materialutilities_remove_material_slot,
128    VIEW3D_OT_materialutilities_remove_all_material_slots,
129
130    VIEW3D_OT_materialutilities_replace_material,
131    VIEW3D_OT_materialutilities_fake_user_set,
132    VIEW3D_OT_materialutilities_change_material_link,
133
134    MATERIAL_OT_materialutilities_merge_base_names,
135    MATERIAL_OT_materialutilities_join_objects,
136    MATERIAL_OT_materialutilities_auto_smooth_angle,
137
138    MATERIAL_OT_materialutilities_material_slot_move,
139
140    VIEW3D_MT_materialutilities_assign_material,
141    VIEW3D_MT_materialutilities_select_by_material,
142
143    VIEW3D_MT_materialutilities_clean_slots,
144    VIEW3D_MT_materialutilities_specials,
145
146    VIEW3D_MT_materialutilities_main,
147
148    VIEW3D_MT_materialutilities_preferences
149)
150
151
152# This allows you to right click on a button and link to the manual
153def materialutilities_manual_map():
154    url_manual_prefix = "https://github.com/ChrisHinde/MaterialUtilities"
155    url_manual_map = []
156    #url_manual_mapping = ()
157        #("bpy.ops.view3d.materialutilities_*", ""),
158        #("bpy.ops.view3d.materialutilities_assign_material_edit", ""),
159        #("bpy.ops.view3d.materialutilities_select_by_material_name", ""),)
160
161    for cls in classes:
162        if issubclass(cls, bpy.types.Operator):
163            url_manual_map.append(("bpy.ops." + cls.bl_idname, ""))
164
165    url_manual_mapping = tuple(url_manual_map)
166    #print(url_manual_mapping)
167    return url_manual_prefix, url_manual_mapping
168
169mu_classes_register, mu_classes_unregister = bpy.utils.register_classes_factory(classes)
170
171
172def register():
173    """Register the classes of Material Utilities together with the default shortcut (Shift+Q)"""
174    mu_classes_register()
175
176    bpy.types.VIEW3D_MT_object_context_menu.append(materialutilities_specials_menu)
177
178    bpy.types.MATERIAL_MT_context_menu.prepend(materialutilities_menu_move)
179    bpy.types.MATERIAL_MT_context_menu.append(materialutilities_menu_functions)
180
181    kc = bpy.context.window_manager.keyconfigs.addon
182    if kc:
183        km = kc.keymaps.new(name = "3D View", space_type = "VIEW_3D")
184        kmi = km.keymap_items.new('wm.call_menu', 'Q', 'PRESS', ctrl = False, shift = True)
185        kmi.properties.name = VIEW3D_MT_materialutilities_main.bl_idname
186
187        bpy.utils.register_manual_map(materialutilities_manual_map)
188
189
190def unregister():
191    """Unregister the classes of Material Utilities together with the default shortcut for the menu"""
192
193    bpy.utils.unregister_manual_map(materialutilities_manual_map)
194
195    bpy.types.VIEW3D_MT_object_context_menu.remove(materialutilities_specials_menu)
196
197    bpy.types.MATERIAL_MT_context_menu.remove(materialutilities_menu_move)
198    bpy.types.MATERIAL_MT_context_menu.remove(materialutilities_menu_functions)
199
200    kc = bpy.context.window_manager.keyconfigs.addon
201    if kc:
202        km = kc.keymaps["3D View"]
203        for kmi in km.keymap_items:
204            if kmi.idname == 'wm.call_menu':
205                if kmi.properties.name == VIEW3D_MT_materialutilities_main.bl_idname:
206                    km.keymap_items.remove(kmi)
207                    break
208
209    mu_classes_unregister()
210
211if __name__ == "__main__":
212    register()
213