Skip to content

Commit

Permalink
Depot Repath and Cross platform Path support, SVM fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Simarilius-uk committed Mar 14, 2024
1 parent 21594f7 commit 21411c8
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 54 deletions.
130 changes: 104 additions & 26 deletions i_scene_cp77_gltf/exporters/sectors_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,42 +370,62 @@ def exportSectors( filename):
wIMNs+=1
#print(wIMNs)
meshname = data['mesh']['DepotPath']['$value'].replace('\\', os.sep)

#if 'chopstick' in meshname:
# print('worldInstancedMeshNode - ',meshname)
if not checkexists(meshname, Masters):
print(meshname, ' not found in masters')
continue

num=data['worldTransformsBuffer']['numElements']
start=data['worldTransformsBuffer']['startIndex']
if(meshname != 0):
for idx in range(start, start+num):
bufferID=0
if 'Data' in data['worldTransformsBuffer']['sharedDataBuffer'].keys():
inst_trans=data['worldTransformsBuffer']['sharedDataBuffer']['Data']['buffer']['Data']['Transforms'][idx]

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'][idx]
# store the bufferID for when we add new stuff.
if Sector_additions_coll:
Sector_additions_coll['Inst_bufferID']=bufferID
obj_col=find_col(i,idx,Sector_coll)
if obj_col:
if obj_col and inst_trans:
if len(obj_col.objects)>0:
obj=obj_col.objects[0]
if obj.matrix_world!=Matrix(obj['matrix']):
# Check for Position and if changed delete the original and add to the new sector
if obj.matrix_world!=Matrix(obj_col['matrix']):
deletions[sectorName].append(obj_col)
new_ni=len(template_nodes)
template_nodes.append(copy.deepcopy(nodes[obj_col['nodeIndex']]))
# might need to convert instanced to static here, not sure what the best approach is.
createNodeData(template_nodeData, obj_col, new_ni, obj,ID)
ID+=1
else:
deletions[sectorName].append(obj_col)
obj_col['exported']=True
# Need to change deletions to pass the values otherwise cant deal with deleted collectors
if obj_col:
deletions[sectorName].append(obj_col)


case 'worldStaticDecalNode':
#print('worldStaticDecalNode')
instances = [x for x in t if x['NodeIndex'] == i]
for idx,inst in enumerate(instances):
instances = [(x,y) for y,x in enumerate(t) if x['NodeIndex'] == i]
for idx,(inst,instNid) in enumerate(instances):
obj=find_decal(i,idx,Sector_coll)
if obj:
# Check for Position and if changed delete the original and add to the new sector
if obj.matrix_world!=Matrix(obj['matrix']):
deletions['Decals'][sectorName].append({'nodeIndex':idx,'NodeComment' :obj.name, 'NodeType' : obj['nodeType']})
deletions['Decals'][sectorName].append({'nodeIndex':instNid,'NodeComment' :obj.name, 'NodeType' : obj['nodeType']})
new_ni=len(template_nodes)
template_nodes.append(copy.deepcopy(nodes[obj['nodeIndex']]))
createNodeData(template_nodeData, Sector_coll, new_ni, obj,ID)
ID+=1
obj['exported']=True
else:
deletions['Decals'][sectorName].append({'nodeIndex':idx,'NodeComment' :'DELETED Decal nid:'+str(inst['NodeIndex'])+' ndid:'+str(idx), 'NodeType' : 'worldStaticDecalNode'})
deletions['Decals'][sectorName].append({'nodeIndex':instNid,'NodeComment' :'DELETED Decal nid:'+str(inst['NodeIndex'])+' ndid:'+str(instNid), 'NodeType' : 'worldStaticDecalNode'})


case 'worldStaticMeshNode' | 'worldBuildingProxyMeshNode' | 'worldGenericProxyMeshNode'| 'worldTerrainProxyMeshNode':
if isinstance(e, dict) and 'mesh' in data.keys():
Expand All @@ -417,7 +437,6 @@ def exportSectors( filename):
obj_col=find_col(i,idx,Sector_coll)
#print(obj_col)
if obj_col:
obj_col['exported']=True
if len(obj_col.objects)>0:
obj=obj_col.objects[0]
# Check for Position and if changed delete the original and add to the new sector
Expand All @@ -429,7 +448,8 @@ def exportSectors( filename):
createNodeData(template_nodeData, obj_col, new_ni, obj,ID)
ID+=1
else:
deletions[sectorName].append(obj_col)
if obj_col:
deletions[sectorName].append(obj_col)

