Page MenuHome

patch.diff

File Metadata

Author
Fabio Arnold (donfabio)
Created
Mar 9 2014, 7:37 PM

patch.diff

From bbb65cddee0960ae683f58f6136e0bc6ebc0c985 Mon Sep 17 00:00:00 2001
From: Fabio Arnold <fabioarnold90@gmail.com>
Date: Sun, 9 Mar 2014 18:43:29 +0100
Subject: [PATCH] STL export normals
---
io_mesh_stl/stl_utils.py | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/io_mesh_stl/stl_utils.py b/io_mesh_stl/stl_utils.py
index 49e8d0f..020a897 100644
--- a/io_mesh_stl/stl_utils.py
+++ b/io_mesh_stl/stl_utils.py
@@ -30,6 +30,7 @@ import struct
import mmap
import contextlib
import itertools
+from mathutils import Vector # for face normal construction
# TODO: endien
@@ -165,13 +166,13 @@ def _binary_write(filename, faces):
# 3 vertex == 9f
pack = struct.Struct('<9f').pack
- # pad is to remove normal, we do use them
- pad = b'\0' * struct.calcsize('<3f')
nb = 0
for verts in faces:
- # write pad as normal + vertexes + pad as attributes
- data.write(pad + pack(*itertools.chain.from_iterable(verts)))
+ # calculate face normal
+ normal = Vector.cross(verts[1] - verts[0], verts[2] - verts[0]).normalized()
+ # write normal + vertexes + pad as attributes
+ data.write(struct.pack('3f', *normal) + pack(*itertools.chain.from_iterable(verts)))
data.write(b'\0\0')
nb += 1
@@ -186,7 +187,9 @@ def _ascii_write(filename, faces):
data.write('solid %s\n' % header)
for face in faces:
- data.write('''facet normal 0 0 0\nouter loop\n''')
+ # calculate face normal
+ normal = Vector.cross(face[1] - face[0], face[2] - face[0]).normalized()
+ data.write('facet normal %f %f %f\nouter loop\n' % normal[:])
for vert in face:
data.write('vertex %f %f %f\n' % vert[:])
data.write('endloop\nendfacet\n')
--
1.8.4.2

Event Timeline