-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPOLYGON_Farm.py
446 lines (361 loc) · 15.6 KB
/
POLYGON_Farm.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
import bpy
import os
## IMPORTANT! Convert the SourceFiles/Characters/*.fbx from ASCII to FBX 2013.
## Put the converted files in a 'FBX 2013' subfolder like so:
## SourceFiles/Characters/FBX 2013/*.fbx
## Do the same for the following files in SourceFiles/FBX/ :
## - SM_Chr_Attach_ScarecrowHat_01.fbx
## - SM_Veh_Pickup_01.fbx
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']
# Find our character FBX's - https://blender.stackexchange.com/a/253543
folder = bpy.path.abspath("//SourceFiles/Characters/FBX 2013")
fbxs = [f for f in os.listdir(folder) \
if f.endswith(".fbx") \
and f.startswith("SK_Chr_")
]
# 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,
)
# Loop through Characters
first_armature = None
collection = bpy.data.collections["Characters"]
for obj in collection.objects:
# Save the first armature object
if first_armature == None and obj.type == 'ARMATURE':
first_armature = obj
continue
if obj.type == 'MESH':
# Always use non-duplicate version of material
obj.active_material = bpy.data.materials[
obj
.active_material.name
.split('.')[0]
]
# Rename Meshes to be the same as their parent MeshObjects
obj.data.name = obj.name
if first_armature != None and obj.modifiers[0].type == 'ARMATURE':
# Set all mesh Armature modifiers to the first armature object
obj.modifiers[0].object = first_armature
# Make our first armature the object parent
obj.parent = first_armature
# Deselect all
bpy.ops.object.select_all(action='DESELECT')
# Select unnecessary armatures
first_armature = None
for obj in collection.objects:
if obj.type == 'ARMATURE':
if first_armature == None:
first_armature = obj
else:
obj.select_set(True)
# Delete them
bpy.ops.object.delete()
first_armature.name = "Armature"
def import_attachments():
# 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['Character_Attachments']
# Find our character 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_Attach_") \
and f not in ["SM_Chr_Attach_ScarecrowHat_01.fbx"]
]
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,
)
# And the converted one
bpy.ops.import_scene.fbx(
filepath=bpy.path.abspath("//SourceFiles/FBX/FBX 2013/SM_Chr_Attach_ScarecrowHat_01.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':
# Use the same material as the characters
obj.active_material = bpy.data.materials["lambert2"]
# 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 'Characters' collection - https://blender.stackexchange.com/a/248563
bpy.context.view_layer.active_layer_collection = \
bpy.context.view_layer.layer_collection.children['Weapons']
# Find our character 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':
# Use the same material as the characters
obj.active_material = bpy.data.materials["lambert2"]
# 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 'Characters' collection - https://blender.stackexchange.com/a/248563
bpy.context.view_layer.active_layer_collection = \
bpy.context.view_layer.layer_collection.children['Buildings']
# Find our character 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':
# Use the same material as the characters
obj.active_material = bpy.data.materials["lambert2"]
# Rename Meshes to be the same as their parent MeshObjects
obj.data.name = obj.name
def import_environments():
# 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['Environments']
# Find our character 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_Env_") or f.startswith("SM_Generic_"))
]
collection = bpy.data.collections["Environments"]
# 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':
# Use the same material as the characters
obj.active_material = bpy.data.materials["lambert2"]
# 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 'Characters' collection - https://blender.stackexchange.com/a/248563
bpy.context.view_layer.active_layer_collection = \
bpy.context.view_layer.layer_collection.children['Props']
# Find our character 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':
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]
]
# Use the same material as the characters
if (material_slot.material.name in [
"Farm", "lambert1", "lambert23", "Vehicles", "Blade",
"lambert861"
]) \
or material_slot.material.name.startswith("Farm"):
material_slot.material = bpy.data.materials["lambert2"]
elif material_slot.material.name in ["Leaves"]:
material_slot.material = bpy.data.materials["lambert863"]
# Rename Meshes to be the same as their parent MeshObjects
obj.data.name = obj.name
def import_vehicles():
# 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['Vehicles']
# Find our character 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_Veh_") \
and f not in ["SM_Veh_Pickup_01.fbx"]
]
collection = bpy.data.collections["Vehicles"]
# 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,
)
# And the converted one
bpy.ops.import_scene.fbx(
filepath=bpy.path.abspath("//SourceFiles/FBX/FBX 2013/SM_Veh_Pickup_01.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':
# Always use non-duplicate version of material
obj.active_material = bpy.data.materials[
obj
.active_material.name
.split('.')[0]
]
# Use the same material as the characters
if obj.active_material.name in ["lambert1", "lambert2"]:
obj.active_material = bpy.data.materials["lambert2"]
# 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":
if filename in ['Fence_Alpha.tga', 'PolygonFarm_Texture_01_A.png', 'PolygonFarm_Signs.png']:
image.filepath = bpy.path.abspath(
"//SourceFiles\\Textures\\" + filename
)
elif filename == 'leavessheet_cavity.png':
image.filepath = bpy.path.abspath(
"//SourceFiles\\Textures\\Leaves_Diff.tga"
)
for material in bpy.data.materials:
# Leaves material
if material.name == 'lambert863':
material.name = "POLYGONFarm_Leaves"
# https://blender.stackexchange.com/a/129014
bsdf = material.node_tree.nodes["Principled BSDF"]
tex_image = material.node_tree.nodes.new('ShaderNodeTexImage')
tex_image.image = bpy.data.images.load(bpy.path.abspath(
"//SourceFiles\\Textures\\Leaves_Diff.tga"
))
norm_image = material.node_tree.nodes.new('ShaderNodeTexImage')
norm_image.image = bpy.data.images.load(bpy.path.abspath(
"//SourceFiles\\Textures\\Leaves_Normals.png"
))
normal_map = material.node_tree.nodes["Normal Map"]
material.node_tree.links.new(bsdf.inputs['Alpha'], tex_image.outputs['Alpha'])
material.node_tree.links.new(normal_map.inputs['Color'], norm_image.outputs['Color'])
bsdf.inputs["Metallic"].default_value = 0.0
bsdf.inputs["Specular"].default_value = 0.2
bsdf.inputs["Roughness"].default_value = 0.8
# Glass material
elif material.name == 'lambert3':
material.name = "POLYGONFarm_Glass"
bsdf = material.node_tree.nodes["Principled BSDF"]
tex_image = material.node_tree.nodes.new('ShaderNodeTexImage')
tex_image.image = bpy.data.images.load(bpy.path.abspath(
"//SourceFiles\\Textures\\PolygonFarm_Texture_01_A.png"
))
material.node_tree.links.new(bsdf.inputs['Base Color'], tex_image.outputs['Color'])
bsdf.inputs["Metallic"].default_value = 0.0
bsdf.inputs["Specular"].default_value = 0.8
bsdf.inputs["Roughness"].default_value = 0.2
bsdf.inputs["Alpha"].default_value = 0.5
elif material.name == 'fence':
material.name = "POLYGONFarm_Fence"
elif material.name == 'lambert864':
material.name = "POLYGONFarm_Signs"
bsdf = material.node_tree.nodes["Principled BSDF"]
bsdf.inputs["Metallic"].default_value = 0.0
bsdf.inputs["Specular"].default_value = 0.2
bsdf.inputs["Roughness"].default_value = 0.8
elif material.name == 'lambert9':
material.name = "POLYGONFarm_Wire"
# https://blender.stackexchange.com/a/129014
bsdf = material.node_tree.nodes["Principled BSDF"]
alpha_tex = material.node_tree.nodes['Image Texture.001']
material.node_tree.links.new(bsdf.inputs['Alpha'], alpha_tex.outputs['Alpha'])
bsdf.inputs["Metallic"].default_value = 0.0
bsdf.inputs["Specular"].default_value = 0.2
bsdf.inputs["Roughness"].default_value = 0.8
elif material.name == 'lambert2':
material.name = "POLYGONFarm_Base"
bsdf = material.node_tree.nodes["Principled BSDF"]
bsdf.inputs["Metallic"].default_value = 0.0
bsdf.inputs["Specular"].default_value = 0.2
bsdf.inputs["Roughness"].default_value = 0.8
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_attachments()
import_weapons()
import_buildings()
import_environments()
import_props()
import_vehicles()
fix_materials()
cleanup()