case 'worldEntityNode':
if isinstance(e, dict) and 'entityTemplate' in data.keys():
Expand All @@ -440,27 +460,87 @@ def exportSectors( filename):
for idx,inst in enumerate(instances):
obj_col=find_col(i,idx,Sector_coll)
#print(obj_col)
# THIS IS WRONG, the entity meshes are in child collectors not objects
if obj_col and len(obj_col.children)>0:
if len(obj_col.children[0].objects)>0:
# Check for Position and if changed delete the original and add to the new sector
# Find a better way to do this
if obj.matrix_world!=Matrix(obj_col['matrix']):
deletions[sectorName].append(obj_col)
new_ni=len(template_nodes)
template_nodes.append(copy.deepcopy(nodes[obj_col['nodeIndex']]))

createNodeData(template_nodeData, obj_col, new_ni, obj,ID)
ID+=1
obj_col['exported']=True

else:
if obj_col:
deletions[sectorName].append(obj_col)
obj_col['exported']=True
deletions[sectorName].append(obj_col)




case 'worldInstancedDestructibleMeshNode':
#print('worldInstancedDestructibleMeshNode',i)
if isinstance(e, dict) and 'mesh' in data.keys():
meshname = data['mesh']['DepotPath']['$value'].replace('\\', os.sep)
num=data['cookedInstanceTransforms']['numElements']
start=data['cookedInstanceTransforms']['startIndex']
instances = [x for x in t if x['NodeIndex'] == i]
for tlidx,inst in enumerate(instances):
for idx in range(start, start+num):
bufferID=0
basic_trans=None
# Transforms are inside the cookedInstanceTransforms in a buffer
if 'Data' in data['cookedInstanceTransforms']['sharedDataBuffer'].keys():
basic_trans=data['cookedInstanceTransforms']['sharedDataBuffer']['Data']['buffer']['Data']['Transforms'][idx]

# Transforms are in a shared buffer in another node, so get the reference and find the transform data
elif 'HandleRefId' in data['cookedInstanceTransforms']['sharedDataBuffer'].keys():
bufferID = int(data['cookedInstanceTransforms']['sharedDataBuffer']['HandleRefId'])
ref=e
for n in nodes:
if n['HandleId']==str(bufferID-1):
ref=n
basic_trans = ref['Data']['cookedInstanceTransforms']['sharedDataBuffer']['Data']['buffer']['Data']['Transforms'][idx]
#print(basic_trans)
else :
print(e)
# store the bufferID for when we add new stuff.
if Sector_additions_coll:
Sector_additions_coll['Dest_bufferID']=bufferID
#print('Setting Dest_bufferID to ',bufferID)

# the Transforms are stored as 2 parts, a basic transform applied to all the instances and individual ones per instance
# lets get the basic one so we can calculate the instance one.
basic_pos =Vector(get_pos(basic_trans))
basic_rot =Quaternion(get_rot(basic_trans))
basic_scale =Vector((1,1,1))
basic_matr=Matrix.LocRotScale(basic_pos,basic_rot,basic_scale)
basic_matr_inv=basic_matr.inverted()

# Never modify the basic on as other nodes may be referencing it. (its normally 0,0,0 anyway)
inst_pos =Vector(get_pos(inst))
inst_rot =Quaternion(get_rot(inst))
inst_scale =Vector((1,1,1))
inst_m=Matrix.LocRotScale(inst_pos,inst_rot,inst_scale)


obj_col=find_wIDMN_col(i,tlidx,idx,Sector_coll)
if obj_col:
if len(obj_col.objects)>0:
obj=obj_col.objects[0]
# Check for Position and if changed delete the original and add to the new sector
if obj.matrix_world!=Matrix(obj_col['matrix']):
deletions[sectorName].append(obj_col)
new_ni=len(template_nodes)
template_nodes.append(copy.deepcopy(nodes[obj_col['nodeIndex']]))

createNodeData(template_nodeData, obj_col, new_ni, obj,ID)
ID+=1

else:
if obj_col:
deletions[sectorName].append(obj_col)
print(wIMNs)

# __ __ __ __ ___ ___ ___
# /\ | \ | \ | |\ | / _` /__` | | | |__ |__
Expand All @@ -472,21 +552,19 @@ def exportSectors( filename):
for node in t:
if int(node['Id'])>ID:
ID=int(node['Id'])+1

for col in Sector_coll.children:
if 'exported' not in col.keys() or col['exported']==False:
if Sector_additions_coll:
for col in Sector_additions_coll.children:
if 'nodeIndex' in col.keys() and col['sectorName']==sectorName and len(col.objects)>0:
match col['nodeType']:
case 'worldStaticMeshNode' | 'worldStaticDecalNode' | 'worldBuildingProxyMeshNode' | 'worldGenericProxyMeshNode' | 'worldTerrainProxyMeshNode':
obj=col.objects[0]
new_ni=len(nodes)
createNodeData(template_nodeData, col, new_ni, obj,ID)
obj=col.objects[0]
createNodeData(t, col, col['nodeIndex'], obj,ID)
ID+=1
case 'worldEntityNode':
new_ni=len(nodes)
nodes.append(copy.deepcopy(nodes[col['nodeIndex']]))
obj=col.objects[0]
createNodeData(template_nodeData, col, new_ni, obj,ID)
createNodeData(t, col, new_ni, obj,ID)
ID+=1

