Skip to content

Commit 5d9f2ed

Browse files
serceroparoj
authored andcommitted
Fix for Error when Exporting a Shape Key's Normals (#56)
1 parent 0b9de88 commit 5d9f2ed

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [External OGRE Materials](#external-ogre-materials)
1313
- [Console Export](#console-export)
1414
- [Exporting Custom Vertex Groups](#exporting-custom-vertex-groups)
15+
- [Exporting Skeletal Animations](#exporting-skeletal-animations)
1516
- [Exporting Particle Systems](#exporting-particle-systems)
1617
- [Exporting Shape (or Pose) Animations](#exporting-shape-animations)
1718
- [Exporting Node Animations](#exporting-node-animations)

io_ogre/ogre/mesh.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def dot_mesh( ob, path, force_name=None, ignore_shape_animation=False, normals=T
110110
doc = SimpleSaxWriter(f, 'mesh', {})
111111

112112
# Very ugly, have to replace number of vertices later
113-
doc.start_tag('sharedgeometry ', {'vertexcount' : '__TO_BE_REPLACED_VERTEX_COUNT__'})
113+
doc.start_tag('sharedgeometry', {'vertexcount' : '__TO_BE_REPLACED_VERTEX_COUNT__'})
114114

115115
logger.info('* Writing shared geometry')
116116

@@ -160,6 +160,7 @@ def dot_mesh( ob, path, force_name=None, ignore_shape_animation=False, normals=T
160160
_remap_verts_ = []
161161
_remap_normals_ = []
162162
_face_indices_ = []
163+
163164
numverts = 0
164165

165166
# Create bmesh to help obtain custom vertex normals
@@ -172,18 +173,29 @@ def dot_mesh( ob, path, force_name=None, ignore_shape_animation=False, normals=T
172173
bm.from_mesh(mesh)
173174

174175
# Ogre only supports triangles
175-
bmesh.ops.triangulate(bm, faces=bm.faces)
176+
bmesh_return = bmesh.ops.triangulate(bm, faces=bm.faces)
176177
bm.to_mesh(mesh)
178+
179+
# Map the original face indices to the tesselated ones
180+
face_map = bmesh_return['face_map']
181+
182+
_tess_polygon_face_map_ = {}
183+
184+
for tess_face in face_map:
185+
#print("tess_face.index : %s <---> polygon_face.index : %s" % (tess_face.index, face_map[tess_face].index))
186+
_tess_polygon_face_map_[tess_face.index] = face_map[tess_face].index
177187

178188
# Vertex colors
179189
vertex_color_lookup = VertexColorLookup(mesh)
180190

181191
if tangents:
182192
mesh.calc_tangents(uvmap=mesh.uv_layers.active.name)
183193

184-
progressScale = 1.0 / (len(mesh.polygons) - 1)
194+
progressScale = 1.0 / len(mesh.polygons)
195+
196+
# Process mesh after triangulation
185197
for F in mesh.polygons:
186-
percent = F.index * progressScale
198+
percent = (F.index + 1) * progressScale
187199
sys.stdout.write( "\r + Faces [" + '=' * int(percent * 50) + '>' + '.' * int(50 - percent * 50) + "] " + str(int(percent * 10000) / 100.0) + "% ")
188200
sys.stdout.flush()
189201

@@ -222,9 +234,7 @@ def dot_mesh( ob, path, force_name=None, ignore_shape_animation=False, normals=T
222234
vert_uvs = []
223235
if dotextures:
224236
for layer in mesh.uv_layers:
225-
vert_uvs.append(layer.data[loop_idx].uv)
226-
"""for layer in uvtris[ tidx ]:
227-
vert_uvs.append(layer[ vidx ])"""
237+
vert_uvs.append( layer.data[ loop_idx ].uv )
228238

229239
''' Check if we already exported that vertex with same normal, do not export in that case,
230240
(flat shading in blender seems to work with face normals, so we copy each flat face'
@@ -257,8 +267,13 @@ def dot_mesh( ob, path, force_name=None, ignore_shape_animation=False, normals=T
257267
numverts += 1
258268
_remap_verts_.append( v )
259269
_remap_normals_.append( n )
260-
_face_indices_.append( F.index )
261-
270+
271+
# Use mapping from tesselated face to polygon face if the mapping exists
272+
if F.index in _tess_polygon_face_map_:
273+
_face_indices_.append( _tess_polygon_face_map_[F.index] )
274+
else:
275+
_face_indices_.append( F.index )
276+
262277
x,y,z = swap(v.co) # xz-y is correct!
263278

264279
doc.start_tag('vertex', {})
@@ -592,12 +607,18 @@ def duplicate_object(scene, name, copyobj):
592607

593608
if config.get('SHAPE_NORMALS'):
594609
n = _remap_normals_[ vidx ]
610+
595611
if smooth:
596-
pn = mathutils.Vector( [snormals[ v.index * 3 ], snormals[ v.index * 3 + 1], snormals[ v.index * 3 + 2]] )
612+
pn = mathutils.Vector( [snormals[ v.index * 3 + 0 ], snormals[ v.index * 3 + 1 ], snormals[ v.index * 3 + 2 ]] )
597613
else:
598614
vindex = _face_indices_[ vidx ]
599-
pn = mathutils.Vector( [snormals[ vindex * 3 ], snormals[ vindex * 3 + 1], snormals[ vindex * 3 + 2]] )
600-
nx,ny,nz = swap( pn - n )
615+
616+
pn = mathutils.Vector( [snormals[ vindex * 3 + 0 ], snormals[ vindex * 3 + 1 ], snormals[ vindex * 3 + 2 ]] )
617+
618+
if mesh.has_custom_normals:
619+
nx,ny,nz = n
620+
else:
621+
nx,ny,nz = swap( pn )
601622

602623
#for i,p in enumerate( skey.data ):
603624
#x,y,z = p.co - ob.data.vertices[i].co

0 commit comments

Comments
 (0)