-
Notifications
You must be signed in to change notification settings - Fork 5
/
import_rselu.py
249 lines (189 loc) · 8.35 KB
/
import_rselu.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
#####
# Most of the code is based on logic found in...
#
### GunZ 1
# - RTypes.h
# - RToken.h
# - RealSpace2.h/.cpp
# - RBspObject.h/.cpp
# - RMaterialList.h/.cpp
# - RMesh_Load.cpp
# - RMeshUtil.h
# - MZFile.cpp
# - R_Mtrl.cpp
# - EluLoader.h/cpp
# - LightmapGenerator.h/.cpp
# - MCPlug2_Mesh.cpp
#
### GunZ 2
# - RVersions.h
# - RTypes.h
# - RD3DVertexUtil.h
# - RStaticMeshResource.h
# - RStaticMeshResourceFileLoadImpl.cpp
# - MTypes.h
# - MVector3.h
# - MSVector.h
# - RMesh.cpp
# - RMeshNodeData.h
# - RMeshNodeLoadImpl.h/.cpp
# - RSkeleton.h/.cpp
#
# Please report maps and models with unsupported features to me on Discord: Krunk#6051
#####
import bpy, os, math
import xml.dom.minidom as minidom
from mathutils import Vector, Matrix
from .constants_gzrs2 import *
from .classes_gzrs2 import *
from .parse_gzrs2 import *
from .readelu_gzrs2 import *
from .lib_gzrs2 import *
def importElu(self, context):
state = GZRS2State()
state.convertUnits = self.convertUnits
state.doCleanup = self.doCleanup
state.doBoneRolls = self.doBoneRolls
state.doTwistConstraints = self.doTwistConstraints
if self.panelLogging:
print()
print("=======================================================================")
print("=========================== RSELU Import ============================")
print("=======================================================================")
print(f"== { self.filepath }")
print("=======================================================================")
print()
state.logEluHeaders = self.logEluHeaders
state.logEluMats = self.logEluMats
state.logEluMeshNodes = self.logEluMeshNodes
state.logVerboseIndices = self.logVerboseIndices and self.logEluMeshNodes
state.logVerboseWeights = self.logVerboseWeights and self.logEluMeshNodes
state.logCleanup = self.logCleanup
elupath = self.filepath
state.directory = os.path.dirname(elupath)
state.filename = os.path.basename(elupath).split(os.extsep)[0]
for ext in XML_EXTENSIONS:
eluxmlpath = pathExists(f"{ elupath }.{ ext }")
if eluxmlpath:
state.xmlEluMats[elupath] = parseEluXML(self, minidom.parse(eluxmlpath), state)
break
if readElu(self, elupath, state):
return { 'CANCELLED' }
bpy.ops.ed.undo_push()
collections = bpy.data.collections
rootMesh = collections.new(state.filename)
context.collection.children.link(rootMesh)
setupErrorMat(state)
for eluMat in state.eluMats:
setupEluMat(self, eluMat, state)
if eluxmlpath:
for xmlEluMat in state.xmlEluMats[elupath]:
setupXmlEluMat(self, elupath, xmlEluMat, state)
if state.doCleanup and state.logCleanup:
print()
print("=== Elu Mesh Cleanup ===")
print()
for eluMesh in state.eluMeshes:
meshName = eluMesh.meshName
if meshName.startswith(("Bip01", "Bone")):
state.gzrsValidBones.add(meshName)
if eluMesh.isDummy:
blDummyObj = bpy.data.objects.new(meshName, None)
blDummyObj.empty_display_type = 'ARROWS'
blDummyObj.empty_display_size = 0.1
blDummyObj.matrix_world = eluMesh.transform
rootMesh.objects.link(blDummyObj)
state.blDummyObjs.append(blDummyObj)
state.blObjPairs.append((eluMesh, blDummyObj))
else:
setupElu(self, eluMesh, False, rootMesh, context, state)
processEluHeirarchy(self, state)
if len(state.gzrsValidBones) > 0:
state.blArmature = bpy.data.armatures.new("Armature")
state.blArmatureObj = bpy.data.objects.new("Armature", state.blArmature)
state.blArmatureObj.display_type = 'WIRE'
state.blArmatureObj.show_in_front = True
rootMesh.objects.link(state.blArmatureObj)
for viewLayer in context.scene.view_layers:
viewLayer.objects.active = state.blArmatureObj
bpy.ops.object.mode_set(mode = 'EDIT')
reorientLocal = Matrix.Rotation(math.radians(-90.0), 4, 'Z') @ Matrix.Rotation(math.radians(-90.0), 4, 'Y')
for eluMesh, blMeshOrDummyObj in state.blObjPairs:
if eluMesh.meshName not in state.gzrsValidBones:
continue
editBone = state.blArmature.edit_bones.new(eluMesh.meshName)
editBone.tail = (0, 0.1, 0)
editBone.matrix = blMeshOrDummyObj.matrix_world @ reorientLocal
if eluMesh.isDummy and 'Nub' in eluMesh.meshName:
for collection in blMeshOrDummyObj.users_collection:
collection.objects.unlink(blMeshOrDummyObj)
state.blBonePairs.append((eluMesh, editBone))
for child, childBone in state.blBonePairs:
if child.meshName == 'Bip01':
continue
found = False
for parent, parentBone in state.blBonePairs:
if child != parent and child.parentName == parent.meshName:
childBone.parent = parentBone
found = True
break
if not found:
self.report({ 'INFO' }, f"GZRS2: Parent not found for elu child bone: { child.meshName }, { child.parentName }")
for eluMesh, editBone in state.blBonePairs:
if editBone.name == 'Bip01':
continue
elif len(editBone.children) > 0:
length = 0
for child in editBone.children:
length = max(length, (child.head - editBone.head).length)
editBone.length = length
elif editBone.parent is not None:
editBone.length = editBone.parent.length / 2
if editBone.parent is not None and (Vector(editBone.parent.tail) - Vector(editBone.head)).length < 0.0001:
editBone.use_connect = True
if state.doBoneRolls:
bpy.ops.armature.select_all(action = 'SELECT')
bpy.ops.armature.calculate_roll(type = 'GLOBAL_POS_Z')
bpy.ops.armature.select_all(action = 'DESELECT')
bpy.ops.object.mode_set(mode = 'OBJECT')
blPoseBones = state.blArmatureObj.pose.bones
if state.doBoneRolls and state.doTwistConstraints:
for parentBone in blPoseBones:
if 'twist' in parentBone.name.lower():
for siblingBone in parentBone.parent.children:
if parentBone != siblingBone and len(siblingBone.children) > 0:
constraint = parentBone.constraints.new(type = 'TRACK_TO')
constraint.target = state.blArmatureObj
constraint.subtarget = siblingBone.children[0].name
constraint.track_axis = 'TRACK_Y'
constraint.up_axis = 'UP_Z'
constraint.use_target_z = True
constraint.target_space = 'POSE'
constraint.owner_space = 'POSE'
break
for child, childObj in state.blObjPairs:
isBone = child.meshName in state.gzrsValidBones
noParentBone = child.parentName not in state.gzrsValidBones
isNubDummy = isBone and child.isDummy and 'Nub' in child.meshName
isNotRoot = child.meshName != 'Bip01'
if isNubDummy or noParentBone and isNotRoot:
continue
targetName = child.meshName if isBone else child.parentName
found = False
for parentBone in blPoseBones:
if targetName == parentBone.name:
transform = childObj.matrix_world
childObj.parent = state.blArmatureObj
childObj.parent_bone = parentBone.name
childObj.parent_type = 'BONE'
childObj.matrix_world = transform
found = True
break
if not found:
self.report({ 'INFO' }, f"GZRS2: Bone parent not found for .elu child mesh or dummy: { child.meshName }, { child.parentName }, { child.isDummy }")
for blMeshObj in state.blMeshObjs:
modifier = getModifierByType(self, blMeshObj.modifiers, 'ARMATURE')
if modifier:
modifier.object = state.blArmatureObj
bpy.ops.object.select_all(action = 'DESELECT')
return { 'FINISHED' }