mirror of
https://github.com/wahyd4/three.js.git
synced 2026-08-14 15:35:57 +10:00
Added model alignment to new Blender exporter.
Fixed crash bug with non-textured models in new Blender exporter. Fixed bottom alignment bug in OBJ converters.
This commit is contained in:
@@ -37,6 +37,9 @@ class ExportTHREEJSSlim(bpy.types.Operator, ExportHelper):
|
||||
use_modifiers = BoolProperty(name="Apply Modifiers", description="Apply modifiers to the exported mesh", default=True)
|
||||
use_normals = BoolProperty(name="Normals", description="Export normals", default=True)
|
||||
use_uv_coords = BoolProperty(name="UVs", description="Export texture coordinates", default=True)
|
||||
|
||||
align_types = [("None","None","None"), ("Center","Center","Center"), ("Bottom","Bottom","Bottom"), ("Top","Top","Top")]
|
||||
align_model = EnumProperty(name="Align model", description="Align model", items=align_types, default="Center")
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
@@ -57,9 +60,12 @@ class ExportTHREEJSSlim(bpy.types.Operator, ExportHelper):
|
||||
|
||||
row = layout.row()
|
||||
row.prop(self.properties, "use_modifiers")
|
||||
row = layout.row()
|
||||
row.prop(self.properties, "use_normals")
|
||||
row = layout.row()
|
||||
row.prop(self.properties, "use_uv_coords")
|
||||
row = layout.row()
|
||||
row.prop(self.properties, "align_model")
|
||||
|
||||
|
||||
def menu_func(self, context):
|
||||
|
||||
+93
-8
@@ -41,9 +41,6 @@ import random
|
||||
# #####################################################
|
||||
# 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):
|
||||
# white, red, green, blue, yellow, cyan, magenta
|
||||
@@ -131,6 +128,84 @@ def get_uv_indices(f, uvs, mesh):
|
||||
uv.append( uvs[veckey2d(i)] )
|
||||
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
|
||||
# #####################################################
|
||||
@@ -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
|
||||
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 = os.path.normpath(fn)
|
||||
fn_strip = os.path.basename(fn)
|
||||
@@ -428,14 +503,24 @@ def generate_materials_string(mesh, scene):
|
||||
# #####################################################
|
||||
# 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)
|
||||
|
||||
normals = extract_vertex_normals(mesh, use_normals)
|
||||
uvs = extract_uvs(mesh, use_uv_coords)
|
||||
|
||||
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_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
|
||||
# #####################################################
|
||||
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):
|
||||
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:
|
||||
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.write(text)
|
||||
file.close()
|
||||
|
||||
@@ -393,7 +393,7 @@ def bottom(vertices):
|
||||
cy = bb['y'][0]
|
||||
cz = bb['z'][0] + (bb['z'][1] - bb['z'][0])/2.0
|
||||
|
||||
translate(vertices, [-cx,cy,-cz])
|
||||
translate(vertices, [-cx,-cy,-cz])
|
||||
|
||||
def normalize(v):
|
||||
"""Normalize 3d vector"""
|
||||
|
||||
@@ -314,7 +314,7 @@ def bottom(vertices):
|
||||
cy = bb['y'][0]
|
||||
cz = bb['z'][0] + (bb['z'][1] - bb['z'][0])/2.0
|
||||
|
||||
translate(vertices, [-cx,cy,-cz])
|
||||
translate(vertices, [-cx,-cy,-cz])
|
||||
|
||||
def normalize(v):
|
||||
"""Normalize 3d vector"""
|
||||
|
||||
Reference in New Issue
Block a user