-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInstance_meshes3.py
397 lines (354 loc) · 24.5 KB
/
Instance_meshes3.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import json
import glob
import os
import bpy
from mathutils import Vector, Matrix
import time
start_time = time.time()
C = bpy.context
coll_scene = C.scene.collection
path = 'F:\\CPmod\\coyote\\source\\raw'
def get_pos(inst):
pos=[0,0,0]
if 'Properties' in inst['Position'].keys():
pos[0] = inst['Position']['Properties']['X'] /100
pos[1] = inst['Position']['Properties']['Y'] /100
pos[2] = inst['Position']['Properties']['Z'] /100
else:
pos[0] = inst['Position']['X'] /100
pos[1] = inst['Position']['Y'] /100
pos[2] = inst['Position']['Z'] /100
return pos
def get_rot(inst):
rot=[0,0,0,0]
if 'Properties' in inst['Orientation'].keys():
rot[0] = inst['Orientation']['Properties']['r']
rot[1] = inst['Orientation']['Properties']['i']
rot[2] = inst['Orientation']['Properties']['j']
rot[3] = inst['Orientation']['Properties']['k']
else:
rot[0] = inst['Orientation']['r']
rot[1] = inst['Orientation']['i']
rot[2] = inst['Orientation']['j']
rot[3] = inst['Orientation']['k']
return rot
def get_scale(inst):
scale=[0,0,0]
if 'Properties' in inst['Scale'].keys():
scale[0] = inst['Scale']['Properties']['X'] /100
scale[1] = inst['Scale']['Properties']['Y'] /100
scale[2] = inst['Scale']['Properties']['Z'] /100
else:
scale[0] = inst['Scale']['X'] /100
scale[1] = inst['Scale']['Y'] /100
scale[2] = inst['Scale']['Z'] /100
return scale
if "MasterInstances" not in coll_scene.children.keys():
Masters=bpy.data.collections.new("MasterInstances")
coll_scene.children.link(Masters)
else:
Masters=bpy.data.collections.get("MasterInstances")
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']
sectorName=os.path.basename(filepath)[:-5]
if sectorName in coll_scene.children.keys():
Sector_coll=bpy.data.collections.get(sectorName)
else:
Sector_coll=bpy.data.collections.new(sectorName)
coll_scene.children.link(Sector_coll)
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]
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)
app=e['Data']['appearanceName']
entpath=os.path.join(path,e['Data']['entityTemplate']['DepotPath'])+'.json'
ent_groupname=os.path.basename(entpath).split('.')[0]+'_'+app
if ent_groupname in Masters.children.keys():
move_coll=Masters.children.get(ent_groupname)
else:
try:
bpy.ops.io_scene_gltf.cp77entity(filepath=entpath, appearances=app)
objs = C.selected_objects
move_coll= coll_scene.children.get( os.path.basename(entpath).split('.')[0]+'_'+app )
Masters.children.link(move_coll)
coll_scene.children.unlink(move_coll)
except:
print('failed on ',os.path.basename(entpath))
instances = [x for x in t if x['NodeIndex'] == i]
for inst in instances:
print(inst)
group=move_coll
groupname=move_coll.name
if (group):
print('Group found for ',groupname)
new=bpy.data.collections.new(groupname)
Sector_coll.children.link(new)
new['nodeType']=type
new['nodeIndex']=i
new['debugName']=e['Data']['debugName']
new['sectorName']=sectorName
new['HandleId']=e['HandleId']
new['entityTemplate']=os.path.basename(e['Data']['entityTemplate']['DepotPath'])
new['appearanceName']=e['Data']['appearanceName']
new['pivot']=inst['Pivot']
for old_obj in group.all_objects:
obj=old_obj.copy()
new.objects.link(obj)
curse=bpy.context.scene.cursor.location
with bpy.context.temp_override(selected_editable_objects=obj):
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
obj.location = get_pos(inst)
obj.rotation_quaternion = get_rot(inst)
obj.scale = get_scale(inst)
bpy.context.scene.cursor.location=curse
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'])
groupname = os.path.splitext(os.path.split(meshname)[-1])[0]
group=Masters.children.get(groupname)
if (group):
print('Group found for ',groupname)
for i in range(start, start+num):
#create the linked copy of the group of mesh
new=bpy.data.collections.new(groupname)
Sector_coll.children.link(new)
new['nodeType']=type
new['nodeIndex']=i
new['mesh']=meshname
new['debugName']=e['Data']['debugName']
new['sectorName']=sectorName
for old_obj in group.all_objects:
obj=old_obj.copy()
new.objects.link(obj)
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 'XworldDecorationMeshNode':
#print('worldDecorationMeshNode',i)
pass
case 'XworldInstancedOccluderNode':
#print('worldInstancedOccluderNode')
pass
case 'worldStaticDecalNode':
print('worldStaticDecalNode')
instances = [x for x in t if x['NodeIndex'] == i]
for inst in instances:
print( inst)
o = bpy.data.objects.new( "empty", None )
o['nodeType']='worldStaticDecalNode'
o['nodeIndex']=i
o['decal']=e['Data']['material']['DepotPath']
o['debugName']=e['Data']['debugName']
o['sectorName']=sectorName
Sector_coll.objects.link(o)
o.location = get_pos(inst)
o.rotation_quaternion = get_rot(inst)
o.scale = get_scale(inst)
o.empty_display_size = 0.002
o.empty_display_type = 'IMAGE'
case 'XworldStaticOccluderMeshNode':
#print('worldStaticOccluderMeshNode',i)
pass
case 'XworldCollisionNode':
#print('worldCollisionNode',1)
pass
case 'worldStaticMeshNode' | 'worldBuildingProxyMeshNode' | 'worldGenericProxyMeshNode':
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'])
groupname = os.path.splitext(os.path.split(meshname)[-1])[0]
group=Masters.children.get(groupname)
if (group):
print('Group found for ',groupname)
instances = [x for x in t if x['NodeIndex'] == i]
for inst in instances:
new=bpy.data.collections.new(groupname)
Sector_coll.children.link(new)
new['nodeType']=type
new['nodeIndex']=i
new['mesh']=meshname
new['debugName']=e['Data']['debugName']
new['sectorName']=sectorName
new['pivot']=inst['Pivot']
for old_obj in group.all_objects:
obj=old_obj.copy()
new.objects.link(obj)
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'])
groupname = os.path.splitext(os.path.split(meshname)[-1])[0]
group=Masters.children.get(groupname)
if (group):
#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)
new=bpy.data.collections.new(groupname)
Sector_coll.children.link(new)
new['nodeType']=type
new['nodeIndex']=i
new['mesh']=meshname
new['debugName']=e['Data']['debugName']
new['sectorName']=sectorName
new['pivot']=inst['Pivot']
for old_obj in group.all_objects:
obj=old_obj.copy()
new.objects.link(obj)
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 'worldRoadProxyMeshNode' :
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'])
#roads all have stupid prx0 names so instancing by name wont work.
try:
bpy.ops.io_scene_gltf.cp77(filepath=meshname, with_materials=withMaterials)
objs = C.selected_objects
groupname = objs[0].users_collection[0].name
group= coll_scene.children.get( groupname )
coll_target.children.link(group)
coll_scene.children.unlink(group)
coll_target['glb_file']=meshname
except:
print("Failed on ",meshname)
if (group):
print('Group found for ',groupname)
instances = [x for x in t if x['NodeIndex'] == i]
for inst in instances:
new=bpy.data.collections.new(groupname)
Sector_coll.children.link(new)
new['nodeType']=type
new['nodeIndex']=i
new['mesh']=meshname
new['debugName']=e['Data']['debugName']
new['sectorName']=sectorName
new['pivot']=inst['Pivot']
for old_obj in group.all_objects:
obj=old_obj.copy()
new.objects.link(obj)
obj.location = get_pos(inst)
if obj.location.x == 0:
print('Mesh - ',meshname, ' - ',i,'HandleId - ', e['HandleId'])
curse=bpy.context.scene.cursor.location
bpy.context.scene.cursor.location=Vector((inst['Pivot']['X'] /100,inst['Pivot']['Y'] /100,inst['Pivot']['Z'] /100))
with bpy.context.temp_override(selected_editable_objects=obj):
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
#print(i,obj.name,' x= ',obj.location.x, ' y= ', obj.location.y, ' z= ',obj.location.z)
obj.rotation_quaternion = get_rot(inst)
obj.scale = get_scale(inst)
bpy.context.scene.cursor.location=curse
else:
print('Mesh not found - ',meshname, ' - ',i, e['HandleId'])
case _:
print('None of the above',i)
pass
print('Finished with ',filepath)
print('Finished')
print("--- %s seconds ---" % (time.time() - start_time))