I'm trying to write a script in blender that will take an object and save it to PostgreSQL db. All I need to do is to get the list of vertices per face in a clockwise manner (how postgis knows inside vs outside). I am starting with a simple cube example. The syntax I need for a cube follows:
NOTE: the first node is repeated to close the face
SELECT ST_GeomFromText('POLYHEDRALSURFACE Z(
((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)),
((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)),
((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)),
((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)),
((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)),
((0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1)) )');
So far I can access a list of faces and a list of vertices, how can I get a list of vertices per face (clockwise) so it prints as above. Here is my current effort:
import bpy
ob = bpy.context.active_object
me = ob.data
bm = bmesh.from_edit_mesh(me)
for f in bm.faces:
if f.select:
print(f.index)
for v in bm.verts:
print(v.co.x, v.co.y, v.co.z)
@@@### EDIT ###@@@
OK, so here is where I am now, getting close. I've included rounding to make it easier to understand.
print("SELECT ST_GeomFromText('POLYHEDRALSURFACE Z( ")
#print("="*40) # printing marker
for polygon in current_obj.data.polygons:
verts_in_face = polygon.vertices[:]
#print("face index", polygon.index, end=" ")
#print("normal", polygon.normal)
print ("((", end=" ")
#
for vert in verts_in_face:
print(
str(round(current_obj.data.vertices[vert].co.x, 1)),
str(round(current_obj.data.vertices[vert].co.y, 1)),
str(round(current_obj.data.vertices[vert].co.z, 1)),",", end=" ")
print (")),")
So it still needs the first vertex to be repeated and if it is the last 'print' exclude the last commas. I have commented out the normals indicator incase extra needs to be added to ensure the clockwise construction direction. Result:
(( 1.0 1.0 0.0 , 1.0 0.0 0.0 , 0.0 0.0 0.0 , 0.0 1.0 0.0 , )),
(( 1.0 1.0 1.0 , 0.0 1.0 1.0 , 0.0 0.0 1.0 , 1.0 -0.0 1.0 , )),
(( 1.0 1.0 0.0 , 1.0 1.0 1.0 , 1.0 -0.0 1.0 , 1.0 0.0 0.0 , )),
(( 1.0 0.0 0.0 , 1.0 -0.0 1.0 , 0.0 0.0 1.0 , 0.0 0.0 0.0 , )),
(( 0.0 0.0 0.0 , 0.0 0.0 1.0 , 0.0 1.0 1.0 , 0.0 1.0 0.0 , )),
(( 1.0 1.0 1.0 , 1.0 1.0 0.0 , 0.0 1.0 0.0 , 0.0 1.0 1.0 , )),