Skip to content
Snippets Groups Projects
Commit 31a9b97f authored by Campbell Barton's avatar Campbell Barton
Browse files

option for x3d to write normals

parent 4e0ccae7
No related branches found
No related tags found
No related merge requests found
...@@ -97,6 +97,7 @@ class ExportX3D(bpy.types.Operator, ExportHelper): ...@@ -97,6 +97,7 @@ class ExportX3D(bpy.types.Operator, ExportHelper):
use_selection = BoolProperty(name="Selection Only", description="Export selected objects only", default=False) use_selection = BoolProperty(name="Selection Only", description="Export selected objects only", default=False)
use_apply_modifiers = BoolProperty(name="Apply Modifiers", description="Use transformed mesh data from each object", default=True) use_apply_modifiers = BoolProperty(name="Apply Modifiers", description="Use transformed mesh data from each object", default=True)
use_triangulate = BoolProperty(name="Triangulate", description="Write quads into 'IndexedTriangleSet'", default=True) use_triangulate = BoolProperty(name="Triangulate", description="Write quads into 'IndexedTriangleSet'", default=True)
use_normals = BoolProperty(name="Normals", description="Write normals with geometry", default=False)
use_compress = BoolProperty(name="Compress", description="GZip the resulting file, requires a full python install", default=False) use_compress = BoolProperty(name="Compress", description="GZip the resulting file, requires a full python install", default=False)
axis_forward = EnumProperty( axis_forward = EnumProperty(
......
...@@ -103,7 +103,9 @@ def export(file, ...@@ -103,7 +103,9 @@ def export(file,
scene, scene,
use_apply_modifiers=False, use_apply_modifiers=False,
use_selection=True, use_selection=True,
EXPORT_TRI=False,): use_triangulate=False,
use_normals=False,
):
fw = file.write fw = file.write
...@@ -255,7 +257,7 @@ def export(file, ...@@ -255,7 +257,7 @@ def export(file,
return "%s" % (newname) return "%s" % (newname)
secureName.nodeID = 0 secureName.nodeID = 0
def writeIndexedFaceSet(ident, ob, mesh, mtx, world, EXPORT_TRI=False): def writeIndexedFaceSet(ident, ob, mesh, mtx, world):
shape_name_x3d = clean_str(ob.name) shape_name_x3d = clean_str(ob.name)
mesh_name_x3d = clean_str(mesh.name) mesh_name_x3d = clean_str(mesh.name)
...@@ -342,6 +344,7 @@ def export(file, ...@@ -342,6 +344,7 @@ def export(file,
mesh_materials_use_face_texture = [getattr(material, "use_face_texture", True) for material in mesh_materials] mesh_materials_use_face_texture = [getattr(material, "use_face_texture", True) for material in mesh_materials]
# fast access! # fast access!
mesh_vertices = mesh.vertices[:]
mesh_faces = mesh.faces[:] mesh_faces = mesh.faces[:]
mesh_faces_materials = [f.material_index for f in mesh_faces] mesh_faces_materials = [f.material_index for f in mesh_faces]
mesh_faces_vertices = [f.vertices[:] for f in mesh_faces] mesh_faces_vertices = [f.vertices[:] for f in mesh_faces]
...@@ -429,7 +432,7 @@ def export(file, ...@@ -429,7 +432,7 @@ def export(file,
mesh_faces_uv = mesh.uv_textures.active.data if is_uv else None mesh_faces_uv = mesh.uv_textures.active.data if is_uv else None
#-- IndexedFaceSet or IndexedLineSet #-- IndexedFaceSet or IndexedLineSet
if EXPORT_TRI: if use_triangulate:
fw("%s<IndexedTriangleSet " % ident) fw("%s<IndexedTriangleSet " % ident)
ident += "\t" ident += "\t"
...@@ -438,6 +441,10 @@ def export(file, ...@@ -438,6 +441,10 @@ def export(file,
if is_smooth: if is_smooth:
fw("creaseAngle=\"%.4g\" " % mesh.auto_smooth_angle) fw("creaseAngle=\"%.4g\" " % mesh.auto_smooth_angle)
if use_normals:
# currently not optional, could be made so:
fw("normalPerVertex=\"true\" ")
slot_uv = None slot_uv = None
slot_col = None slot_col = None
...@@ -517,21 +524,27 @@ def export(file, ...@@ -517,21 +524,27 @@ def export(file,
fw("%s<Coordinate " % ident) fw("%s<Coordinate " % ident)
fw("point=\"") fw("point=\"")
mesh_vertices = mesh.vertices
for x3d_v in vert_tri_list: for x3d_v in vert_tri_list:
fw("%.6g %.6g %.6g, " % mesh_vertices[x3d_v[1]].co[:]) fw("%.6g %.6g %.6g " % mesh_vertices[x3d_v[1]].co[:])
fw("\" />\n") fw("\" />\n")
if use_normals:
fw("%s<Normal " % ident)
fw("vector=\"")
for x3d_v in vert_tri_list:
fw("%.6g %.6g %.6g " % mesh_vertices[x3d_v[1]].normal[:])
fw("\" />\n")
if is_uv: if is_uv:
fw("%s<TextureCoordinate point=\"" % ident) fw("%s<TextureCoordinate point=\"" % ident)
for x3d_v in vert_tri_list: for x3d_v in vert_tri_list:
fw("%.4g %.4g, " % x3d_v[0][slot_uv]) fw("%.4g %.4g " % x3d_v[0][slot_uv])
fw("\" />\n") fw("\" />\n")
if is_col: if is_col:
fw("%s<Color color=\"" % ident) fw("%s<Color color=\"" % ident)
for x3d_v in vert_tri_list: for x3d_v in vert_tri_list:
fw("%.3g %.3g %.3g, " % x3d_v[0][slot_col]) fw("%.3g %.3g %.3g " % x3d_v[0][slot_col])
fw("\" />\n") fw("\" />\n")
fw("%s</IndexedTriangleSet>\n" % ident) fw("%s</IndexedTriangleSet>\n" % ident)
...@@ -545,6 +558,10 @@ def export(file, ...@@ -545,6 +558,10 @@ def export(file,
if is_smooth: if is_smooth:
fw("creaseAngle=\"%.4g\" " % mesh.auto_smooth_angle) fw("creaseAngle=\"%.4g\" " % mesh.auto_smooth_angle)
if use_normals:
# currently not optional, could be made so:
fw("normalPerVertex=\"true\" ")
# IndexedTriangleSet assumes true # IndexedTriangleSet assumes true
if is_col: if is_col:
fw("colorPerVertex=\"false\" ") fw("colorPerVertex=\"false\" ")
...@@ -556,10 +573,10 @@ def export(file, ...@@ -556,10 +573,10 @@ def export(file,
j = 0 j = 0
for i in face_group: for i in face_group:
if len(mesh_faces_vertices[i]) == 4: if len(mesh_faces_vertices[i]) == 4:
fw("%d %d %d %d -1, " % (j, j + 1, j + 2, j + 3)) fw("%d %d %d %d -1 " % (j, j + 1, j + 2, j + 3))
j += 4 j += 4
else: else:
fw("%d %d %d -1, " % (j, j + 1, j + 2)) fw("%d %d %d -1 " % (j, j + 1, j + 2))
j += 3 j += 3
fw("\" ") fw("\" ")
# --- end texCoordIndex # --- end texCoordIndex
...@@ -569,9 +586,9 @@ def export(file, ...@@ -569,9 +586,9 @@ def export(file,
for i in face_group: for i in face_group:
fv = mesh_faces_vertices[i] fv = mesh_faces_vertices[i]
if len(fv) == 3: if len(fv) == 3:
fw("%i %i %i -1, " % fv) fw("%i %i %i -1 " % fv)
else: else:
fw("%i %i %i %i -1, " % fv) fw("%i %i %i %i -1 " % fv)
fw("\" ") fw("\" ")
# --- end coordIndex # --- end coordIndex
...@@ -583,19 +600,28 @@ def export(file, ...@@ -583,19 +600,28 @@ def export(file,
if True: if True:
if is_coords_written: if is_coords_written:
fw("%s<Coordinate USE=\"%s%s\" />\n" % (ident, "coord_", mesh_name_x3d)) fw("%s<Coordinate USE=\"%s%s\" />\n" % (ident, "coord_", mesh_name_x3d))
if use_normals:
fw("%s<Normal USE=\"%s%s\" />\n" % (ident, "normals_", mesh_name_x3d))
else: else:
fw("%s<Coordinate DEF=\"%s%s\" " % (ident, "coord_", mesh_name_x3d)) fw("%s<Coordinate DEF=\"%s%s\" " % (ident, "coord_", mesh_name_x3d))
fw("point=\"") fw("point=\"")
for v in mesh.vertices: for v in mesh.vertices:
fw("%.6g %.6g %.6g, " % v.co[:]) fw("%.6g %.6g %.6g " % v.co[:])
fw("\" />\n") fw("\" />\n")
is_coords_written = True is_coords_written = True
if use_normals:
fw("%s<Normal DEF=\"%s%s\" " % (ident, "normals_", mesh_name_x3d))
fw("vector=\"")
for v in mesh.vertices:
fw("%.6g %.6g %.6g " % v.normal[:])
fw("\" />\n")
if is_uv: if is_uv:
fw("%s<TextureCoordinate point=\"" % ident) fw("%s<TextureCoordinate point=\"" % ident)
for i in face_group: for i in face_group:
for uv in mesh_faces_uv[i].uv: for uv in mesh_faces_uv[i].uv:
fw("%.4g %.4g, " % uv[:]) fw("%.4g %.4g " % uv[:])
del mesh_faces_uv del mesh_faces_uv
fw("\" />\n") fw("\" />\n")
...@@ -603,7 +629,7 @@ def export(file, ...@@ -603,7 +629,7 @@ def export(file,
fw("%s<Color color=\"" % ident) fw("%s<Color color=\"" % ident)
# XXX, 1 color per face, only # XXX, 1 color per face, only
for i in face_group: for i in face_group:
fw("%.3g %.3g %.3g, " % mesh_faces_col[i].color1[:]) fw("%.3g %.3g %.3g " % mesh_faces_col[i].color1[:])
fw("\" />\n") fw("\" />\n")
#--- output vertexColors #--- output vertexColors
...@@ -805,7 +831,7 @@ def export(file, ...@@ -805,7 +831,7 @@ def export(file,
me = ob.data me = ob.data
if me is not None: if me is not None:
writeIndexedFaceSet(ident, ob, me, ob_mat, world, EXPORT_TRI=EXPORT_TRI) writeIndexedFaceSet(ident, ob, me, ob_mat, world)
# free mesh created with create_mesh() # free mesh created with create_mesh()
if me != ob.data: if me != ob.data:
...@@ -845,6 +871,7 @@ def save(operator, context, filepath="", ...@@ -845,6 +871,7 @@ def save(operator, context, filepath="",
use_selection=True, use_selection=True,
use_apply_modifiers=False, use_apply_modifiers=False,
use_triangulate=False, use_triangulate=False,
use_normals=False,
use_compress=False, use_compress=False,
global_matrix=None, global_matrix=None,
): ):
...@@ -879,7 +906,8 @@ def save(operator, context, filepath="", ...@@ -879,7 +906,8 @@ def save(operator, context, filepath="",
context.scene, context.scene,
use_apply_modifiers=use_apply_modifiers, use_apply_modifiers=use_apply_modifiers,
use_selection=use_selection, use_selection=use_selection,
EXPORT_TRI=use_triangulate, use_triangulate=use_triangulate,
use_normals=use_normals,
) )
return {'FINISHED'} return {'FINISHED'}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment