@ -18,10 +18,12 @@ if 0:
return l
else :
def import_fix_coord ( v ) :
return ( - v [ 0 ] , v [ 2 ] , - v [ 1 ] )
return ( - v [ 0 ] , - v [ 2 ] , v [ 1 ] )
def import_fix_normal ( v ) :
return ( v [ 0 ] , - v [ 2 ] , v [ 1 ] )
return ( v [ 0 ] , v [ 2 ] , - v [ 1 ] )
def import_fix_winding ( l ) :
l = list ( l )
l . reverse ( )
return l
def convert_model ( geo_model , mesh_data , obj , scale ) :
@ -37,6 +39,7 @@ def convert_model(geo_model, mesh_data, obj, scale):
coords = [ c for v in geomesh . geovertex for c in import_scale_coord ( import_fix_coord ( v . coord ) , scale ) ]
normals = [ n for v in geomesh . geovertex for n in import_fix_normal ( v . normal ) ]
#print("normals: %s" % (normals, ))
loop_totals = [ ]
loop_starts = [ ]
i = 0
@ -78,17 +81,53 @@ def convert_model(geo_model, mesh_data, obj, scale):
#todo: attempt to load textures/images
def getBonePositionBody ( bone ) :
if bone . parent is None :
p = bone . tail
print ( " root: %s : %s ( %s ) " % ( bone , p , bone . head ) )
return p
else :
p = bone . tail + getBonePositionBody ( bone . parent )
print ( " : %s : %s ( %s ) -> %s " % ( bone , bone . tail , bone . head , p ) )
return p
def load ( operator , context , scale = 1.0 , filepath = " " , global_matrix = None , use_mesh_modifiers = True ) :
def getBonePosition ( arm , name ) :
return getBonePositionBody ( arm . bones [ name ] )
def load ( operator , context , scale = 1.0 , filepath = " " , global_matrix = None , use_mesh_modifiers = True , ignore_lod = True ) :
#load .geo
fh_in = open ( filepath , " rb " )
geo = Geo ( )
geo . loadFromFile ( fh_in )
fh_in . close ( )
#Choose the first selected armature as the armature to attach to.
armature = None
for ob in context . selected_objects :
if ob . type == " ARMATURE " :
armature_obj = ob
armature = ob . data
break
else :
#If none found try again with all objects.
for ob in bpy . data . objects :
if ob . type == " ARMATURE " :
armature_obj = ob
armature = ob . data
break
for geo_model in geo . models :
model_name = geo_model . name . decode ( " utf-8 " )
if ignore_lod :
islod = False
#todo: better LOD model detection
for l in [ " _LOD1 " , " _LOD2 " ] :
if l in model_name :
islod = True
break
if islod :
continue
#create object matching model's name (or next equivilant)
mesh = bpy . data . meshes . new ( name = geo_model . name . decode ( " utf-8 " ) )
obj = bpy . data . objects . new ( geo_model . name . decode ( " utf-8 " ) , mesh )
mesh = bpy . data . meshes . new ( name = model_name )
obj = bpy . data . objects . new ( model_name , mesh )
#convert model to mesh
convert_model ( geo_model , mesh , obj , scale )
#Create object for this mesh
@ -96,6 +135,20 @@ def load(operator, context, scale = 1.0, filepath = "", global_matrix = None, us
bpy . context . collection . objects . link ( obj )
bpy . context . view_layer . objects . active = obj
#Try and attach it to the armature.
if armature != None :
bone_name = geo_model . getBoneRoot ( )
if bone_name is None :
#No bones, don't attach.
pass
elif bone_name in armature . bones :
bone_pos = getBonePosition ( armature , bone_name )
obj . matrix_world = mathutils . Matrix . Translation ( bone_pos )
obj . modifiers . new ( name = ' Armature ' , type = ' ARMATURE ' )
obj . modifiers [ ' Armature ' ] . object = armature_obj
else :
print ( " Bone ' %s ' not found in armature ' %s ' , skipping. " % ( bone_name , armature ) )
obj . select_set ( True )
pass
pass