Triangulate mesh before export


I’m writing a simple export script to export mesh data in a custom format. The issue I have is that I need all faces to be triangles in my custom format, but blender can store faces as quads (not sure if n-gons are supported). I would like my export script to

  • copy the mesh data to some temporary mesh
  • triangulate that temp mesh
  • export with the temp mesh
  • delete the temp mesh

I was wondering if someone could help me out with the best way to create a copy of the mesh data mess around with without dirtying the scene being exported?

Something like
temp = meshToExport.duplicate()
temp.triangulate() (i’ve noticed there’s a bpy.ops.mesh.quads_convert_to_tris() command)
…do my stuff…

Thanks for your help!

Check out the “Object.to_mesh” function. This will create a duplicate of the mesh that is attached to your Object and allow you to apply modifiers to it. You can then use the standard operations to triangulate it or just turn the quads to triangles yourself. Check out the OBJ exporter in your install. It does this this functionality so it is a good way to see how it is done. Find it under your “<blender version>\scripts\addons\io_scene_obj\”

Hey kastoria, took a look at export_obj like you suggested and it made me realise I could just transform each quad into 2 tris myself on the fly. Much cleaner than copying the mesh and relying on blender. Thanks so much for your help!

Another plus of doing the triangles yourself is you have complete control of where the “seam” goes if you care. Glad I could help!