-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnt_Import1.py
128 lines (120 loc) · 6.83 KB
/
Ent_Import1.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
# Blender Entity import script by Simarilius
import json
import glob
import os
import bpy
C = bpy.context
coll_scene = C.scene.collection
path = 'F:\\CPmod\\tygers\\source\\raw'
ent_name = 'gang__tyger_ma.ent'
# The list below needs to be the appearanceNames for each ent that you want to import
# NOT the name in appearances list, expand it and its the property inside, also its name in the app file
appearances =['biker__lvl1_05','gangster__lvl3_04']
jsonpath = glob.glob(path+"\**\*.ent.json", recursive = True)
if len(jsonpath)==0:
print('No jsons found')
for i,e in enumerate(jsonpath):
if os.path.basename(e)== ent_name+'.json' :
filepath=e
with open(filepath,'r') as f:
j=json.load(f)
ent_apps= j['Data']['RootChunk']['appearances']
# if you've already imported the body/head and set the rig up you can exclude them by putting them in this list
exclude_meshes= ['h0_012_ma_a__young.mesh' ,'t0_000_ma_base__full.mesh']
app_path = glob.glob(path+"\**\*.app.json", recursive = True)
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]
if len(meshnames)==0:
print('No Meshes found')
if len(meshnames)<1 or len(jsonpath)<1 or len(app_path)<1:
print("You need to export the meshes and convert app and ent to json")
else:
for x,app_name in enumerate(appearances):
ent_coll=bpy.data.collections.new(ent_name+'_'+app_name)
ent_coll['appearanceName']=app_name
ent_coll['depotPath']=ent_name
coll_scene.children.link(ent_coll)
comps=[]
if len(ent_apps)>0:
app_idx=0
for i,a in enumerate(ent_apps):
#print(i,a['appearanceName'])
if a['appearanceName']==app_name:
print('appearance matched, id = ',i)
app_idx=i
app_file = ent_apps[app_idx]['appearanceResource']['DepotPath']
for i,e in enumerate(app_path):
#print(os.path.basename(e))
if os.path.basename(e)== os.path.basename(app_file)+'.json' :
filepath=e
if len(filepath)>0:
with open(filepath,'r') as a:
a_j=json.load(a)
app_idx=0
apps=a_j['Data']['RootChunk']['appearances']
for i,a in enumerate(apps):
print(i,a['Data']['name'])
if a['Data']['name']==app_name:
print('appearance matched, id = ',i)
app_idx=i
if 'Data' in a_j['Data']['RootChunk']['appearances'][app_idx].keys():
comps= a_j['Data']['RootChunk']['appearances'][app_idx]['Data']['components']
if len(comps)<1:
print('falling back to rootchunck comps')
comps= j['Data']['RootChunk']['components']
for c in comps:
if 'mesh' in c.keys():
#print(c['mesh']['DepotPath'])
app='default'
meshname=os.path.basename(c['mesh']['DepotPath'])
if meshname not in exclude_meshes:
if isinstance( c['mesh']['DepotPath'], str):
if os.path.exists(os.path.join(path, c['mesh']['DepotPath'][:-4]+'glb')):
try:
bpy.ops.io_scene_gltf.cp77(filepath=os.path.join(path, c['mesh']['DepotPath'][:-4]+'glb'))
objs = C.selected_objects
x=c['localTransform']['Position']['x']['Bits']/131072
y=c['localTransform']['Position']['y']['Bits']/131072
z=c['localTransform']['Position']['z']['Bits']/131072
meshApp='default'
if 'meshAppearance' in c.keys():
meshApp=c['meshAppearance']
#print(meshApp)
for obj in objs:
#print(obj.name, obj.type)
if obj.type=='MESH':
'''
# Attempt at picking materials
materialName='bob'
print('materialName',materialName,obj.users_collection[0].name)
materialName = 'ml_'+obj.users_collection[0].name+'_'+meshApp
print('B ',materialName, obj.data.materials.keys())
if materialName in obj.data.materials.keys():
mat = bpy.data.materials.get(materialName)
obj.active_material = mat
print('C')
elif len(meshApp)>0 and meshApp in obj.data.materials.keys():
mat = bpy.data.materials.get(meshApp)
obj.active_material = mat
print('D')
'''
obj.location.x = x
obj.location.y = y
obj.location.z = z
obj.rotation_quaternion.x = c['localTransform']['Orientation']['i']
obj.rotation_quaternion.y = c['localTransform']['Orientation']['j']
obj.rotation_quaternion.z = c['localTransform']['Orientation']['k']
obj.rotation_quaternion.w = c['localTransform']['Orientation']['r']
if 'scale' in c['localTransform'].keys():
obj.scale.x = c['localTransform']['scale']['X']
obj.scale.y = c['localTransform']['scale']['Y']
obj.scale.z = c['localTransform']['scale']['Z']
move_coll= coll_scene.children.get( objs[0].users_collection[0].name )
move_coll['depotPath']=c['mesh']['DepotPath']
move_coll['meshAppearance']=meshApp
ent_coll.children.link(move_coll)
coll_scene.children.unlink(move_coll)
except:
print("Failed on ",c['mesh']['DepotPath'])
print('Exported' ,app_name)