You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.7 KiB
Python

from .geo import Geo
from .geomesh import *
import bpy.path
import bpy
from bpy_extras.io_utils import axis_conversion
def convert_mesh(geo_model, mesh, obj):
# Be sure tessface & co are available!
if not mesh.tessfaces and mesh.polygons:
mesh.calc_tessface()
mesh_verts = mesh.vertices # save a lookup
has_uv = bool(mesh.tessface_uv_textures)
if has_uv:
active_uv_layer = mesh.tessface_uv_textures.active
if not active_uv_layer:
has_uv = False
else:
active_uv_layer = active_uv_layer.data
geomesh = GeoMesh()
texture_name = "white"
print("len(mesh.tessfaces): %s" % len(mesh.tessfaces))
print("mesh.tessfaces: %s" % repr(mesh.tessfaces))
for i, f in enumerate(mesh.tessfaces):
if has_uv:
uv = active_uv_layer[i]
texture_image = uv.image
uv = [uv.uv1, uv.uv2, uv.uv3, uv.uv4]
if texture_image is None:
texture_name = "white"
else:
texture_name = texture_image.name
if texture_name == "":
texture_name = bpy.path.display_name_from_filepath(texture_image.filepath)
if texture_name == "":
texture_name = "white"
else:
uv = [(0, 0)] * 4
texture_name = "white"
f_verts = f.vertices
verts = []
norms = []
groups = []
geoverts = []
for i, v_index in enumerate(f_verts):
v = mesh_verts[v_index]
verts.append(v.co)
norms.append(v.normal)
weights = []
for weight in v.groups:
group = obj.vertex_groups[weight.group]
w = [group.name, weight.weight]
weights.append(w)
gv = GeoVertex(v.co, v.normal, uv[i], weights)
geoverts.append(gv)
geomesh.addFace(geoverts, texture_name)
print("face: vertices: %s uvs: %s norms: %s groups: %s" % (verts, uv, norms, weights))
#todo: optimize face order for bone association
geomesh.dump()
geo_model.loadFromGeoMesh(geomesh)
pass
def save(operator, context, scale = 1.0, filepath = "", global_matrix = None, use_mesh_modifiers = True):
print("export_geo.save(): %s" % (filepath, ))
geo = Geo()
geo.getTextureIndex("white.tga")
#geo.getTextureIndex("white")
body_name = bpy.path.display_name_from_filepath(filepath)
geo.setName(body_name)
axis_rotation = axis_conversion('-Y', 'Z', 'Z', 'Y')
axis_rotation.resize_4x4()
print("scale: %s" % (scale, ))
for ob in context.selected_objects:
print("Object: %s (%s)" % (ob.name, ob.type))
if ob.type != "MESH":
continue
ob.update_from_editmode()
if global_matrix is None:
from mathutils import Matrix
global_matrix = Matrix()
# get the modifiers
mesh = ob.to_mesh(bpy.context.scene, use_mesh_modifiers, "PREVIEW")
#translate_matrix = Matrix.Translation(-ob.location)
translate_matrix = Matrix()
#scale_matrix = Matrix.Scale(1 / 0.30480000376701355, 4)
obj_matrix = ob.matrix_world
print(obj_matrix)
obj_scale = obj_matrix.to_scale()[0] #assume scaling is uniform on all axis
print("obj_scale: %s final_scale: %s" % (obj_scale, obj_scale * scale))
print(obj_scale * scale)
scale_matrix = Matrix.Scale(obj_scale * scale, 4)
mesh.transform(global_matrix * scale_matrix * translate_matrix * axis_rotation)
mesh.calc_normals()
geo_model = geo.addModel(ob.name)
convert_mesh(geo_model, mesh, ob)
data = geo.saveToData()
fh = open(filepath, "wb")
fh.write(data)
fh.close()
return {'FINISHED'}