@ -1,13 +1,81 @@
from . geo import Geo
from . geomesh import *
from . bones import *
import bpy . path
import bpy
import mathutils
def import_scale_coord ( v , scale ) :
return ( v [ 0 ] * scale , v [ 1 ] * scale , v [ 2 ] * scale )
if 0 :
def import_fix_coord ( v ) :
return ( v [ 0 ] , v [ 2 ] , - v [ 1 ] )
def import_fix_normal ( v ) :
return ( - v [ 0 ] , - v [ 2 ] , v [ 1 ] )
def import_fix_winding ( l ) :
l = list ( l )
l . reverse ( )
return l
else :
def import_fix_coord ( v ) :
return ( - v [ 0 ] , v [ 2 ] , - v [ 1 ] )
def import_fix_normal ( v ) :
return ( v [ 0 ] , - v [ 2 ] , v [ 1 ] )
def import_fix_winding ( l ) :
return l
def convert_model ( geo_model , mesh_data , obj , scale ) :
#Convert the geo_model into a GeoMesh.
geomesh = geo_model . saveToGeoMesh ( )
indices = [ i for face in geomesh . face for i in import_fix_winding ( face . vert_indexes ) ]
texture_indices = [ face . texture_index for face in geomesh . face ]
mesh_data . vertices . add ( len ( geomesh . geovertex ) )
mesh_data . loops . add ( len ( indices ) )
mesh_data . polygons . add ( len ( geomesh . face ) )
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 ) ]
loop_totals = [ ]
loop_starts = [ ]
i = 0
for f in geomesh . face :
loop_totals . append ( len ( f . vert_indexes ) )
loop_starts . append ( i )
i + = loop_totals [ - 1 ]
mesh_data . vertices . foreach_set ( " co " , coords )
mesh_data . vertices . foreach_set ( " normal " , normals )
mesh_data . loops . foreach_set ( " vertex_index " , indices )
mesh_data . polygons . foreach_set ( " loop_start " , loop_starts )
mesh_data . polygons . foreach_set ( " loop_total " , loop_totals )
mesh_data . polygons . foreach_set ( " material_index " , texture_indices )
#mesh_data.update()
vgroup = { }
for w_name in geomesh . weights :
vgroup [ w_name ] = obj . vertex_groups . new ( name = w_name )
for i , v in enumerate ( geomesh . geovertex ) :
for w in v . weights :
#print("vertex idx: %s group: %s weight: %s" % (i, w[0], w[1]))
vgroup [ w [ 0 ] ] . add ( [ i ] , w [ 1 ] , ' REPLACE ' )
#for v in ob.vertices:
# weight_values.append( v.groups[o.vertex_groups[vg_name].index].weight )
#???mesh_data.validate(False)
d = mesh_data . uv_layers . new ( ) . data
uvs = [ c for f in geomesh . face for i in f . vert_indexes for c in geomesh . geovertex [ i ] . uv ]
d . foreach_set ( ' uv ' , uvs )
mesh_data . validate ( )
mesh_data . update ( )
mesh_data . vertices . foreach_set ( " normal " , normals )
mesh_data . update ( )
def convert_model ( geo_model , mesh , obj ) :
#Create vertices
#todo: populate coordinates
#todo: populate normals
#todo: create edges
#todo: create faces
#todo: populate uvs
mesh . validate ( )
mesh . update ( )
#todo: attempt to load textures/images
#todo: attempt to load textures/images
@ -19,15 +87,16 @@ def load(operator, context, scale = 1.0, filepath = "", global_matrix = None, us
fh_in . close ( )
fh_in . close ( )
for geo_model in geo . models :
for geo_model in geo . models :
#create object matching model's name (or next equivilant)
#create object matching model's name (or next equivilant)
mesh = bpy . data . meshes . new ( name = model . name . decode ( " utf-8 " ) )
mesh = bpy . data . meshes . new ( name = geo_model . name . decode ( " utf-8 " ) )
obj = bpy . data . objects . new ( geo_model . name . decode ( " utf-8 " ) , mesh )
#convert model to mesh
#convert model to mesh
convert_mode ( geo_model , mesh , Non e)
convert_mode l ( geo_model , mesh , obj , scal e)
#Create object for this mesh
#Create object for this mesh
scn = bpy . context . scene
scn = bpy . context . scene
obj = bpy . data . objects . new ( ply_name , mesh )
bpy . context . collection . objects . link ( obj )
scn . objects . link ( obj )
bpy . context . view_layer . objects . active = obj
scn . objects . active = obj
obj . select_set ( True )
obj . select = True
pass
pass
pass
pass
return { ' FINISHED ' }