-
Notifications
You must be signed in to change notification settings - Fork 7
/
POLYGON_WarMap.py
93 lines (76 loc) · 3.11 KB
/
POLYGON_WarMap.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
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']
# 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("SK_Character_")]
# 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 first_armature != None and obj.type == 'MESH':
if 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')
# Remove IK bones
bpy.context.view_layer.objects.active = first_armature
bpy.ops.object.mode_set(mode='EDIT')
# Remove the IK bones
for bone in first_armature.data.edit_bones:
if bone.name.startswith("ik_"):
first_armature.data.edit_bones.remove(bone)
bpy.ops.object.mode_set(mode='OBJECT')
# 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()
# Apply location, rotation, scale to deltas
for obj in collection.all_objects:
obj.delta_location += obj.location
obj.location = (0, 0, 0)
obj.delta_rotation_euler.rotate(obj.rotation_euler)
obj.rotation_euler = (0, 0, 0)
obj.delta_scale = obj.scale
obj.scale = (1, 1, 1)
first_armature.name = "Characters"
# Textures pointing to the wrong directory. Fix that
# https://blender.stackexchange.com/a/280804
for image in bpy.data.images.values():
if image.source == "FILE":
# Textures are pointing towards wrong file. Fix that
filename = os.path.basename(image.filepath)
if filename == "PolygonWar_Map_Texture_Mike.psd":
filename = "PolygonWar_Map_Texture_01_A.png"
# Make absolute
image.filepath = bpy.path.abspath(
"//SourceFiles\\Textures\\" + filename
)
import_characters()