-
Notifications
You must be signed in to change notification settings - Fork 0
/
apogee_tools.py
288 lines (237 loc) · 10.7 KB
/
apogee_tools.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
bl_info = {
"name": "Apogee Tools",
"author": "Henri Hebeisen",
"version": (1, 0),
"blender": (2, 83, 0),
"location": "View3D >T Menu",
"description": "Various tools to help production of Apogee short film",
"warning": "",
"doc_url": "",
"category": "Object",
}
import bpy
from bpy.types import Operator,AddonPreferences,Panel,PropertyGroup
from bpy.props import StringProperty,PointerProperty,EnumProperty
from os import listdir,mkdir,path
from os.path import isfile, join,basename,dirname,exists
import json
class ApogeeAddonPreferences(AddonPreferences):
bl_idname = __name__
half_res: StringProperty(
name="Half Resolution name",
default='half_res'
)
quarter_res: StringProperty(
name="Quarter Resolution name",
default='quarter_res'
)
def draw(self, context):
layout = self.layout
layout.prop(self, "half_res")
layout.prop(self, "quarter_res")
# Utilities
def dump_dict_to_json(slots_status):
file_name = bpy.context.blend_data.filepath[:-6]
with open('{}.json'.format(file_name), 'w') as file:
file.write(json.dumps(slots_status))
def read_dict_from_json():
file_name = bpy.context.blend_data.filepath[:-6]
with open('{}.json'.format(file_name)) as json_file:
return json.load(json_file)
def get_initial_material_slots_as_dict(object):
#TODO Handle empty slots
return {object.name:{'original':[slot.material.name for slot in object.material_slots]}}
def override_object_materials(object,mat_override):
for slot in object.material_slots:
slot.material = bpy.data.materials[mat_override]
return {'overriden':[slot.material.name for slot in object.material_slots]}
def remove_object_override_materials(object_name,original_materials):
obj = bpy.data.objects[object_name]
for slot,original_mat_name in zip(obj.material_slots,original_materials):
original_mat = bpy.data.materials[original_mat_name]
slot.material = original_mat
if original_mat.use_fake_user and original_mat.users > 0:
original_mat.use_fake_user = False
#Methods
def get_slots_in_selection(self,context):
try :
slots = read_dict_from_json()
except:
slots = {}
for object in context.selected_objects:
if object.type == 'EMPTY' and object.instance_collection is not None:
for obj in bpy.data.collections[object.instance_collection.name].all_objects:
if obj.name in slots and 'overriden' in slots[obj.name]:
continue
slots.update(get_initial_material_slots_as_dict(obj))
elif object.material_slots is not None and len(object.material_slots) > 0:
if object.name in slots and 'overriden' in slots[object.name]:
continue
slots.update(get_initial_material_slots_as_dict(object))
return slots
def override_selection_materials(self,context,mat_override):
slots = read_dict_from_json()
for object in context.selected_objects:
if object.type == 'EMPTY' and object.instance_collection is not None:
for obj in bpy.data.collections[object.instance_collection.name].all_objects:
overriden = override_object_materials(obj,mat_override)
slots[obj.name].update(overriden)
elif object.material_slots is not None and len(object.material_slots) > 0:
overriden = override_object_materials(object,mat_override)
slots[object.name].update(overriden)
#we check if some materials don't have any users
for mat in bpy.data.materials:
mat.use_fake_user = mat.users == 0
return slots
def remove_override_selection(self,context):
slots = read_dict_from_json()
for object in context.selected_objects:
if object.type == 'EMPTY' and object.instance_collection is not None:
for obj in bpy.data.collections[object.instance_collection.name].all_objects:
remove_object_override_materials(obj.name,slots[obj.name]['original'])
slots[obj.name].pop('overriden',None)
elif object.material_slots is not None and len(object.material_slots) > 0:
remove_object_override_materials(object.name,slots[object.name]['original'])
slots[object.name].pop('overriden',None)
return slots
def reset_path_to_default(context,path):
'''We reset the path to its default, i.e. the path links to folder names Textures'''
preferences = context.preferences
addon_prefs = preferences.addons[__name__].preferences
if path.endswith(addon_prefs.half_res):
return path[:-len(addon_prefs.half_res)-1] #we remove the name and the _
elif path.endswith(addon_prefs.quarter_res):
return path[:-len(addon_prefs.quarter_res)-1] #idem
else:
return path
def change_images_path(self,context,resolution=None):
results = { 'success':[],'fail':[]}
preferences = context.preferences
addon_prefs = preferences.addons[__name__].preferences
for image in bpy.data.images:
current_path = bpy.path.abspath(image.filepath)
if not current_path:
continue
new_path = reset_path_to_default(context,dirname(current_path))
if resolution is not None:
new_path = "{}_{}".format(new_path, resolution)
new_path = join(new_path, basename(current_path))
if exists(new_path):
image.filepath = new_path
results['success'].append(image)
else:
results['fail'].append(image)
self.report({'WARNING'}, 'Could not change image for %s' %new_path)
self.report({'INFO'}, 'Success : {} | Fail : {}'.format(len(results['success']),len(results['fail'])))
class OBJECT_OT_OVERRIDE_SELECTION_MATERIAL(Operator):
"""Override all the materials in the current selection and store original materials in a json"""
bl_idname = "apogee.override_materials"
bl_label = "Override selected object materials"
bl_options = {'REGISTER', 'UNDO'}
def execute(self,context):
if not bpy.context.blend_data.filepath:
self.report({'WARNING'},'Please save the file first')
return {'FINISHED'}
if not context.scene.apogee_override_material:
self.report({'WARNING'},'Select a material first !')
return {'FINISHED'}
slots = get_slots_in_selection(self,context)
dump_dict_to_json(slots)
slots = override_selection_materials(self,context,context.scene.apogee_override_material)
dump_dict_to_json(slots)
return {'FINISHED'}
class OBJECT_OT_DELETE_OVERRIDE(Operator):
"""Delete Override"""
bl_idname = "apogee.delete_override"
bl_label = "Delete override on selection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self,context):
slots = remove_override_selection(self,context)
dump_dict_to_json(slots)
return {'FINISHED'}
class OBJECT_OT_images_half_res(Operator):
"""Change the path of all the textures in file to half of their resolution"""
bl_idname = "apogee.textures_half_size"
bl_label = "half size textures"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
preferences = context.preferences
addon_prefs = preferences.addons[__name__].preferences
change_images_path(self, context,resolution=addon_prefs.half_res)
return {'FINISHED'}
class OBJECT_OT_images_full_res(Operator):
"""Change the path of all the textures in file to their full resolution"""
bl_idname = "apogee.textures_full_size"
bl_label = "full size textures"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
change_images_path(self, context)
return {'FINISHED'}
class OBJECT_OT_images_quarter_res(Operator):
"""Change the path of all the textures in file to quarter of their resolution"""
bl_idname = "apogee.textures_quarter_size"
bl_label = "quarter size textures"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
preferences = context.preferences
addon_prefs = preferences.addons[__name__].preferences
change_images_path(self, context,resolution=addon_prefs.quarter_res)
return {'FINISHED'}
# User Interface
class VIEW3D_PT_apogee_tools_panel(Panel):
"""Panel for all Apogee Tools"""
bl_label = "Texture Size"
bl_category = "Apogee"
bl_idname = "APOGEE_PT_texture_size"
bl_space_type = 'VIEW_3D'
bl_region_type = "UI"
bl_context = "objectmode"
bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
scene = context.scene
layout.label(text="Change textures sizes:")
row = layout.row(align=True)
row.operator("apogee.textures_full_size",text="Full Size")
row.operator("apogee.textures_half_size",text="Half Size")
row.operator("apogee.textures_quarter_size",text="Quarter Size")
class APOGEE_PT_override_materials(Panel):
"""Override material with a custom one"""
bl_label = "Override Material"
bl_category = "Apogee"
bl_idname = "APOGGE_PT_override_material"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_context = "objectmode"
bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
scene = context.scene
layout.prop_search(scene, "apogee_override_material", bpy.data, "materials",text="Material Override")
row = layout.row(align=True)
row.operator("apogee.override_materials",text="Override current selection materials")
row = layout.row(align=True)
row.operator("apogee.delete_override",text="Delete overrides")
# Registration
def register():
bpy.utils.register_class(OBJECT_OT_images_half_res)
bpy.utils.register_class(OBJECT_OT_images_full_res)
bpy.utils.register_class(OBJECT_OT_images_quarter_res)
bpy.utils.register_class(OBJECT_OT_OVERRIDE_SELECTION_MATERIAL)
bpy.utils.register_class(OBJECT_OT_DELETE_OVERRIDE)
bpy.utils.register_class(VIEW3D_PT_apogee_tools_panel)
bpy.utils.register_class(APOGEE_PT_override_materials)
bpy.utils.register_class(ApogeeAddonPreferences)
bpy.types.Scene.apogee_override_material = StringProperty()
def unregister():
bpy.utils.unregister_class(OBJECT_OT_images_half_res)
bpy.utils.unregister_class(OBJECT_OT_images_full_res)
bpy.utils.unregister_class(OBJECT_OT_images_quarter_res)
bpy.utils.unregister_class(OBJECT_OT_OVERRIDE_SELECTION_MATERIAL)
bpy.utils.unregister_class(OBJECT_OT_DELETE_OVERRIDE)
bpy.utils.unregister_class(VIEW3D_PT_apogee_tools_panel)
bpy.utils.unregister_class(APOGEE_PT_override_materials)
bpy.utils.unregister_class(ApogeeAddonPreferences)
del bpy.types.Scene.apogee_override_material
if __name__ == "__main__":
register()