|
@@ -41,9 +41,6 @@ import random
|
|
# #####################################################
|
|
# #####################################################
|
|
# Configuration
|
|
# Configuration
|
|
# #####################################################
|
|
# #####################################################
|
|
-ALIGN = "center" # center bottom top none
|
|
|
|
-SHADING = "smooth" # smooth flat
|
|
|
|
-TYPE = "ascii" # ascii binary
|
|
|
|
|
|
|
|
# default colors for debugging (each material gets one distinct color):
|
|
# default colors for debugging (each material gets one distinct color):
|
|
# white, red, green, blue, yellow, cyan, magenta
|
|
# white, red, green, blue, yellow, cyan, magenta
|
|
@@ -131,6 +128,84 @@ def get_uv_indices(f, uvs, mesh):
|
|
uv.append( uvs[veckey2d(i)] )
|
|
uv.append( uvs[veckey2d(i)] )
|
|
return uv
|
|
return uv
|
|
|
|
|
|
|
|
+# #####################################################
|
|
|
|
+# Alignment
|
|
|
|
+# #####################################################
|
|
|
|
+def bbox(vertices):
|
|
|
|
+ """Compute bounding box of vertex array.
|
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ if len(vertices)>0:
|
|
|
|
+ minx = maxx = vertices[0].co.x
|
|
|
|
+ miny = maxy = vertices[0].co.y
|
|
|
|
+ minz = maxz = vertices[0].co.z
|
|
|
|
+
|
|
|
|
+ for v in vertices[1:]:
|
|
|
|
+ if v.co.x < minx:
|
|
|
|
+ minx = v.co.x
|
|
|
|
+ elif v.co.x > maxx:
|
|
|
|
+ maxx = v.co.x
|
|
|
|
+
|
|
|
|
+ if v.co.y < miny:
|
|
|
|
+ miny = v.co.y
|
|
|
|
+ elif v.co.y > maxy:
|
|
|
|
+ maxy = v.co.y
|
|
|
|
+
|
|
|
|
+ if v.co.z < minz:
|
|
|
|
+ minz = v.co.z
|
|
|
|
+ elif v.co.z > maxz:
|
|
|
|
+ maxz = v.co.z
|
|
|
|
+
|
|
|
|
+ return { 'x':[minx,maxx], 'y':[miny,maxy], 'z':[minz,maxz] }
|
|
|
|
+
|
|
|
|
+ else:
|
|
|
|
+ return { 'x':[0,0], 'y':[0,0], 'z':[0,0] }
|
|
|
|
+
|
|
|
|
+def translate(vertices, t):
|
|
|
|
+ """Translate array of vertices by vector t.
|
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ for i in range(len(vertices)):
|
|
|
|
+ vertices[i].co.x += t[0]
|
|
|
|
+ vertices[i].co.y += t[1]
|
|
|
|
+ vertices[i].co.z += t[2]
|
|
|
|
+
|
|
|
|
+def center(vertices):
|
|
|
|
+ """Center model (middle of bounding box).
|
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ bb = bbox(vertices)
|
|
|
|
+
|
|
|
|
+ cx = bb['x'][0] + (bb['x'][1] - bb['x'][0])/2.0
|
|
|
|
+ cy = bb['y'][0] + (bb['y'][1] - bb['y'][0])/2.0
|
|
|
|
+ cz = bb['z'][0] + (bb['z'][1] - bb['z'][0])/2.0
|
|
|
|
+
|
|
|
|
+ translate(vertices, [-cx,-cy,-cz])
|
|
|
|
+
|
|
|
|
+def top(vertices):
|
|
|
|
+ """Align top of the model with the floor (Y-axis) and center it around X and Z.
|
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ bb = bbox(vertices)
|
|
|
|
+
|
|
|
|
+ cx = bb['x'][0] + (bb['x'][1] - bb['x'][0])/2.0
|
|
|
|
+ cy = bb['y'][1]
|
|
|
|
+ cz = bb['z'][0] + (bb['z'][1] - bb['z'][0])/2.0
|
|
|
|
+
|
|
|
|
+ translate(vertices, [-cx,-cy,-cz])
|
|
|
|
+
|
|
|
|
+def bottom(vertices):
|
|
|
|
+ """Align bottom of the model with the floor (Y-axis) and center it around X and Z.
|
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ bb = bbox(vertices)
|
|
|
|
+
|
|
|
|
+ cx = bb['x'][0] + (bb['x'][1] - bb['x'][0])/2.0
|
|
|
|
+ cy = bb['y'][0]
|
|
|
|
+ cz = bb['z'][0] + (bb['z'][1] - bb['z'][0])/2.0
|
|
|
|
+
|
|
|
|
+ translate(vertices, [-cx,-cy,-cz])
|
|
|
|
+
|
|
# #####################################################
|
|
# #####################################################
|
|
# Elements
|
|
# Elements
|
|
# #####################################################
|
|
# #####################################################
|
|
@@ -398,7 +473,7 @@ def extract_materials(mesh, scene):
|
|
# http://www.blender.org/documentation/blender_python_api_2_54_0/bpy.types.Material.html#bpy.types.Material.specular_hardness
|
|
# http://www.blender.org/documentation/blender_python_api_2_54_0/bpy.types.Material.html#bpy.types.Material.specular_hardness
|
|
materials[m.name]["specular_coef"] = m.specular_hardness
|
|
materials[m.name]["specular_coef"] = m.specular_hardness
|
|
|
|
|
|
- if m.active_texture:
|
|
|
|
|
|
+ if m.active_texture and m.active_texture.type == 'IMAGE':
|
|
fn = bpy.path.abspath(m.active_texture.image.filepath)
|
|
fn = bpy.path.abspath(m.active_texture.image.filepath)
|
|
fn = os.path.normpath(fn)
|
|
fn = os.path.normpath(fn)
|
|
fn_strip = os.path.basename(fn)
|
|
fn_strip = os.path.basename(fn)
|
|
@@ -428,14 +503,24 @@ def generate_materials_string(mesh, scene):
|
|
# #####################################################
|
|
# #####################################################
|
|
# ASCII exporter
|
|
# ASCII exporter
|
|
# #####################################################
|
|
# #####################################################
|
|
-def generate_ascii_model(mesh, scene, use_normals, use_uv_coords):
|
|
|
|
|
|
+def generate_ascii_model(mesh, scene, use_normals, use_uv_coords, align_model):
|
|
|
|
+
|
|
|
|
+ vertices = mesh.vertices[:]
|
|
|
|
+
|
|
|
|
+ if align_model == 1:
|
|
|
|
+ center(vertices)
|
|
|
|
+ elif align_model == 2:
|
|
|
|
+ bottom(vertices)
|
|
|
|
+ elif align_model == 3:
|
|
|
|
+ top(vertices)
|
|
|
|
+
|
|
sfaces = sort_faces(mesh.faces, use_normals, use_uv_coords)
|
|
sfaces = sort_faces(mesh.faces, use_normals, use_uv_coords)
|
|
|
|
|
|
normals = extract_vertex_normals(mesh, use_normals)
|
|
normals = extract_vertex_normals(mesh, use_normals)
|
|
uvs = extract_uvs(mesh, use_uv_coords)
|
|
uvs = extract_uvs(mesh, use_uv_coords)
|
|
|
|
|
|
text = TEMPLATE_FILE_ASCII % {
|
|
text = TEMPLATE_FILE_ASCII % {
|
|
- "vertices" : ",".join(generate_vertex(v) for v in mesh.vertices),
|
|
|
|
|
|
+ "vertices" : ",".join(generate_vertex(v) for v in vertices),
|
|
|
|
|
|
"triangles" : ",".join(generate_triangle(f) for f in sfaces['triangles_flat']),
|
|
"triangles" : ",".join(generate_triangle(f) for f in sfaces['triangles_flat']),
|
|
"triangles_n" : ",".join(generate_triangle_n(f, normals, mesh) for f in sfaces['triangles_smooth']),
|
|
"triangles_n" : ",".join(generate_triangle_n(f, normals, mesh) for f in sfaces['triangles_smooth']),
|
|
@@ -462,7 +547,7 @@ def generate_ascii_model(mesh, scene, use_normals, use_uv_coords):
|
|
# #####################################################
|
|
# #####################################################
|
|
# Main
|
|
# Main
|
|
# #####################################################
|
|
# #####################################################
|
|
-def save(operator, context, filepath="", use_modifiers=True, use_normals=True, use_uv_coords=True):
|
|
|
|
|
|
+def save(operator, context, filepath="", use_modifiers=True, use_normals=True, use_uv_coords=True, align_model=1):
|
|
|
|
|
|
def rvec3d(v):
|
|
def rvec3d(v):
|
|
return round(v[0], 6), round(v[1], 6), round(v[2], 6)
|
|
return round(v[0], 6), round(v[1], 6), round(v[2], 6)
|
|
@@ -508,7 +593,7 @@ def save(operator, context, filepath="", use_modifiers=True, use_normals=True, u
|
|
if not active_uv_layer:
|
|
if not active_uv_layer:
|
|
use_uv_coords = False
|
|
use_uv_coords = False
|
|
|
|
|
|
- text = generate_ascii_model(mesh, scene, use_normals, use_uv_coords)
|
|
|
|
|
|
+ text = generate_ascii_model(mesh, scene, use_normals, use_uv_coords, align_model)
|
|
file = open(filepath, 'w')
|
|
file = open(filepath, 'w')
|
|
file.write(text)
|
|
file.write(text)
|
|
file.close()
|
|
file.close()
|