Created
February 25, 2019 20:16
-
-
Save mandyedi/200aaa098c21ab078e81770b241c26da to your computer and use it in GitHub Desktop.
Not so feature rich blender exporter (to a custom binary file format). #python #blender #model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Exports a blender model into my binary model format. | |
Python: 3.2 | |
Blender: 263 | |
Author: Eduárd Mándy | |
""" | |
""" | |
MBM FILE SRUCTURE | |
HEADERS | |
struct sHeader | |
{ | |
unsigned int numObjects; | |
unsigned int numMaterials; | |
unsigned int offsetObjects; | |
unsigned int offsetMaterials; | |
}; | |
struct sObjects | |
{ | |
unsigned int materialID; | |
unsigned int numVertices; | |
unsigned int numTexCoords; | |
unsigned int numNormals; | |
unsigned int numFaceIndices; | |
unsigned int offsetVertices; | |
unsigned int offsetTexCoords; | |
unsigned int offsetNormals; | |
unsigned int offsetFaceIndices; | |
}; | |
struct sMaterials | |
{ | |
unsigned int lengthTexFileName; | |
unsigned int offsetTexFileName; | |
}; | |
DATA | |
texFileNames | |
vertices | |
texCoords | |
normals | |
faceIndices | |
""" | |
import bpy | |
import array | |
headerSize = 16 | |
objectsSize = 36 | |
materialsSize = 8 | |
numObjects = len(bpy.data.meshes) | |
numMaterials = numObjects | |
offsetObjects = headerSize | |
offsetMaterials = offsetObjects + objectsSize * numObjects | |
header = [] | |
objects = [] | |
materials = [] | |
texFileNames = [] | |
vertices = [] | |
texCoords = [] | |
normals = [] | |
faceIndices = [] | |
positionOffset = [] | |
offsetTexFileName = [] | |
offsetVertices = [] | |
offsetTexCoords = [] | |
offsetNormals = [] | |
offsetFaceIndices = [] | |
""" | |
SET POSITION OFFSET | |
""" | |
for object in bpy.data.objects: | |
if object.type == "MESH": | |
x = object.matrix_local[0].w | |
y = object.matrix_local[1].w | |
z = object.matrix_local[2].w | |
positionOffset.append([x, y, z]) | |
""" | |
SET DATA | |
""" | |
i = 0 | |
for mesh in bpy.data.meshes: | |
# set texture file names | |
tempString = mesh.materials[0].active_texture.image.name | |
tempList = [ord(c) for c in tempString] | |
texFileNames.extend( tempList ) | |
#set vertices | |
for vertex in mesh.vertices: | |
x = vertex.co.x | |
y = vertex.co.y | |
z = vertex.co.z | |
vertices.extend( [ y+positionOffset[i][1], z+positionOffset[i][2], x+positionOffset[i][0] ] ) | |
i += 1 | |
# set texture coordinates | |
for data in mesh.uv_layers.active.data: | |
texCoords.extend( data.uv ) | |
# set normals | |
for polygon in mesh.polygons: | |
normals.extend( polygon.normal ) | |
# set face indices | |
for polygon in mesh.polygons: | |
faceIndices.extend( polygon.vertices ) | |
""" | |
SET OFFSETS | |
""" | |
offset = headerSize + objectsSize * numObjects + materialsSize * numMaterials | |
# offsetTexFileName | |
for mesh in bpy.data.meshes: | |
offsetTexFileName.append(offset) | |
offset += len( mesh.materials[0].active_texture.image.name ) | |
# offsetVertices | |
for mesh in bpy.data.meshes: | |
offsetVertices.append(offset) | |
offset += len(mesh.vertices) * 12 | |
# offsetTexCoords | |
for mesh in bpy.data.meshes: | |
offsetTexCoords.append(offset) | |
offset += len(mesh.uv_layers.active.data) * 8 | |
# offsetNormals | |
for mesh in bpy.data.meshes: | |
offsetNormals.append(offset) | |
offset += len(mesh.polygons) * 12 | |
# offsetFaceIndices | |
for mesh in bpy.data.meshes: | |
offsetFaceIndices.append(offset) | |
offset += len(mesh.polygons) * 12 | |
""" | |
SET HEADER | |
""" | |
header.append( numObjects ) | |
header.append( numMaterials ) | |
header.append( offsetObjects ) | |
header.append( offsetMaterials ) | |
""" | |
SET OBJECTS | |
""" | |
mesh = bpy.data.meshes | |
for i in range(numObjects): | |
objects.append( i ) # materialID | |
objects.append( len( mesh[i].vertices ) ) # numVertices | |
objects.append( len( mesh[i].uv_layers.active.data ) ) # numTexCoords | |
objects.append( len( mesh[i].polygons ) ) # numNormals | |
objects.append( len( mesh[i].polygons ) * 3 ) # numFaces | |
objects.append( offsetVertices[i] ) | |
objects.append( offsetTexCoords[i] ) | |
objects.append( offsetNormals[i] ) | |
objects.append( offsetFaceIndices[i] ) | |
""" | |
SET MATERIALS DATA | |
""" | |
for i in range(numObjects): | |
materials.append( len( mesh[i].materials[0].active_texture.image.name ) ) | |
materials.append( offsetTexFileName[i] ) | |
""" | |
WRITE TO BINARY FILE | |
""" | |
f = open("2planes2.mbm", "wb") | |
headerArray = array.array('i', header) | |
objectsArray = array.array('i', objects) | |
materialsArray = array.array('i', materials) | |
texFileNamesArray = array.array('b', texFileNames) | |
verticesArray = array.array('f', vertices) | |
texCoordArray = array.array('f', texCoords) | |
normalsArray = array.array('f', normals) | |
facesIndArray = array.array('i', faceIndices) | |
headerArray.tofile(f) | |
objectsArray.tofile(f) | |
materialsArray.tofile(f) | |
texFileNamesArray.tofile(f) | |
verticesArray.tofile(f) | |
texCoordArray.tofile(f) | |
normalsArray.tofile(f) | |
facesIndArray.tofile(f) | |
f.close() | |
print("MBM Export done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment