|
|
|
@ -6,12 +6,18 @@ import bpy
|
|
|
|
|
from bpy_extras.io_utils import axis_conversion
|
|
|
|
|
|
|
|
|
|
def convert_mesh(geo_model, mesh, obj):
|
|
|
|
|
if bpy.app.version < (2, 80):
|
|
|
|
|
# Be sure tessface & co are available!
|
|
|
|
|
if not mesh.tessfaces and mesh.polygons:
|
|
|
|
|
mesh.calc_tessface()
|
|
|
|
|
else:
|
|
|
|
|
# Be sure tessellated loop trianlges are available!
|
|
|
|
|
if not mesh.loop_triangles and mesh.polygons:
|
|
|
|
|
mesh.calc_loop_triangles()
|
|
|
|
|
|
|
|
|
|
mesh_verts = mesh.vertices # save a lookup
|
|
|
|
|
|
|
|
|
|
if bpy.app.version < (2, 80):
|
|
|
|
|
has_uv = bool(mesh.tessface_uv_textures)
|
|
|
|
|
if has_uv:
|
|
|
|
|
active_uv_layer = mesh.tessface_uv_textures.active
|
|
|
|
@ -19,28 +25,56 @@ def convert_mesh(geo_model, mesh, obj):
|
|
|
|
|
has_uv = False
|
|
|
|
|
else:
|
|
|
|
|
active_uv_layer = active_uv_layer.data
|
|
|
|
|
else:
|
|
|
|
|
has_uv = bool(mesh.uv_layers)
|
|
|
|
|
if has_uv:
|
|
|
|
|
active_uv_layer = mesh.uv_layers.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):
|
|
|
|
|
texture_name = "white.tga"
|
|
|
|
|
if bpy.app.version < (2, 80):
|
|
|
|
|
faces = mesh.tessfaces
|
|
|
|
|
print("len(mesh.tessfaces): %s" % len(faces))
|
|
|
|
|
print("mesh.tessfaces: %s" % repr(faces))
|
|
|
|
|
else:
|
|
|
|
|
faces = mesh.loop_triangles
|
|
|
|
|
for i, f in enumerate(faces):
|
|
|
|
|
if has_uv:
|
|
|
|
|
uv = active_uv_layer[i]
|
|
|
|
|
if bpy.app.version < (2, 80):
|
|
|
|
|
texture_image = uv.image
|
|
|
|
|
uv = [uv.uv1, uv.uv2, uv.uv3, uv.uv4]
|
|
|
|
|
if texture_image is None:
|
|
|
|
|
texture_name = "white"
|
|
|
|
|
texture_name = "white.tga"
|
|
|
|
|
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"
|
|
|
|
|
texture_name = "white.tga"
|
|
|
|
|
else:
|
|
|
|
|
mat = mesh.materials[f.material_index]
|
|
|
|
|
if len(mat.texture_paint_images) <= 0:
|
|
|
|
|
texture_name = mat.name
|
|
|
|
|
else:
|
|
|
|
|
texture_image = mat.texture_paint_images[0]
|
|
|
|
|
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.tga"
|
|
|
|
|
if bpy.app.version < (2, 80):
|
|
|
|
|
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]
|
|
|
|
|
else:
|
|
|
|
|
uv = [(0, 0)] * 4
|
|
|
|
|
texture_name = "white"
|
|
|
|
|
texture_name = "white.tga"
|
|
|
|
|
f_verts = f.vertices
|
|
|
|
|
verts = []
|
|
|
|
|
norms = []
|
|
|
|
@ -55,6 +89,7 @@ def convert_mesh(geo_model, mesh, obj):
|
|
|
|
|
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)
|
|
|
|
|
geoverts.append(gv)
|
|
|
|
|
geomesh.addFace(geoverts, texture_name)
|
|
|
|
@ -90,7 +125,10 @@ def save(operator, context, scale = 1.0, filepath = "", global_matrix = None, us
|
|
|
|
|
global_matrix = Matrix()
|
|
|
|
|
|
|
|
|
|
# get the modifiers
|
|
|
|
|
if bpy.app.version < (2, 80):
|
|
|
|
|
mesh = ob.to_mesh(bpy.context.scene, use_mesh_modifiers, "PREVIEW")
|
|
|
|
|
else:
|
|
|
|
|
mesh = ob.to_mesh(preserve_all_data_layers = True)
|
|
|
|
|
|
|
|
|
|
#translate_matrix = Matrix.Translation(-ob.location)
|
|
|
|
|
translate_matrix = Matrix()
|
|
|
|
@ -101,7 +139,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)
|
|
|
|
|
|
|
|
|
|
mesh.calc_normals()
|
|
|
|
|
|
|
|
|
|