1
$\begingroup$

I am using the following exporter:

mesh = bpy.data.meshes['Plane']
for f in mesh.polygons:
    for v in f.vertices:
        print("%f %f %f %f %f %f" % (mesh.vertices[v].co[0], mesh.vertices[v].co[2], mesh.vertices[v].co[1], f.normal[0], f.normal[2], f.normal[1]))
    print("\n")

The 'Plane' mesh is the default plane that appears with SHIFT+A -> Mesh -> Plane after scaling it by 20 and dividing it in triangles. When I run the above script, the output is:

1.000000 0.000000 -1.000000 -0.000000 1.000000 0.000000
-1.000000 0.000000 1.000000 -0.000000 1.000000 0.000000
-1.000000 0.000000 -1.000000 -0.000000 1.000000 0.000000


1.000000 0.000000 -1.000000 -0.000000 1.000000 0.000000
1.000000 0.000000 1.000000 -0.000000 1.000000 0.000000
-1.000000 0.000000 1.000000 -0.000000 1.000000 0.000000

As you can see, the scaling information is lost. To make sure, here are the relevant parts of the OBJ export by default OBJ exporter:

v -20.000000 0.000000 20.000000
v 20.000000 0.000000 20.000000
v -20.000000 0.000000 -20.000000
v 20.000000 0.000000 -20.000000
vn 0.0000 1.0000 0.0000

How can I get the correct scaling information? Do I need to retrieve the scaling factor/matrix somehow and transform each vertex manually?

$\endgroup$
0

1 Answer 1

1
$\begingroup$

Unless you apply scale, rotations, transformation to the mesh the vert coordinates are "local". Eg for the default plane combinations of -1, 1 for x, y and 0 for z. If it is the mesh of an object that is scaled by 20 (with no rotation or transformation) then the global coords are 20 times the local.

To get the global coord multiply by the matrix_world of the object.

ob = context.scene.objects.get("Plane")
if ob:
    mesh = ob.data
    # global vert coordinates
    for v in mesh.vertices:
        print(ob.matrix_world * v.co)

btw that print statement is awful..

for i in f:
    v = mesh.vertices[i]
    print("%f %f %f" % v.co.xzy[:], end=" ")
    print("%f %f %f" % f.normal.xzy[:])

Also look at bpy_extras.io_utils.axis_conversion it gives you the matrix to convert from -Z_FORWARD Y_UP to -Y_FORWARD, Z_UP and other combinations.

$\endgroup$
0

You must log in to answer this question.

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