case 'worldInstancedMeshNode':
Expand Down
19 changes: 9 additions & 10 deletions i_scene_cp77_gltf/importers/entity_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from mathutils import Vector, Matrix , Quaternion
import bmesh
from ..main.common import json_ver_validate
from ..main.common import jsonload
from .phys_import import cp77_phys_import
from ..main.collisions import draw_box_collider, draw_capsule_collider, draw_convex_collider, draw_sphere_collider

Expand All @@ -28,10 +29,10 @@ def importEnt( filepath='', appearances=[], exclude_meshes=[], with_materials=Tr
ent_name=os.path.basename(filepath)[:-9]
print('Importing Entity', ent_name)
with open(filepath,'r') as f:
j=json.load(f)
j=jsonload(f)
valid_json=json_ver_validate(j)
if not valid_json:
bpy.ops.cp77.message_box('INVOKE_DEFAULT', message="Incompatible entity json file detected. This add-on version requires files generated by WolvenKit 8.9.1 or higher.")
bpy.ops.cp77.message_box('INVOKE_DEFAULT', message="Incompatible entity json file detected. This add-on version requires files generated WolvenKit 8.9.1 or higher.")
return {'CANCELLED'}

ent_apps= j['Data']['RootChunk']['appearances']
Expand All @@ -50,7 +51,7 @@ def importEnt( filepath='', appearances=[], exclude_meshes=[], with_materials=Tr
for comp in ent_components:
ent_complist.append(comp['name'])
if 'rig' in comp.keys():
print(comp['rig'])
print(comp['rig']['DepotPath']['$value'])
ent_rigs.append(os.path.join(path,comp['rig']['DepotPath']['$value']))
if comp['name']['$value'] == 'Chassis':
chassis_info = comp
Expand Down Expand Up @@ -98,8 +99,8 @@ def importEnt( filepath='', appearances=[], exclude_meshes=[], with_materials=Tr
# then check for an anim in the project thats using the rig (some things like the arch bike dont ref the anim in the ent)
# otherwise just skip this section
#
anim_files = glob.glob(path+"\\base\\animations\\"+"\**\*.glb", recursive = True)
ep1_anim_files = glob.glob(path+"\\ep1\\animations\\"+"\**\*.glb", recursive = True)
anim_files = glob.glob(os.path.join(path,"base","animations","**","*.glb"), recursive = True)
ep1_anim_files = glob.glob(os.path.join(path,"ep1","animations","**","*.glb"), recursive = True)
anim_files = anim_files + ep1_anim_files
app_name=None
rig=None
Expand Down Expand Up @@ -141,7 +142,7 @@ def importEnt( filepath='', appearances=[], exclude_meshes=[], with_materials=Tr
print('no anim rig found')

# find the rig json associated with the ent
rigjsons = glob.glob(path+"\**\*.rig.json", recursive = True)
rigjsons = glob.glob(os.path.join(path,"**","*.rig.json"), recursive = True)
rig_j=None
if len(rigjsons)>0 and len(ent_rigs)>0:
entrigjsons=[x for x in rigjsons if x[:-5] in ent_rigs]
Expand Down Expand Up @@ -283,7 +284,7 @@ def importEnt( filepath='', appearances=[], exclude_meshes=[], with_materials=Tr
meshApp=c['meshAppearance']['$value']
#print(meshApp)
try:
bpy.ops.io_scene_gltf.cp77(filepath=meshpath, appearances=meshApp, with_materials=with_materials, update_gi=False,remap_depot=remapdepot)
bpy.ops.io_scene_gltf.cp77(filepath=meshpath, appearances=meshApp, with_materials=with_materials, update_gi=False, remap_depot=remapdepot)
for obj in C.selected_objects:
obj['componentName'] = c['name']['$value']
obj['sourcePath'] = meshpath
Expand All @@ -297,8 +298,7 @@ def importEnt( filepath='', appearances=[], exclude_meshes=[], with_materials=Tr
objs = C.selected_objects
if meshname=='v_sportbike2_arch_nemesis__ext01_axle_f_a_01':
print('those annoying front forks')
if 'rotor' in meshname or 'prop' in meshname:
print('those annoying front forks')

# NEW parentTransform stuff - fixes vehicles being exploded
x=None
y=None
Expand Down Expand Up @@ -486,7 +486,6 @@ def importEnt( filepath='', appearances=[], exclude_meshes=[], with_materials=Tr
cr.target=target
else:
target=obj


# end new stuff
# dont get the local transform here if we already did it before
Expand Down
Loading

0 comments on commit 21411c8

Please sign in to comment.