Hi all,

find attached a patch for blender's ac3d_export.py

It contains three modifications:

- properly handle modifiers (e.g. Subsurf, Mirror, Boolean)
- properly handle material indices attached to the object instead of the
mesh
- export face vertices in clockwise order looking along the normal
specified in Blender. This also properly handles multiple mirrored
copies of the same mesh, e.g. left and right cockpit door.

This should solve many of the normals issues with the Blender AC3D export.

I'll submit this patch also to William Germano, the current maintainer
of ac3d_export.py.

You'll have to apply the patch using "patch -p0".

Cheers,
Ralf
Index: ac3d_export.py
===================================================================
RCS file: /cvsroot/bf-blender/blender/release/scripts/ac3d_export.py,v
retrieving revision 1.15
diff -u -b -r1.15 ac3d_export.py
--- ac3d_export.py	18 Jun 2006 19:05:51 -0000	1.15
+++ ac3d_export.py	21 Nov 2006 12:51:11 -0000
@@ -96,6 +96,7 @@
 import Blender
 from Blender import sys as bsys
 from Blender import Mathutils
+import Blender.NMesh
 
 # Globals
 ERROR_MSG = '' # popup error msg
@@ -347,7 +348,8 @@
 				self.kids(kidsnum)
 
 			if objtype == 'Mesh':
-				mesh = self.mesh = obj.getData()
+				mesh = self.mesh = Blender.NMesh.GetRawFromObject(obj.name)
+				# Transform the mesh to worldspace, don't recalc the normals
 				meshes = self.split_mesh(mesh)
 				if len(meshes) > 1:
 					if NO_SPLIT or self.dont_split(objname):
@@ -454,8 +456,8 @@
 		if texline: file.write(texline)
 		if AC3D_4:
 			self.crease(mesh.getMaxSmoothAngle())
-		self.numvert(mesh.verts, obj.getMatrix())
-		self.numsurf(mesh.faces, mesh.hasFaceUV(), foomesh)
+		self.numvert(mesh.verts,obj.getMatrix())
+		self.numsurf(mesh.faces, mesh.hasFaceUV(), obj.getMatrix(), foomesh)
 
 	def MATERIALS(self, meshlist):
 		for meobj in meshlist:
@@ -551,13 +553,13 @@
 	def numvert(self, verts, matrix):
 		file = self.file
 		file.write("numvert %s\n" % len(verts))
-		m = matrix * acmatrix
+		m = matrix*acmatrix
 		verts = transform_verts(verts, m)
 		for v in verts:
 			v0, v1, v2 = Round(v[0]), Round(v[1]), Round(v[2])
 			file.write("%s %s %s\n" % (v0, v1, v2))
 
-	def numsurf(self, faces, hasFaceUV, foomesh = False):
+	def numsurf(self, faces, hasFaceUV, matrix, foomesh = False):
 
 		global ADD_DEFAULT_MAT, MATIDX_ERROR
 		file = self.file
@@ -568,14 +570,22 @@
 
 		mlist = self.mlist
 		omlist = {}
-		objmats = self.mesh.materials[:]
+		objmats = self.obj.getMaterials()[:]
+		meshmats = self.mesh.materials[:]
 		matidx_error_told = 0
 		for i in range(len(objmats)):
 			objmats[i] = objmats[i].name
+		for i in range(len(meshmats)):
+			meshmats[i] = meshmats[i].name
 		for f in faces:
 			m_idx = f.materialIndex
+			old_m_idx=m_idx
 			try:
+				if (m_idx<len(objmats) and objmats[m_idx] is not None):
 				m_idx = mlist.index(objmats[m_idx])
+				else:
+					m_idx = mlist.index(meshmats[m_idx])
+				if ADD_DEFAULT_MAT: m_idx += 1
 			except IndexError:
 				if not MATIDX_ERROR:
 					rdat = REPORT_DATA['warns']
@@ -599,11 +609,11 @@
 			two_side = (two_side > 0) << 1
 			flaghigh = f.smooth | two_side
 			surfstr = "SURF 0x%d%d\n" % (flaghigh, flaglow)
-			if ADD_DEFAULT_MAT and objmats: m_idx += 1
 			matstr = "mat %s\n" % m_idx
 			refstr = "refs %s\n" % refs
 			u, v, vi = 0, 0, 0
 			fvstr = []
+			self.invertface(f,verts,foomesh,matrix)
 			if foomesh:
 				for vert in f.v:
 					fvstr.append(str(vert.index))
@@ -625,6 +635,28 @@
 
 			file.write("%s%s%s%s" % (surfstr, matstr, refstr, fvstr))
 
+	def invertface(self,f,verts,foomesh,matrix):
+		if foomesh:
+			v0=f.v[0].v.co
+			v1=f.v[1].v.co
+			v2=f.v[2].v.co
+		else:
+			v0=f.v[0].co
+			v1=f.v[1].co
+			v2=f.v[2].co
+		v0=v0*matrix
+		v1=v1*matrix
+		v2=v2*matrix
+		norig=Mathutils.Vector(f.no)*matrix.rotationPart()
+		nnew=Mathutils.TriangleNormal(v0,v1,v2)
+		dir=norig*nnew
+		if dir>=0:
+			return
+		f.col.reverse()
+		f.uv.reverse()
+		f.v.reverse()
+			
+
 # End of Class AC3DExport
 
 from Blender.Window import FileSelector
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to