-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPOLYGON_Office.py
342 lines (278 loc) · 14.2 KB
/
POLYGON_Office.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
import bpy
import os
def import_characters():
# Deselect all
bpy.ops.object.select_all(action='DESELECT')
# Highlight the 'Characters' collection - https://blender.stackexchange.com/a/248563
bpy.context.view_layer.active_layer_collection = \
bpy.context.view_layer.layer_collection.children['Characters']
# Import the characters
bpy.ops.import_scene.fbx(
filepath=bpy.path.abspath("//SourceFiles/FBX/Character.fbx"),
use_anim=False,
ignore_leaf_bones=True,
force_connect_children=True,
automatic_bone_orientation=True,
)
collection = bpy.data.collections["Characters"]
for obj in collection.objects:
if obj.type == 'MESH':
# All these characters share the same material
obj.active_material = bpy.data.materials['lambert295']
def import_character_attachments():
# Deselect all
bpy.ops.object.select_all(action='DESELECT')
# Highlight the 'Buildings' collection - https://blender.stackexchange.com/a/248563
bpy.context.view_layer.active_layer_collection = \
bpy.context.view_layer.layer_collection.children['Character_Attachments']
# Find our FBX's - https://blender.stackexchange.com/a/253543
folder = bpy.path.abspath("//SourceFiles/FBX/")
fbxs = [f for f in os.listdir(folder) \
if f.endswith(".fbx") and f.startswith("SM_Chr_Attachment_")
]
collection = bpy.data.collections["Character_Attachments"]
# Import them
for fbx in fbxs:
bpy.ops.import_scene.fbx(
filepath=os.path.join(folder, fbx),
use_anim=False,
ignore_leaf_bones=True,
force_connect_children=True,
automatic_bone_orientation=True,
)
for obj in collection.objects:
if obj.type == 'MESH':
# All char attachments in this pack use a single texture and it's always
# the base one.
obj.active_material = bpy.data.materials['lambert295']
# Rename Meshes to be the same as their parent MeshObjects
obj.data.name = obj.name
def import_buildings():
# Deselect all
bpy.ops.object.select_all(action='DESELECT')
# Highlight the 'Buildings' collection - https://blender.stackexchange.com/a/248563
bpy.context.view_layer.active_layer_collection = \
bpy.context.view_layer.layer_collection.children['Buildings']
# Find our FBX's - https://blender.stackexchange.com/a/253543
folder = bpy.path.abspath("//SourceFiles/FBX/")
fbxs = [f for f in os.listdir(folder) \
if f.endswith(".fbx") and f.startswith("SM_Bld_")
]
collection = bpy.data.collections["Buildings"]
# Import them
for fbx in fbxs:
bpy.ops.import_scene.fbx(
filepath=os.path.join(folder, fbx),
use_anim=False,
ignore_leaf_bones=True,
force_connect_children=True,
automatic_bone_orientation=True,
)
for obj in collection.objects:
if obj.type == 'MESH':
for material_slot in obj.material_slots:
# Always use non-duplicate version of material
material_slot.material = bpy.data.materials[
material_slot.material.name.split('.')[0]
]
for material_slot in obj.material_slots:
if material_slot.material.name.startswith("A_Glass") \
or material_slot.material.name.startswith("Glass"):
material_slot.material = bpy.data.materials["A_Glass"]
elif material_slot.material.name.startswith('lambert') \
or material_slot.material.name.startswith('OfficeSHD'):
material_slot.material = bpy.data.materials['lambert295']
# Rename Meshes to be the same as their parent MeshObjects
obj.data.name = obj.name
def import_props():
# Deselect all
bpy.ops.object.select_all(action='DESELECT')
# Highlight the 'Buildings' collection - https://blender.stackexchange.com/a/248563
bpy.context.view_layer.active_layer_collection = \
bpy.context.view_layer.layer_collection.children['Props']
# Find our FBX's - https://blender.stackexchange.com/a/253543
folder = bpy.path.abspath("//SourceFiles/FBX/")
fbxs = [f for f in os.listdir(folder) \
if f.endswith(".fbx") and f.startswith("SM_Prop_")
]
collection = bpy.data.collections["Props"]
# Import them
for fbx in fbxs:
bpy.ops.import_scene.fbx(
filepath=os.path.join(folder, fbx),
use_anim=False,
ignore_leaf_bones=True,
force_connect_children=True,
automatic_bone_orientation=True,
)
for obj in collection.objects:
if obj.type == 'MESH':
# A couple of meshes have incorrect materials
if obj.name in ['SM_Prop_Sculpture_02', 'SM_Prop_Sculpture_Base_01']:
obj.active_material = bpy.data.materials['lambert295']
# This one has a material named 'lambert12' and it's the only
# one that needs to be set to Chrome so do that here to avoid
# name clashes in other meshes.
elif obj.name == 'SM_Prop_Sculpture_01':
obj.active_material.name = 'POLYGONOffice_Chrome'
for material_slot in obj.material_slots:
# Always use non-duplicate version of material
material_slot.material = bpy.data.materials[
material_slot.material.name.split('.')[0]
]
if obj.name == 'SM_Prop_Clock_02' and material_slot.material.name == 'lambert2':
material_slot.material = bpy.data.materials["Screen"]
elif obj.name == 'SM_Prop_Laptop_02' and material_slot.material.name == 'OfficeSHD18':
material_slot.material = bpy.data.materials["Screen"]
elif obj.name == 'SM_Prop_Laptop_01' and material_slot.material.name == 'OfficeSHD16':
material_slot.material = bpy.data.materials["Screen"]
elif obj.name == 'SM_Prop_GraphicsTablet_02' and material_slot.material.name == 'OfficeSHD1':
material_slot.material = bpy.data.materials["Screen"]
elif obj.name == 'SM_Prop_GraphicsTablet_03' and material_slot.material.name == 'OfficeSHD1':
material_slot.material = bpy.data.materials["Screen3"]
elif obj.name == 'SM_Prop_GraphicsTablet_04' and material_slot.material.name == 'OfficeSHD1':
material_slot.material = bpy.data.materials["Screen2"]
elif obj.name == 'SM_Prop_TV_Wall_01' and material_slot.material.name == '_lambert2':
material_slot.material = bpy.data.materials["Screen2"]
elif obj.name in ['SM_Prop_Photocopier_01', 'SM_Prop_Photocopier_02'] and material_slot.material.name == 'Screen':
material_slot.material = bpy.data.materials["Screen2"]
elif obj.name == 'SM_Prop_Laptop_Folding_Screen_01' and material_slot.material.name == 'OfficeSHD16':
material_slot.material = bpy.data.materials["Screen"]
elif obj.name == 'SM_Prop_Laptop_Folding_Screen_02' and material_slot.material.name == 'OfficeSHD16':
material_slot.material = bpy.data.materials["Screen2"]
elif obj.name == 'SM_Prop_Monitor_Crt_01' and material_slot.material.name == 'OfficeSHD7':
material_slot.material = bpy.data.materials["Screen"]
elif obj.name == 'SM_Prop_ArcadeMachine_01' and material_slot.material.name == 'OfficeSHD1':
material_slot.material = bpy.data.materials.new(name="POLYGONOffice_Arcade")
elif obj.name == 'SM_Prop_Bottle_01' and material_slot.material.name == 'lambert8':
material_slot.material = bpy.data.materials["A_Glass"]
elif material_slot.material.name == 'glassSHD':
material_slot.material = bpy.data.materials["A_Glass"]
# Everything else goes to default mat
elif material_slot.material.name.startswith("lambert") \
or material_slot.material.name.startswith("Office") \
or material_slot.material.name.startswith("gang"):
material_slot.material = bpy.data.materials["lambert295"]
# Rename Meshes to be the same as their parent MeshObjects
obj.data.name = obj.name
def import_weapons():
# Deselect all
bpy.ops.object.select_all(action='DESELECT')
# Highlight the 'Buildings' collection - https://blender.stackexchange.com/a/248563
bpy.context.view_layer.active_layer_collection = \
bpy.context.view_layer.layer_collection.children['Weapons']
# Find our FBX's - https://blender.stackexchange.com/a/253543
folder = bpy.path.abspath("//SourceFiles/FBX/")
fbxs = [f for f in os.listdir(folder) \
if f.endswith(".fbx") and f.startswith("SM_Wep_")
]
collection = bpy.data.collections["Weapons"]
# Import them
for fbx in fbxs:
bpy.ops.import_scene.fbx(
filepath=os.path.join(folder, fbx),
use_anim=False,
ignore_leaf_bones=True,
force_connect_children=True,
automatic_bone_orientation=True,
)
for obj in collection.objects:
if obj.type == 'MESH':
for material_slot in obj.material_slots:
# Always use non-duplicate version of material
material_slot.material = bpy.data.materials[
material_slot.material.name.split('.')[0]
]
# All weapons in this pack use a single texture and it's always
# the base one.
obj.active_material = bpy.data.materials['lambert295']
# Rename Meshes to be the same as their parent MeshObjects
obj.data.name = obj.name
def fix_materials():
# https://blender.stackexchange.com/a/280804
for image in bpy.data.images.values():
filename = os.path.basename(image.filepath)
if image.source == "FILE":
# Eye/Eyebrow pointing to wrong file in wrong directory. Fix that
if filename == "PolygonOffice_Texture_01_A_New.psd":
# Make absolute
image.filepath = bpy.path.abspath(
"//SourceFiles/Textures/PolygonOffice_Texture_01_A.png"
)
else:
# Make absolute
image.filepath = bpy.path.abspath(
"//SourceFiles/Textures/" + filename
)
for material in bpy.data.materials:
if material.name == 'lambert295':
material.name = 'POLYGONOffice_Base'
# https://blender.stackexchange.com/a/129014
bsdf = material.node_tree.nodes["Principled BSDF"]
bsdf.inputs["Metallic"].default_value = 0.0
bsdf.inputs["Specular IOR Level"].default_value = 0.2
bsdf.inputs["Roughness"].default_value = 0.8
tex_emissive = material.node_tree.nodes.new('ShaderNodeTexImage')
tex_emissive.image = bpy.data.images.load(bpy.path.abspath(
"//SourceFiles/Textures/Emissive_01.png"
))
tex_emissive_mp = material.node_tree.nodes.new('ShaderNodeMath')
tex_emissive_mp.operation = 'MULTIPLY'
tex_emissive_mp.inputs[1].default_value = 2
material.node_tree.links.new(tex_emissive_mp.inputs[0], tex_emissive.outputs['Color'])
material.node_tree.links.new(bsdf.inputs['Emission Strength'], tex_emissive_mp.outputs[0])
elif material.name == 'A_Glass':
material.name = 'POLYGONOffice_Glass'
# https://blender.stackexchange.com/a/129014
bsdf = material.node_tree.nodes["Principled BSDF"]
bsdf.inputs["Alpha"].default_value = 0.5
elif material.name == 'Net1':
material.name = 'POLYGONOffice_Net'
bsdf = material.node_tree.nodes["Principled BSDF"]
tex_emissive = material.node_tree.nodes["Image Texture.001"]
material.node_tree.links.new(bsdf.inputs['Alpha'], tex_emissive.outputs['Alpha'])
elif material.name == 'POLYGONOffice_Arcade':
material.use_nodes = True
bsdf = material.node_tree.nodes.new("ShaderNodeBsdfPrincipled")
tex_albedo = material.node_tree.nodes.new('ShaderNodeTexImage')
tex_albedo.image = bpy.data.images.load(bpy.path.abspath(
"//SourceFiles/Textures/PolygonOffice_Texture_Sceen_Arcade_01.png"
))
material.node_tree.links.new(bsdf.inputs['Base Color'], tex_albedo.outputs['Color'])
material.node_tree.links.new(bsdf.inputs['Emission Color'], tex_albedo.outputs['Color'])
# Set up the 5 Screen materials
for i in range(1, 4):
if material.name == 'Screen' + str(i).replace('1', ''):
material.name = 'POLYGONOffice_Screen' + str(i)
bsdf = material.node_tree.nodes["Principled BSDF"]
tex_albedo = material.node_tree.nodes.new('ShaderNodeTexImage')
tex_albedo.image = bpy.data.images.load(bpy.path.abspath(
"//SourceFiles/Textures/PolygonOffice_Texture_Sceen_0" + str(i) + ".png"
))
material.node_tree.links.new(bsdf.inputs['Base Color'], tex_albedo.outputs['Color'])
material.node_tree.links.new(bsdf.inputs['Emission Color'], tex_albedo.outputs['Color'])
def cleanup():
# Apply rotation, scale
for obj in bpy.data.objects:
obj.select_set(True)
bpy.ops.object.transform_apply(scale=True, rotation=True)
# Removed orphaned data
for block in bpy.data.meshes:
if block.users == 0:
bpy.data.meshes.remove(block)
for block in bpy.data.materials:
if block.users == 0:
bpy.data.materials.remove(block)
for block in bpy.data.textures:
if block.users == 0:
bpy.data.textures.remove(block)
for block in bpy.data.images:
if block.users == 0:
bpy.data.images.remove(block)
import_characters()
import_character_attachments()
import_buildings()
import_props()
import_weapons()
fix_materials()
cleanup()