Python API: easy way to get the triangle data of an object
Basically I am frustrated with the convoluted ways one has to write an exporter in. Exporting meshes as triangles is very common, so why isn't there a simple routine that you call on an object that returns all the data you care about, i.e. a list of triangles with all the 3D data we need (normals, UV coordinates, etc.)?
It would work something like this:
triangles = get_triangles_for_object(object, apply_modifiers=True)
And when you are done with it:
free_triangle_list(triangles)
This would drastically simplify the code needed and it could be optimised for speed.
EDIT: as pointed out in a comment by testure, the current way of storing data in loops is interesting because it means you do not have to go through all the available data, nor do you have to generate it. I think this routine could work the same way, where you pass flags to ask for specific data you care about and it returns the equivalent of loops.
2 Comments
"all the data you care about" is going to be pretty different depending on who you talk to. it's already possible to do what you're asking- and it's streamlined for speed by virtue of the fact that it doesn't include "everything" because not everybody needs everything. As an example- I could care less about UVs and Vertex colors. If I'm processing a 4 million triangle STL for manufacturing purposes, all of that extra data is just wasted memory and processing time.
Which is exactly why all of that data is stored in loops- and you can get whatever you need on-demand using evaluated depsgraph data if you need modifiers (or unevaluated data if you don't).
The closer you get to a 'one size fits all single command' the slower and more obtuse it becomes. If there is specific data that you routinely need to access for your use case, you might consider writing a utility function that you can execute in a single line and get all of the data you want back.
'The closer you get to a 'one size fits all single command' the slower and more obtuse it becomes.' I totally agree, I did not have a one-command-only kind of API in mind though, I was thinking of different versions of the routine for different needs, or maybe one routine with flags to set or something. My point is exporting triangles is something obvious people will do with this kind of software, so there should really be an easy way to do it. Maybe you could just reuse the loops idea and return arrays of the data you care about. The fact that it is up to you to prepare meshes to get triangles in a 3D modeling tool just seems absurd to me.