Here is some snippets from my exporter (not complete yet)
import bpy
import os
import math
import mathutils
import struct
from mathutils import Vector
def CalcDegree(eu):
degree_x, degree_y, degree_z = map(math.degrees, eu)
return Vector([degree_x, degree_y, degree_z])
def CalcDegree(eu):
degree_x, degree_y, degree_z = map(math.degrees, eu)
return Vector([degree_x, degree_y, degree_z])
objs_list = bpy.context.selected_objects # Selected object
mesh_obj=objs_list[0] # Presume its a mesh object with uv data
#Verts
Write_Longs(pFile, [len(mesh_obj.data.vertices)])
for tVert in mesh_obj.data.vertices:
Write_Floats(pFile, tVert.co) # Coord
Write_Floats(pFile, tVert.normal) # Normal of vert
def Export_Face(pFile, tVerts, tFace):
# These are the vertices to use (can be splitting!)
iV1 = tVerts[0]
iV2 = tVerts[1]
iV3 = tVerts[2]
# Save the details to do with the face
Write_Longs(pFile,[tFace.vertices[iV1],tFace.vertices[iV2],tFace.vertices[iV3]])
# Save the face normal as well
Write_Floats(pFile, tFace.normal)
# Save extra face details
if tFace.use_smooth==False:
Write_Bytes(pFile,[0])
else:
Write_Bytes(pFile,[1])
return
#Polygons
for tFace in mesh_obj.data.faces:
# All are at least a 3 vert face
tVerts=(0,1,2)
Export_Face(pFile, tVerts, tFace)
if len(tFace.vertices)>=4:
# This is 4? verts big, triangulate ...
tVerts=(2,0,3)
Export_Face(pFile, tVerts, tFace)
def Export_UVFace(pFile, tVerts, tFace):
# These are the vertices to use (can be splitting!)
iV1 = tVerts[0]
iV2 = tVerts[1]
iV3 = tVerts[2]
# Save the details to do with the face
Write_Floats(pFile,[tFace[iV1][0],tFace[iV1][1]]) #UV mapping of this polygon
Write_Floats(pFile,[tFace[iV2][0],tFace[iV2][1]])
Write_Floats(pFile,[tFace[iV3][0],tFace[iV3][1]])
return
#UV data of face
if mesh_obj.data.uv_textures:
for uv_channel in mesh_obj.data.uv_textures:
Write_String(pFile,uv_channel.name)
for tFace in uv_channel.data:
# Calculate the texture id for this face
iTextureID = 0 # Default to no texture image
if tFace.image:
iTextureID = (ltexture_list.index(tFace.image.name))+1 #Get the texture from the list, so we have its ID
tVerts=(0,1,2)
Write_Longs(pFile, [iTextureID]) # Save the texture id
Export_UVFace(pFile, tVerts, tFace.uv) # Save the UV mapping information
if len(tFace.uv)>=4:
tVerts=(2,0,3)
Write_Longs(pFile, [iTextureID])
Export_UVFace(pFile, tVerts, tFace.uv)