Skip to content

Instantly share code, notes, and snippets.

@emersonmoretto
Created February 29, 2012 17:38
Show Gist options
  • Save emersonmoretto/1942825 to your computer and use it in GitHub Desktop.
Save emersonmoretto/1942825 to your computer and use it in GitHub Desktop.
Blender python script to generate 3D model by vertex
import os
# Output file
file = open(os.path.expanduser("~/Desktop/model3d.mdl"), "w")
model_object = None
model_mesh = None
# Search for the first object of type mesh
for obj in bpy.context.scene.objects:
if obj.type == 'MESH':
model_object = obj
model_mesh = obj.data
# Triangulate
model_object.select = True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.quads_convert_to_tris()
# Write each vertex per line with x,y,z separated by a tab
for face in model_mesh.faces:
for vertex_index in face.vertices:
vertex = model_mesh.vertices[vertex_index]
file.write(str(vertex.co[0]))
file.write('\t')
file.write(str(vertex.co[1]))
file.write('\t')
file.write(str(vertex.co[2]))
file.write('\n')
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment