-
Notifications
You must be signed in to change notification settings - Fork 1
/
car.py
246 lines (163 loc) · 8.2 KB
/
car.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
import os
import panda3d.core
import library.io
class Item:
def __init__(self, tag: str, name: str, model) -> None:
self.tag = tag
self.name = name
self.model = model
def unload_model(self) -> None:
if self.model:
self.model.removeNode()
self.model = None
class Car:
def __init__(self, main) -> None:
self.main = main
self.path = None
self.json = None
self.items = None
self.nodepath = None
self.load(tag=self.main.config_json["defaults"]["car"])
def is_default(self, tag: str) -> bool:
return tag in self.json["defaults"]
def is_optional(self, tag: str) -> bool:
return self.get_part_type(tag=tag) in self.json["optional"]
def is_wheel(self, tag: str) -> bool:
return "brake" not in tag and (tag.startswith("wheel") or tag not in self.json["names"])
@staticmethod
def is_brake(tag: str) -> bool:
return "brake" in tag
def get_first_wheel(self) -> Item:
return self.items["wheels"][list(self.items["wheels"])[0]][0]
def get_first_brake(self, tag) -> Item:
part_type = self.get_part_type(tag=tag)
return self.items[part_type][0]
@staticmethod
def get_part_type(tag: str) -> str:
return tag.split("_")[0]
def get_items_of_same_part_type(self, tag: str) -> list:
found_items = []
part_type = self.get_part_type(tag=tag)
for i in self.items:
if not self.is_wheel(tag=i) and not self.is_brake(tag=i):
if self.items[i].tag.startswith(part_type) and self.items[i].model:
found_items.append(self.items[i].tag)
return found_items
def check_car_config_sanity(self, tag: str) -> None:
car_parts_files = library.io.get_file_path(path=self.path, extension="glb", number=0)
car_parts_files = [part.split(".")[0] for part in car_parts_files]
car_parts_json = list(self.json["names"])
assert sorted(car_parts_files) == sorted(car_parts_json), \
f"mismatch between content in car folder \"{tag}\" and \"{self.main.PATH_ITEMS_CONFIG_JSON}\" file"
def load(self, tag: str) -> None:
if self.items:
self.unload()
self.path = os.path.join(self.main.PATH_CARS, tag)
self.json = library.io.get_json(path=os.path.join(self.path, self.main.PATH_ITEMS_CONFIG_JSON))
self.nodepath = panda3d.core.NodePath("car")
self.nodepath.reparentTo(self.main.render)
self.items = {}
self.check_car_config_sanity(tag=tag)
for item_tag in self.json["names"]:
if self.is_default(tag=item_tag):
if self.is_wheel(tag=item_tag):
self.load_wheels(tag=item_tag, oem=True, no_cache=True)
elif self.is_brake(tag=item_tag):
self.load_brakes(tag=item_tag, no_cache=True)
else:
self.load_part(tag=item_tag, no_cache=True)
elif not self.is_wheel(tag=item_tag) and not self.is_brake(tag=item_tag):
self.items[item_tag] = Item(tag=item_tag, name=self.json["names"][item_tag], model=None)
def load_part(self, tag: str, no_cache: bool = False) -> None:
if self.is_wheel(tag=tag):
return
elif tag not in self.json["names"]:
return
elif self.get_items_of_same_part_type(tag=tag) == [tag] and self.is_optional(tag=tag):
self.unload_part(tag=tag)
return
for part in self.get_items_of_same_part_type(tag=tag):
self.items[part].unload_model()
self.items[tag] = Item(tag=tag,
name=self.json["names"][tag],
model=self.main.loader.loadModel(modelPath=os.path.join(self.path, tag + ".glb"),
noCache=no_cache))
self.items[tag].model.setPos(tuple(self.json["chassis"]["position"]))
self.items[tag].model.setHpr(tuple(self.json["chassis"]["rotation"]))
self.items[tag].model.setScale(tuple(self.json["chassis"]["scale"]))
self.items[tag].model.reparentTo(self.nodepath)
def load_wheels(self, tag: str, oem: bool, no_cache: bool = False) -> None:
if oem:
wheels_path = os.path.join(self.path, tag + ".glb")
wheels_name = self.json["names"][tag]
wheels_parameters = self.json["wheels"]
else:
wheels_path = os.path.join(self.main.PATH_WHEELS, tag, tag + ".glb")
wheels_name = library.io.get_json(path=os.path.join(self.main.PATH_WHEELS,
tag, self.main.PATH_ITEMS_CONFIG_JSON))["name"]
wheels_parameters = {axle: [{"position": list(wheel.model.getPos()),
"rotation": list(wheel.model.getHpr()),
"scale": list(wheel.model.getScale())}
for wheel in self.items["wheels"][axle]]
for axle in self.items["wheels"]}
if "wheels" in self.items:
self.unload_wheels()
self.items["wheels"] = {}
for axle in wheels_parameters:
self.items["wheels"][axle] = []
for i, wheel in enumerate(wheels_parameters[axle]):
wheel_item = Item(tag=tag,
name=wheels_name,
model=self.main.loader.loadModel(modelPath=wheels_path,
noCache=no_cache))
wheel_item.model.setPos(tuple(wheels_parameters[axle][i]["position"]))
wheel_item.model.setHpr(tuple(wheels_parameters[axle][i]["rotation"]))
wheel_item.model.setScale(tuple(wheels_parameters[axle][i]["scale"]))
wheel_item.model.reparentTo(self.main.render)
self.items["wheels"][axle].append(wheel_item)
def load_brakes(self, tag: str, no_cache: bool = False) -> None:
part_type = self.get_part_type(tag=tag)
axle = part_type.replace("brakes", "")
if tag in self.items:
self.unload_brakes(tag=tag)
else:
self.items[part_type] = []
for i, wheel in enumerate(self.json["wheels"][axle]):
brake_item = Item(tag=tag,
name=self.json["names"][tag],
model=self.main.loader.loadModel(modelPath=os.path.join(self.path, tag + ".glb"),
noCache=no_cache))
brake_item.model.setPos(tuple(self.items["wheels"][axle][i].model.getPos()))
brake_item.model.setScale(tuple(self.items["wheels"][axle][i].model.getScale()))
brake_item.model.reparentTo(self.main.render)
self.items[part_type].append(brake_item)
def load_bodykit(self, bodykit: str) -> None:
for item in list(self.items):
if item not in ["chassis", "wheels", "frontbrakes", "rearbrakes"]:
self.unload_part(tag=item)
bodykit_partlist = [kit["parts"] for kit in self.json["bodykits"] if kit["name"] == bodykit][0]
for part in bodykit_partlist:
self.load_part(tag=part, no_cache=True)
def unload(self) -> None:
for item in list(self.items):
if isinstance(self.items[item], dict):
self.unload_wheels()
elif isinstance(self.items[item], list):
self.unload_brakes(tag=item)
else:
self.unload_part(tag=item)
self.path = None
self.json = None
self.items = None
def unload_part(self, tag: str) -> None:
self.items[tag].unload_model()
def unload_wheels(self) -> None:
for axle in self.items["wheels"]:
for wheel in self.items["wheels"][axle]:
wheel.unload_model()
self.items["wheels"] = {}
def unload_brakes(self, tag: str) -> None:
part_type = self.get_part_type(tag=tag)
for brake in self.items[part_type]:
brake.unload_model()
self.items[part_type] = []