3
$\begingroup$

How would I make a python script that converts the selected object into a CSV of triangles formatted as such:

x | y | z | #point 1 of triangle 1
x | y | z | #point 2 of triangle 1
x | y | z | #point 3 of triangle 1
x | y | z | #point 1 of triangle 2
x | y | z | #point 2 of triangle 2
x | y | z | #point 3 of triangle 2

I so far have created a program that converts a mesh into triangles and then saves it as a CSV. However, it doesn't make separate triangles.

$\endgroup$

1 Answer 1

3
$\begingroup$
output_file = r'E:/out.txt'
text = "point {} of triangle {}"

from bpy import context as C

me = C.object.data

with open(output_file, 'w') as f:
    for triangle in me.loop_triangles:
        for i, vert_i in enumerate(triangle.vertices, start=1):
            vert = me.vertices[vert_i]
            print(*vert.co, text.format(i, triangle.index+1), sep=' | ', file=f)

For the default cube prints:

1.0 | 1.0 | 1.0 | point 1 of triangle 1
-1.0 | 1.0 | 1.0 | point 2 of triangle 1
-1.0 | -1.0 | 1.0 | point 3 of triangle 1
1.0 | 1.0 | 1.0 | point 1 of triangle 2
-1.0 | -1.0 | 1.0 | point 2 of triangle 2
1.0 | -1.0 | 1.0 | point 3 of triangle 2
1.0 | -1.0 | -1.0 | point 1 of triangle 3
1.0 | -1.0 | 1.0 | point 2 of triangle 3
-1.0 | -1.0 | 1.0 | point 3 of triangle 3
1.0 | -1.0 | -1.0 | point 1 of triangle 4
-1.0 | -1.0 | 1.0 | point 2 of triangle 4
-1.0 | -1.0 | -1.0 | point 3 of triangle 4
-1.0 | -1.0 | -1.0 | point 1 of triangle 5
-1.0 | -1.0 | 1.0 | point 2 of triangle 5
-1.0 | 1.0 | 1.0 | point 3 of triangle 5
-1.0 | -1.0 | -1.0 | point 1 of triangle 6
-1.0 | 1.0 | 1.0 | point 2 of triangle 6
-1.0 | 1.0 | -1.0 | point 3 of triangle 6
-1.0 | 1.0 | -1.0 | point 1 of triangle 7
1.0 | 1.0 | -1.0 | point 2 of triangle 7
1.0 | -1.0 | -1.0 | point 3 of triangle 7
-1.0 | 1.0 | -1.0 | point 1 of triangle 8
1.0 | -1.0 | -1.0 | point 2 of triangle 8
-1.0 | -1.0 | -1.0 | point 3 of triangle 8
1.0 | 1.0 | -1.0 | point 1 of triangle 9
1.0 | 1.0 | 1.0 | point 2 of triangle 9
1.0 | -1.0 | 1.0 | point 3 of triangle 9
1.0 | 1.0 | -1.0 | point 1 of triangle 10
1.0 | -1.0 | 1.0 | point 2 of triangle 10
1.0 | -1.0 | -1.0 | point 3 of triangle 10
-1.0 | 1.0 | -1.0 | point 1 of triangle 11
-1.0 | 1.0 | 1.0 | point 2 of triangle 11
1.0 | 1.0 | 1.0 | point 3 of triangle 11
-1.0 | 1.0 | -1.0 | point 1 of triangle 12
1.0 | 1.0 | 1.0 | point 2 of triangle 12
1.0 | 1.0 | -1.0 | point 3 of triangle 12
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .