-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPOLYGON_Adventure.py
312 lines (248 loc) · 10.5 KB
/
POLYGON_Adventure.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
import bpy
import os
from mathutils import Matrix
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("//Character_Files/Unity_Version_Mechanim")
fbxs = [f for f in os.listdir(folder) if f.endswith(".fbx")]
# 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,
)
collection = bpy.data.collections["Characters"]
# Loop through Characters
first_armature = None
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 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"
# Apply location, rotation, scale to deltas
for obj in collection.all_objects:
if obj.name != 'Armature':
mat = obj.matrix_local
obj.data.transform(mat)
obj.matrix_local = Matrix()
else:
obj.delta_rotation_euler.rotate(obj.rotation_euler)
obj.rotation_euler = (0, 0, 0)
obj.delta_scale = obj.scale
obj.scale = (1, 1, 1)
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("//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':
# 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
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("//FBX")
fbxs = [f for f in os.listdir(folder) \
if f.endswith(".fbx") and f.startswith("SM_Env_")
]
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':
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 material_slot.material.name in ['blinn283', 'blinn284', 'blinn285', 'lambert2']:
# material_slot.material = bpy.data.materials["lambert14"]
# 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("//FBX")
fbxs = [f for f in os.listdir(folder) \
if f.endswith(".fbx") and (f.startswith("SM_Prop_") or f.startswith('SM_Item_'))
]
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]
]
# if material_slot.material.name in ['blinn283', 'lambert2']:
# material_slot.material = bpy.data.materials["lambert14"]
# 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("//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]
]
# if material_slot.material.name in ['blinn283', 'lambert2']:
# material_slot.material = bpy.data.materials["lambert14"]
# Rename Meshes to be the same as their parent MeshObjects
obj.data.name = obj.name
def fix_materials():
# Textures pointing to the wrong directory. Fix that
# 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 ["Characters_White.png", "Characters_Black.png"]:
# Make absolute
image.filepath = bpy.path.abspath(
"//Textures/" + filename
)
elif filename in ["Texture_01.psd"]:
image.filepath = bpy.path.abspath(
"//Textures/PolyAdventureTexture_01.png"
)
for material in bpy.data.materials:
if material.name in ['Black', 'White']:
material.name = 'POLYGONAdventure_' + material.name
# https://blender.stackexchange.com/a/129014
bsdf = material.node_tree.nodes["Principled BSDF"]
bsdf.inputs["Metallic"].default_value = 0.0
bsdf.inputs["Specular"].default_value = 0.5
bsdf.inputs["Roughness"].default_value = 0.5
elif material.name in ['blinn265']:
material.name = 'POLYGONAdventure_Base'
# https://blender.stackexchange.com/a/129014
bsdf = material.node_tree.nodes["Principled BSDF"]
bsdf.inputs["Metallic"].default_value = 0.0
bsdf.inputs["Specular"].default_value = 0.5
bsdf.inputs["Roughness"].default_value = 0.5
def cleanup():
bpy.ops.object.select_all(action='DESELECT')
# Apply rotation, scale
for obj in bpy.data.objects:
# Skip the 'Characters' collection. Things mess up if we apply those.
if obj.name != 'Armature' or (obj.parent and obj.parent.name != 'Armature'):
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_buildings()
import_environments()
import_props()
import_weapons()
fix_materials()
cleanup()