|
|
|
@ -4,10 +4,11 @@ from .bones import *
|
|
|
|
|
import bpy.path
|
|
|
|
|
import bpy
|
|
|
|
|
import mathutils
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
|
|
from bpy_extras.io_utils import axis_conversion
|
|
|
|
|
|
|
|
|
|
def convert_mesh(geo_model, mesh, obj):
|
|
|
|
|
def convert_mesh(geo_model, mesh, obj, scale):
|
|
|
|
|
if bpy.app.version < (2, 80):
|
|
|
|
|
# Be sure tessface & co are available!
|
|
|
|
|
if not mesh.tessfaces and mesh.polygons:
|
|
|
|
@ -38,6 +39,14 @@ def convert_mesh(geo_model, mesh, obj):
|
|
|
|
|
|
|
|
|
|
geomesh = GeoMesh()
|
|
|
|
|
|
|
|
|
|
vertex_normals = defaultdict(list)
|
|
|
|
|
for loop in mesh.loops:
|
|
|
|
|
vertex_normals[loop.vertex_index].append(loop.normal)
|
|
|
|
|
averaged_normals = {
|
|
|
|
|
vertex_index: sum(normals, mathutils.Vector()) / len(normals)
|
|
|
|
|
for vertex_index, normals in vertex_normals.items()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
texture_name = "white.tga"
|
|
|
|
|
if bpy.app.version < (2, 80):
|
|
|
|
|
faces = mesh.tessfaces
|
|
|
|
@ -73,28 +82,29 @@ def convert_mesh(geo_model, mesh, obj):
|
|
|
|
|
uv = [uv.uv1, uv.uv2, uv.uv3, uv.uv4]
|
|
|
|
|
else:
|
|
|
|
|
print("uv.uv: %s" % uv.uv)
|
|
|
|
|
uv = [active_uv_layer[l].uv[:] for l in f.loops]
|
|
|
|
|
uv = [active_uv_layer[l].uv[:] for l in (f.loops[0], f.loops[2], f.loops[1])]
|
|
|
|
|
else:
|
|
|
|
|
uv = [(0, 0)] * 4
|
|
|
|
|
texture_name = "white.tga"
|
|
|
|
|
f_verts = f.vertices
|
|
|
|
|
f_verts = (f_verts[0], f_verts[2], f_verts[1])
|
|
|
|
|
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)
|
|
|
|
|
verts.append((-v.co[0], v.co[2], -v.co[1]))
|
|
|
|
|
norm = averaged_normals[v_index]
|
|
|
|
|
norms.append((-norm[0], norm[2], -norm[1]))
|
|
|
|
|
weights = []
|
|
|
|
|
for weight in v.groups:
|
|
|
|
|
group = obj.vertex_groups[weight.group]
|
|
|
|
|
w = [group.name, weight.weight]
|
|
|
|
|
weights.append(w)
|
|
|
|
|
print("i: %s f_verts: %s uv: %s" % (i, repr(f_verts), repr(uv)))
|
|
|
|
|
gv = GeoVertex(v.co, -v.normal, uv[i], weights)
|
|
|
|
|
gv = GeoVertex(verts[-1], norms[-1], uv[i], weights)
|
|
|
|
|
geoverts.append(gv)
|
|
|
|
|
geoverts.reverse()
|
|
|
|
|
geomesh.addFace(geoverts, texture_name)
|
|
|
|
|
|
|
|
|
|
print("face: vertices: %s uvs: %s norms: %s groups: %s" % (verts, uv, norms, weights))
|
|
|
|
@ -155,14 +165,10 @@ def save(operator, context, scale = 1.0, filepath = "", global_matrix = None, us
|
|
|
|
|
print("obj_scale: %s final_scale: %s" % (obj_scale, obj_scale * scale))
|
|
|
|
|
print(obj_scale * scale)
|
|
|
|
|
scale_matrix = Matrix.Scale(obj_scale * scale, 4)
|
|
|
|
|
if bpy.app.version < (2, 80):
|
|
|
|
|
mesh.transform(global_matrix * scale_matrix * translate_matrix * axis_rotation)
|
|
|
|
|
else:
|
|
|
|
|
mesh.transform(global_matrix @ scale_matrix @ translate_matrix @ axis_rotation)
|
|
|
|
|
|
|
|
|
|
geo_model = geo.addModel(ob.name.rsplit(".", 1)[0])
|
|
|
|
|
|
|
|
|
|
convert_mesh(geo_model, mesh, ob)
|
|
|
|
|
convert_mesh(geo_model, mesh, ob, scale * obj_scale)
|
|
|
|
|
|
|
|
|
|
if False: #flip bones
|
|
|
|
|
for i in range(len(geo.models) - 1, -1, -1):
|
|
|
|
|