-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjust_the_meshes.py
245 lines (220 loc) · 16.9 KB
/
just_the_meshes.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
# Script to import CP2077 streaming sectors to Blender
# Recommended for use without materials
# see https://wiki.redmodding.org/wolvenkit/guides/modding-community/exporting-streaming-sectors-to-blender
# By Simarilius Oct 2022
import json
import glob
import os
import bpy
path = 'F:\\CPmod\\coyote\\source\\raw'
withMaterials=False
meshes = glob.glob(path+"\**\*.glb", recursive = True)
glbnames = [ os.path.basename(x) for x in meshes]
meshnames = [ os.path.splitext(x)[0]+".mesh" for x in glbnames]
jsonpath = glob.glob(path+"\**\*.streamingsector.json", recursive = True)
for filepath in jsonpath:
with open(filepath,'r') as f:
j=json.load(f)
t=j['Data']['RootChunk']['nodeData']['Data']
nodes = j["Data"]["RootChunk"]["nodes"]
#print(len(nodes))
for i,e in enumerate(nodes):
#if i > 2: break
data = e['Data']
type = data['$type']
match type:
case 'worldEntityNode':
print('worldEntityNode',i)
pass
case 'worldInstancedMeshNode':
#print('worldInstancedMeshNode')
meshname = data['mesh']['DepotPath']
num=data['worldTransformsBuffer']['numElements']
start=data['worldTransformsBuffer']['startIndex']
if(meshname != 0):
#print('Mesh - ',meshname, ' - ',i, e['HandleId'])
glbfoundname = [ x for x in meshes if os.path.splitext(str(meshname))[0] in x]
if(len(glbfoundname) == 1):
#print('Glb found - ',glbfoundname)
for i in range(start, start+num):
if withMaterials:
try:
bpy.ops.io_scene_gltf.cp77(filepath=glbfoundname[0])
except:
bpy.ops.import_scene.gltf(filepath=glbfoundname[0])
else:
bpy.ops.import_scene.gltf(filepath=glbfoundname[0])
objects = bpy.context.selected_objects
for obj in objects:
nn= str(data['debugName']).replace('{','').replace('}','')
nn = [nn[1:],nn][nn[0].isalnum()]
#print(nn)
obj.name = nn
#print(inst['Position'].keys())
if 'Data' in data['worldTransformsBuffer']['sharedDataBuffer'].keys():
inst_trans=data['worldTransformsBuffer']['sharedDataBuffer']['Data']['buffer']['Data']['Transforms'][i]
elif 'HandleRefId' in data['worldTransformsBuffer']['sharedDataBuffer'].keys():
bufferID = int(data['worldTransformsBuffer']['sharedDataBuffer']['HandleRefId'])
ref=e
for n in nodes:
if n['HandleId']==str(bufferID-1):
ref=n
inst_trans = ref['Data']['worldTransformsBuffer']['sharedDataBuffer']['Data']['buffer']['Data']['Transforms'][i]
else :
print(e)
obj.location.x = inst_trans['translation']['X'] /100
obj.location.y = inst_trans['translation']['Y'] /100
obj.location.z = inst_trans['translation']['Z'] /100
if obj.location.x == 0:
print('Mesh - ',meshname, ' - ',i,'HandleId - ', e['HandleId'])
#print(i,obj.name,' x= ',obj.location.x, ' y= ', obj.location.y, ' z= ',obj.location.z)
obj.rotation_quaternion.x = inst_trans['rotation']['i']
obj.rotation_quaternion.y = inst_trans['rotation']['j']
obj.rotation_quaternion.z = inst_trans['rotation']['k']
obj.rotation_quaternion.w = inst_trans['rotation']['r']
obj.scale.x = inst_trans['scale']['X'] /100
obj.scale.y = inst_trans['scale']['Y'] /100
obj.scale.z = inst_trans['scale']['Z'] /100
else:
print('Mesh not found - ',meshname, ' - ',i, e['HandleId'])
case 'worldDecorationMeshNode':
#print('worldDecorationMeshNode',i)
pass
case 'worldInstancedOccluderNode':
#print('worldInstancedOccluderNode')
pass
case 'worldStaticDecalNode':
#print('worldStaticDecalNode')
pass
case 'worldStaticOccluderMeshNode':
#print('worldStaticOccluderMeshNode',i)
pass
case 'worldCollisionNode':
#print('worldCollisionNode',1)
pass
case 'worldStaticMeshNode':
if isinstance(e, dict) and 'mesh' in data.keys():
meshname = data['mesh']['DepotPath']
#print('Mesh name is - ',meshname, e['HandleId'])
if(meshname != 0):
#print('Mesh - ',meshname, ' - ',i, e['HandleId'])
glbfoundname = [ x for x in meshes if os.path.splitext(str(meshname))[0] in x]
if(len(glbfoundname) == 1):
#print('Glb found - ',glbfoundname)
#print('Glb found, looking for instances of ',i)
instances = [x for x in t if x['NodeIndex'] == i]
for inst in instances:
#print('Inst - ',i, ' - ',meshname)
if withMaterials:
try:
bpy.ops.io_scene_gltf.cp77(filepath=glbfoundname[0])
except:
bpy.ops.import_scene.gltf(filepath=glbfoundname[0])
else:
bpy.ops.import_scene.gltf(filepath=glbfoundname[0])
objects = bpy.context.selected_objects
#obj = bpy.context.object
#print(data.keys())
for obj in objects:
nn= str(data['debugName']).replace('{','').replace('}','')
nn = [nn[1:],nn][nn[0].isalnum()]
#print(nn)
obj.name = nn
#print(inst['Position'].keys())
if 'Properties' in inst['Position'].keys():
obj.location.x = inst['Position']['Properties']['X'] /100
obj.location.y = inst['Position']['Properties']['Y'] /100
obj.location.z = inst['Position']['Properties']['Z'] /100
else:
obj.location.x = inst['Position']['X'] /100
obj.location.y = inst['Position']['Y'] /100
obj.location.z = inst['Position']['Z'] /100
if obj.location.x == 0:
print('Mesh - ',meshname, ' - ',i,'HandleId - ', e['HandleId'])
#print(i,obj.name,' x= ',obj.location.x, ' y= ', obj.location.y, ' z= ',obj.location.z)
if 'Properties' in inst['Orientation'].keys():
obj.rotation_quaternion.x = inst['Orientation']['Properties']['i']
obj.rotation_quaternion.y = inst['Orientation']['Properties']['j']
obj.rotation_quaternion.z = inst['Orientation']['Properties']['k']
obj.rotation_quaternion.w = inst['Orientation']['Properties']['r']
else:
obj.rotation_quaternion.x = inst['Orientation']['i']
obj.rotation_quaternion.y = inst['Orientation']['j']
obj.rotation_quaternion.z = inst['Orientation']['k']
obj.rotation_quaternion.w = inst['Orientation']['r']
if 'Properties' in inst['Scale'].keys():
obj.scale.x = inst['Scale']['Properties']['X'] /100
obj.scale.y = inst['Scale']['Properties']['Y'] /100
obj.scale.z = inst['Scale']['Properties']['Z'] /100
else:
obj.scale.x = inst['Scale']['X'] /100
obj.scale.y = inst['Scale']['Y'] /100
obj.scale.z = inst['Scale']['Z'] /100
else:
print('Mesh not found - ',meshname, ' - ',i, e['HandleId'])
case 'worldInstancedDestructibleMeshNode':
#print('worldInstancedDestructibleMeshNode',i)
if isinstance(e, dict) and 'mesh' in data.keys():
meshname = data['mesh']['DepotPath']
#print('Mesh name is - ',meshname, e['HandleId'])
if(meshname != 0):
#print('Mesh - ',meshname, ' - ',i, e['HandleId'])
glbfoundname = [ x for x in meshes if os.path.splitext(str(meshname))[0] in x]
if(len(glbfoundname) == 1):
#print('Glb found - ',glbfoundname)
#print('Glb found, looking for instances of ',i)
instances = [x for x in t if x['NodeIndex'] == i]
for inst in instances:
#print('Inst - ',i, ' - ',meshname)
if withMaterials:
try:
bpy.ops.io_scene_gltf.cp77(filepath=glbfoundname[0])
except:
bpy.ops.import_scene.gltf(filepath=glbfoundname[0])
else:
bpy.ops.import_scene.gltf(filepath=glbfoundname[0])
objects = bpy.context.selected_objects
#obj = bpy.context.object
#print(data.keys())
for obj in objects:
nn= str(data['debugName']).replace('{','').replace('}','')
nn = [nn[1:],nn][nn[0].isalnum()]
#print(nn)
obj.name = nn
#print(inst['Position'].keys())
if 'Properties' in inst['Position'].keys():
obj.location.x = inst['Position']['Properties']['X'] /100
obj.location.y = inst['Position']['Properties']['Y'] /100
obj.location.z = inst['Position']['Properties']['Z'] /100
else:
obj.location.x = inst['Position']['X'] /100
obj.location.y = inst['Position']['Y'] /100
obj.location.z = inst['Position']['Z'] /100
if obj.location.x == 0:
print('Mesh - ',meshname, ' - ',i,'HandleId - ', e['HandleId'])
#print(i,obj.name,' x= ',obj.location.x, ' y= ', obj.location.y, ' z= ',obj.location.z)
if 'Properties' in inst['Orientation'].keys():
obj.rotation_quaternion.x = inst['Orientation']['Properties']['i']
obj.rotation_quaternion.y = inst['Orientation']['Properties']['j']
obj.rotation_quaternion.z = inst['Orientation']['Properties']['k']
obj.rotation_quaternion.w = inst['Orientation']['Properties']['r']
else:
obj.rotation_quaternion.x = inst['Orientation']['i']
obj.rotation_quaternion.y = inst['Orientation']['j']
obj.rotation_quaternion.z = inst['Orientation']['k']
obj.rotation_quaternion.w = inst['Orientation']['r']
if 'Properties' in inst['Scale'].keys():
obj.scale.x = inst['Scale']['Properties']['X'] /100
obj.scale.y = inst['Scale']['Properties']['Y'] /100
obj.scale.z = inst['Scale']['Properties']['Z'] /100
else:
obj.scale.x = inst['Scale']['X'] /100
obj.scale.y = inst['Scale']['Y'] /100
obj.scale.z = inst['Scale']['Z'] /100
else:
print('Mesh not found - ',meshname, ' - ',i, e['HandleId'])
case _:
print('None of the above',i)
pass
print("Done with ",filepath)
print('Finished')