-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZUD.py
167 lines (140 loc) · 5.79 KB
/
ZUD.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
bl_info = {
"name": "Vagrant Story file formats Add-on",
"description": "Import-Export Vagrant Story file formats (WEP, SHP, SEQ, ZUD, MPD, ZND, P, FBT, FBC).",
"author": "Sigfrid Korobetski (LunaticChimera)",
"version": (2, 12),
"blender": (3, 2, 0),
"location": "File > Import-Export",
"category": "Import-Export",
}
import struct
import math
import bpy
from bpy.props import BoolProperty, EnumProperty, FloatProperty, StringProperty
from bpy_extras.io_utils import ExportHelper, ImportHelper
from . import WEP, SHP, SEQ
class Import(bpy.types.Operator, ImportHelper):
"""Load a ZUD file"""
bl_idname = "import_mesh.zud"
bl_label = "Import ZUD"
filename_ext = ".ZUD"
filepath: bpy.props.StringProperty(default="", subtype="FILE_PATH")
filter_glob: bpy.props.StringProperty(default="*.ZUD", options={"HIDDEN"})
def execute(self, context):
keywords = self.as_keywords(ignore=("axis_forward","axis_up","filter_glob",))
BlenderImport(self, context, **keywords)
return {"FINISHED"}
def BlenderImport(operator, context, filepath):
zud = ZUD()
# we read datas from a file
zud.loadFromFile(filepath)
# Creating Geometry and Meshes for Blender
zud.buildGeometry()
class ZUD:
def __init__(self):
self.name = "ZUD"
self.header = ZUDHeader()
self.shp = None
self.weapon = None
self.shield = None
self.commonSeq = None
self.battleSeq = None
def __repr__(self):
return("(--"+repr(self.name)+".ZUD-- | "+repr(self.header)+")")
def loadFromFile(self, filepath):
# Open a ZUD file and parse it
file = open(filepath, "rb")
self.name = bpy.path.display_name(filepath)
self.parse(file)
file.close()
def parse(self, file):
self.header.feed(file)
#print(self)
# SHP SECTION
file.seek(self.header.ptrSHP)
self.shp = SHP.SHP()
self.shp.name = "{:02X}".format(self.header.idSHP)+".ZSHP"
self.shp.parse(file)
# WEAPON SECTION
if self.header.idWEP != 0:
file.seek(self.header.ptrWEP)
self.weapon = WEP.WEP()
self.weapon.name = "{:02X}".format(self.header.idWEP)+".ZWEP"
self.weapon.parse(file)
# SHIELD SECTION
if self.header.idWEP2 != 0:
file.seek(self.header.ptrWEP2)
self.shield = WEP.WEP()
self.shield.name = "{:02X}".format(self.header.idWEP2)+".ZWEP"
self.shield.parse(file)
# COMMON SEQ SECTION
if self.header.lenCSEQ > 0:
file.seek(self.header.ptrCSEQ)
self.commonSeq = SEQ.SEQ()
self.commonSeq.name = self.name+"_COM"
self.commonSeq.parse(file)
# BATTLE SEQ SECTION
if self.header.lenBSEQ > 0:
file.seek(self.header.ptrBSEQ)
self.battleSeq = SEQ.SEQ()
self.battleSeq.name = self.name+"_BAT"
self.battleSeq.parse(file)
def buildGeometry(self):
#print("ZUD Building...")
shpObj = self.shp.buildGeometry()
if self.header.idWEP != 0:
wepObj = self.weapon.buildGeometry(self.header.idWEPMat)
chiof = wepObj.constraints.new(type="CHILD_OF")
chiof.target = shpObj.parent # Armature
chiof.subtarget = self.shp.getWeaponBoneName()
bpy.ops.constraint.childof_clear_inverse(constraint=chiof.name, owner="OBJECT")
wepObj.rotation_euler = (90, 0, 0)
if self.header.idWEPType == 6:
# if its a staff
wepObj.location = (2.8, 0, 0) # arbitrary value but seems not bad
if self.header.idWEP2 != 0:
shieldObj = self.shield.buildGeometry(self.header.idWEP2Mat)
chiof = shieldObj.constraints.new(type="CHILD_OF")
chiof.target = shpObj.parent # Armature
chiof.subtarget = self.shp.getShieldBoneName()
bpy.ops.constraint.childof_clear_inverse(constraint=chiof.name, owner="OBJECT")
shieldObj.rotation_euler = (90, 0, 0)
if self.header.lenCSEQ > 0:
self.commonSeq.buildAnimations(shpObj)
if self.header.lenBSEQ > 0:
self.battleSeq.buildAnimations(shpObj)
# selecting armature
shpObj.parent.name = self.name
shpObj.parent.select_set(True)
bpy.context.view_layer.objects.active = shpObj.parent
if self.header.lenCSEQ > 0:
shpObj.parent.animation_data.action = bpy.data.actions[self.commonSeq.name + "_Animation_0"]
if self.header.lenBSEQ > 0:
shpObj.parent.animation_data.action = bpy.data.actions[self.battleSeq.name + "_Animation_0"]
class ZUDHeader:
def __init__(self):
self.idSHP = 0
self.idWEP = 0
self.idWEPType = 0
self.idWEPMat = 0
self.idWEP2 = 0
self.idWEP2Mat = 0
self.uk = 0
self.pad = 0
self.ptrSHP = 0
self.lenSHP = 0
self.ptrWEP = 0
self.lenWEP = 0
self.ptrWEP2 = 0
self.lenWEP2 = 0
self.ptrCSEQ = 0
self.lenCSEQ = 0
self.ptrBSEQ = 0
self.lenBSEQ = 0
def __repr__(self):
return (" idSHP : "+ repr(self.idSHP)+ " idWEP : "+ repr(self.idWEP)+ " idWEPType : "+ repr(self.idWEPType)+ " idWEPMat : "+ repr(self.idWEPMat)+ " idWEP2 : "
+ repr(self.idWEP2)+ " idWEP2Mat : "+ repr(self.idWEP2Mat)+ " uk : "+ repr(self.uk)+ " pad : "+ repr(self.pad)
)
def feed(self, file):
self.idSHP,self.idWEP,self.idWEPType,self.idWEPMat,self.idWEP2,self.idWEP2Mat,self.uk,self.pad = struct.unpack("8B", file.read(8))
self.ptrSHP,self.lenSHP,self.ptrWEP,self.lenWEP,self.ptrWEP2,self.lenWEP2,self.ptrCSEQ,self.lenCSEQ,self.ptrBSEQ,self.lenBSEQ = struct.unpack("10I", file.read(40))