I want to write an add-on script for a custom format. I have 99% of the boilerplate by finding a similar format's export and modifying it heavily.
Here is a snippet from my current script.
#open the file for writing
file = open(filepath, "wb")
#get script name from the filepath - may want to check if this is blank at some point int he future?
base_name = os.path.basename(filepath)
script_name = os.path.splitext(base_name)[0]
#branch the output here, depending on what the user wants to export
#model output
model_output_string = '';
model_type_num = 4;
vert_count = 0;
#build the model into a string, then write that to the file later
#start writing the mesh data to the file
model_type_num = 4;
if len(mesh.uv_textures) > 0:
#if the model has a UV map
uv_layer = mesh.tessface_uv_textures.active
for face in mesh.tessfaces:
faceUV = uv_layer.data[face.index]
i=0
for index in face.vertices:
if len(face.vertices) == 3:
vert = mesh.vertices[index]
model_output_string += '%s %s %s ' % (integer_to_string(vert.co.x), integer_to_string(vert.co.y), integer_to_string(vert.co.z))
model_output_string += '%s %s\n' % (integer_to_string(faceUV.uv[i][0]), integer_to_string(1-faceUV.uv[i][1]))
vert_count+=1
i+=1
else:
#else if the model has no UV map
uv_layer = mesh.tessface_uv_textures.active
for face in mesh.tessfaces:
for index in face.vertices:
if len(face.vertices) == 3:
vert = mesh.vertices[index]
model_output_string += '%s %s %s ' % (integer_to_string(vert.co.x), integer_to_string(vert.co.y), integer_to_string(vert.co.z))
model_output_string += '; ;\n'
vert_count+=1
writeString(file, str(vert_count) + '\n')
writeString(file, model_output_string)
#end the model output
#output the file and close it for editing
file.flush()
file.close()
Currently it takes every triangle in the scene and generates a file with the following format:
<number of triangles>
<triangle1.vertex1.x> <triangle1.vertex1.y> <triangle1.vertex1.z> <triangle1.vertex1.u> <triangle1.vertex1.v>
<triangle1.vertex2.x> <triangle1.vertex2.y> <triangle1.vertex2.z> <triangle1.vertex2.u> <triangle1.vertex2.v>
<triangle1.vertex3.x> <triangle1.vertex3.y> <triangle1.vertex3.z> <triangle1.vertex3.u> <triangle1.vertex3.v>
.
.
.
<triangleN.vertexN.x> <triangleN.vertexN.y> <triangleN.vertexN.z> <triangleN.vertexN.z> <triangleN.vertexN.v>
This part works fine. The issue is that I also need to save the texture data as well.
Each triangle has a texture, at least, assuming the model being saved is textured. We'll go under the assumption that the models are textured when being saved. It will make my work easier. The format I want to save it in is of the following format. The format dictates the need for two files.
The geometry file:
<number of triangles>
<number of texture batches>
<triangles within batch 1> <texture id of batch 1>
<triangles within batch 2> <texture id of batch 2>
.
.
.
<triangles within batch N> <texture id of batch N>
<triangle1.vertex1.x> <triangle1.vertex1.y> <triangle1.vertex1.z> <triangle1.vertex1.u> <triangle1.vertex1.v>
<triangle1.vertex2.x> <triangle1.vertex2.y> <triangle1.vertex2.z> <triangle1.vertex2.u> <triangle1.vertex2.v>
<triangle1.vertex3.x> <triangle1.vertex3.y> <triangle1.vertex3.z> <triangle1.vertex3.u> <triangle1.vertex3.v>
.
.
.
<triangleN.vertexN.x> <triangleN.vertexN.y> <triangleN.vertexN.z> <triangleN.vertexN.z> <triangleN.vertexN.v>
The texture file:
<number of textures>
<texture filename 1>
<texture filename 2>
.
.
.
<texture filename N>
I have no clue on how to get the filename of the texture for each triangle. What I would like is to be able to take each triangle as I iterate through them and put the filename of the texture associated with that triangle into an array. I can then take that array myself and make sure none of the texture ids are duplicated and that any sequential faces with the same texture get into the same branch. A later "upgrade" might be to take the triangles and sort them into batches so that no texture id is recycled, but that can also be done in the program they are being loaded into (and it would be way easier at that end then in the python model exporter).
I keep hearing something about texture slots material, but honestly I have no clue what that means at all other than that in computer graphics the material dictates data regarding the physical roughness and smoothness of some surface (aka polygon aka triangle). I do not need anything from the material other than the filename, so a solution that does not make it convenient to go back and add other properties to this export is perfectly acceptable. There will not be any support added later for more material stuff. The software this is being loaded into does not support such data, PERIOD.
For those wondering I'm on blender v2.79 as reported by the software. If a convenient solution requires an upgrade, that is perfectly acceptable. I am only on 2.79 because I don't use blender really at all and I just haven't yet bothered to go download the new version to get feature upgrades I'll probably never use.
I've also heard that supposedly this code does not grab everything in the scene. I'm not familiar enough to know whether this is the case or not. If this is the case please let know in the comments. I can make a separate question to look into getting that corrected (assuming I can't fix it myself).
.obj
file (and corresponding.mtl
file)? It's almost exactly like the format you've described here, just withxyz
on separate lines fromuv
. The texture filenames are in "materials", and materials are assigned to faces. $\endgroup$