-
Notifications
You must be signed in to change notification settings - Fork 7
/
import_odr.py
195 lines (148 loc) · 6.45 KB
/
import_odr.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
import bpy
import os.path
import traceback
from mathutils import *
from . import reader_utils
from . import import_skel
from . import import_mesh
from . import rigging_utils
def import_odr_from_file(filepath, alsoApplyData = True):
filename = os.path.splitext(os.path.basename(filepath))[0]
print("Import GTAV ODR {} : begin".format(filename))
with open(filepath, 'r', encoding='utf-8') as reader:
odrData = string_to_odr(reader, filename, filepath)
if alsoApplyData:
odrData.apply_data()
return odrData
def string_to_odr(reader, odrName, odrPath):
"""returns an ODRData with the info gathered from the file"""
#keep reading until we reach an empty line (end of file)
line = reader.readline()
odrData = ODRData()
odrData.path = odrPath
while line != '':
check_relevant_section_start(reader, line, odrData)
line = reader.readline()
print("done reading ODR data from file {}".format(odrName))
return odrData
def check_relevant_section_start(reader, line, odrData):
"""checks the line content for names of sections of interest, like Shaders"""
if "Shaders" in line:
parse_shaders(reader, line, odrData)
elif "Skeleton" in line:
parse_skeleton(reader, line, odrData)
elif "LodGroup" in line:
parse_lodgroups(reader, line, odrData)
def parse_lodgroups(reader, line, odrData):
print("parsing lodmodels...")
while "}" not in line:
if "High" in line or "Med" in line or "Low" in line or "Vlow" in line:
#the parsing func takes care of moving the reader in this case
line = parse_lodmodel_data(reader, line, odrData)
else:
line = reader.readline()
def parse_lodmodel_data(reader, line, odrData):
#starting from the "model category" (high, med etc) line, check if we open curly braces in the next line
#if we don't, we probably don't have a model declared for this LOD level
line = reader.readline()
if "{" in line:
line = reader.readline()
meshPath = line.strip().split(" ")[0]
if meshPath != "null":
odrPath = os.path.dirname(odrData.path)
meshPath = os.path.join(odrPath, meshPath)
odrData.meshPaths.append(meshPath)
print("reference to mesh at {}".format(meshPath))
#get past the "}" line to make the parser keep going
line = reader.readline()
line = reader.readline()
return line
def parse_skeleton(reader, line, odrData):
#if not null, a relative path to the skel file is declared
skelPath = line.strip().split(" ")[1]
if skelPath != "null":
odrPath = os.path.dirname(odrData.path)
odrData.skeletonFilePath = os.path.join(odrPath, skelPath)
print("got skeleton at {}".format(odrData.skeletonFilePath))
def parse_shaders(reader, line, odrData):
while "}" not in line:
if ".sps" in line:
odrData.shaders.append(parse_shader_data(reader, line, odrData))
line = reader.readline()
def parse_shader_data(reader, line, odrData):
shader = ODRShader()
#we start in the "shader type" line
shader.shaderType = line.strip().split(".")[0]
print("parsing shader {}".format(shader.shaderType))
while "}" not in line:
if "DiffuseSampler" in line:
shader.diffuseSampler = line.strip().split(" ")[1]
elif "BumpSampler" in line:
samplerName = line.strip().split(" ")[1]
if samplerName != "*NULL*" and samplerName != "dummy_normal":
shader.bumpSampler = samplerName
elif "SpecSampler" in line:
samplerName = line.strip().split(" ")[1]
if samplerName != "*NULL*" and samplerName != "dummy_spec":
shader.specSampler = samplerName
elif "Bumpiness" in line:
shader.bumpiness = float(line.strip().split(" ")[1])
line = reader.readline()
return shader
class ODRData:
def __init__(self):
self.path = None
self.shaders = []
self.skeletonFilePath = None
self.meshPaths = []
def apply_data(self, overrideSkel = None, overrideSkelPath = None):
"""runs import procedures for the data contained in this ODRData object.
If overrideSkel data is provided, it will only be used if this ODRData doesn't have a skeletonFilePath set
or if its skeletonFilePath is the same as overrideSkelPath"""
print("applying data from ODR: {}".format(self.path))
importedSkel = None
importedGeoms = []
if self.skeletonFilePath is not None and self.skeletonFilePath != overrideSkelPath:
importedSkel = import_skel.import_skel_from_file(self.skeletonFilePath)
else:
importedSkel = overrideSkel
for meshPath in self.meshPaths:
importedGeoms.extend(import_mesh.import_mesh_from_file(meshPath))
if importedSkel is not None:
#meshes already have weights linked to bone indices;
#we just have to rename vertex groups to the right names
for importedMesh in importedGeoms:
rigging_utils.rig_geometry_to_skel(importedMesh, importedSkel)
class ODRShader:
def __init__(self):
self.shaderType = None
self.diffuseSampler = None
self.bumpSampler = None
self.specSampler = None
self.bumpiness = 1.0
from bpy_extras.io_utils import ImportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator
class ImportGta5ODR(Operator, ImportHelper):
"""Finds and imports all LOD meshes and skeleton declared in the ODR file"""
bl_idname = "io_gta5ped.import_odr"
bl_label = "Import GTA5 Ped .ODR File"
# ImportHelper mixin class uses this
filename_ext = ".odr"
filter_glob: StringProperty(
default="*.odr",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
def execute(self, context):
import_odr_from_file(self.filepath)
return {'FINISHED'}
# Only needed if you want to add into a dynamic menu
def menu_func_import(self, context):
self.layout.operator(ImportGta5ODR.bl_idname, text="Import GTA5 Ped .ODR File")
def register():
bpy.utils.register_class(ImportGta5ODR)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
def unregister():
bpy.utils.unregister_class(ImportGta5ODR)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)