EihiS

June 26, 2014

Blender 2.69 python script for vertex,normals, and UV coords export

Filed under: Raspberry 3.14, linux — Tags: , , , , , , — admin @ 11:36 am

A simple script that can be run from a ‘text’ window in blender 2.69.
This a raw script,up to you to get what’s useful from it.

Obviously ,the script assumes the mesh is called ‘Cube’.
It writes to the file each vertices composing a face of the mesh, with each vertice’s normal and each face’s normal plus the face’s vertices group number + the face’s material number.
a face’s description is 3 lines, with the start char beein ‘A’,'B’ or ‘C’, corresponding to the 3 vertices of the face.

If a vertex is in multiple groups, the script takes the first group’s number for that vertex.
Groups and Materials output are Numbers, from 0…n , directly linked to the Material or Group’s blender - SLOT , whatever the name is.
Vertices are treated as individual (the real vertices coords are used , assuming the user’s program wich will use this output file will make it’s own ‘indexing’ of vertices wich have same coords  in common )
Finally, faces have to be triangulated :the script outputs only the 3 first vertices of a meshe’s face .

Hope this helps!

#!BPY

import bpy

cubedata = bpy.data.meshes['Cube']
v=cubedata.vertices
f=cubedata.polygons
print("\n\n-------------------------------------\n\n")
out = open("/home/path_to_your_dir/custom_output.txt", "w")
idx=0
for f in cubedata.polygons :
    # FACES MUST BE TRIANGULATED
    g=cubedata.vertices[f.vertices[0]]
    uvx=0
    uvy=0
    if cubedata.uv_layers.active!=None:
        uvx=cubedata.uv_layers.active.data[idx].uv.x
        uvy=cubedata.uv_layers.active.data[idx].uv.y
    else:
        print("UV error,face vertex[0]\n")
    out.write("A=%3.3f %3.3f %3.3f VN=%2.5f %2.5f %2.5f FN=%2.5f %2.5f %2.5f G=%u M=%u U=%2.5f V=%2.5f\n" % (g.co.x,g.co.y,g.co.z,g.normal.x,g.normal.y,g.normal.z,f.normal[0],f.normal[1],f.normal[2],g.groups[0].group,f.material_index,uvx,uvy))
    print("A=%3.3f %3.3f %3.3f VN=%2.5f %2.5f %2.5f FN=%2.5f %2.5f %2.5f G=%u M=%u U=%2.5f V=%2.5f\n" % (g.co.x,g.co.y,g.co.z,g.normal.x,g.normal.y,g.normal.z,f.normal[0],f.normal[1],f.normal[2],g.groups[0].group,f.material_index,uvx,uvy))
    g=cubedata.vertices[f.vertices[1]]
    uvx=0
    uvy=0
    if cubedata.uv_layers.active!=None:
        uvx=cubedata.uv_layers.active.data[idx+1].uv.x
        uvy=cubedata.uv_layers.active.data[idx+1].uv.y
    else:
        print("UV error,face vertex[1]\n")  
    out.write("B=%3.3f %3.3f %3.3f VN=%2.5f %2.5f %2.5f FN=%2.5f %2.5f %2.5f G=%u M=%u U=%2.5f V=%2.5f\n" % (g.co.x,g.co.y,g.co.z,g.normal.x,g.normal.y,g.normal.z,f.normal[0],f.normal[1],f.normal[2],g.groups[0].group,f.material_index,uvx,uvy))
    print("B=%3.3f %3.3f %3.3f VN=%2.5f %2.5f %2.5f FN=%2.5f %2.5f %2.5f G=%u M=%u U=%2.5f V=%2.5f\n" % (g.co.x,g.co.y,g.co.z,g.normal.x,g.normal.y,g.normal.z,f.normal[0],f.normal[1],f.normal[2],g.groups[0].group,f.material_index,uvx,uvy))
    g=cubedata.vertices[f.vertices[2]]
    uvx=0
    uvy=0
    if cubedata.uv_layers.active!=None:
        uvx=cubedata.uv_layers.active.data[idx+2].uv.x
        uvy=cubedata.uv_layers.active.data[idx+2].uv.y
    else:
        print("UV error,face vertex[2]\n")
    out.write("C=%3.3f %3.3f %3.3f VN=%2.5f %2.5f %2.5f FN=%2.5f %2.5f %2.5f G=%u M=%u U=%2.5f V=%2.5f\n" % (g.co.x,g.co.y,g.co.z,g.normal.x,g.normal.y,g.normal.z,f.normal[0],f.normal[1],f.normal[2],g.groups[0].group,f.material_index,uvx,uvy))
    print("C=%3.3f %3.3f %3.3f VN=%2.5f %2.5f %2.5f FN=%2.5f %2.5f %2.5f G=%u M=%u U=%2.5f V=%2.5f\n" % (g.co.x,g.co.y,g.co.z,g.normal.x,g.normal.y,g.normal.z,f.normal[0],f.normal[1],f.normal[2],g.groups[0].group,f.material_index,uvx,uvy))
    #
    idx=idx+3
    #   
out.close()

The output file (ascii) will look like :

A=0.351 -0.679 0.733 VN=0.93976 -0.23081 0.25208 FN=0.88620 -0.21390 0.41097 G=0 M=9 U=0.74219 V=0.25781
B=0.349 -0.221 0.975 VN=0.93954 -0.07318 0.33445 FN=0.88620 -0.21390 0.41097 G=0 M=9 U=0.74219 V=0.50000
C=0.249 -0.311 1.144 VN=0.41011 0.22208 0.88455 FN=0.88620 -0.21390 0.41097 G=0 M=9 U=0.74219 V=0.50000
A=0.245 1.119 -0.397 VN=0.41014 0.89917 0.15235 FN=-0.00437 0.84659 -0.53222 G=0 M=0 U=0.50781 V=0.49219
B=0.246 0.843 -0.835 VN=0.41194 0.25422 -0.87500 FN=-0.00437 0.84659 -0.53222 G=0 M=0 U=0.50781 V=0.74219
C=-0.251 1.116 -0.397 VN=-0.41353 0.89776 0.15146 FN=-0.00437 0.84659 -0.53222 G=0 M=0 U=0.25781 V=0.49219
A=-0.345 -0.957 0.295 VN=-0.93915 -0.32841 0.10056 FN=-0.00010 0.03756 0.99929 G=0 M=9 U=0.00000 V=1.00000
B=-0.245 -1.148 0.302 VN=-0.41090 -0.65740 0.63161 FN=-0.00010 0.03756 0.99929 G=0 M=9 U=0.00000 V=1.00000
C=0.251 -1.146 0.302 VN=0.41246 -0.65545 0.63262 FN=-0.00010 0.03756 0.99929 G=0 M=9 U=0.25781 V=1.00000
A=0.352 -0.954 0.295 VN=0.93982 -0.32621 0.10154 FN=0.88526 -0.46476 0.01753 G=0 M=9 U=0.25781 V=1.00000
B=0.251 -1.146 0.302 VN=0.41246 -0.65545 0.63262 FN=0.88526 -0.46476 0.01753 G=0 M=9 U=0.25781 V=1.00000
C=0.251 -1.165 -0.215 VN=0.41252 -0.70116 -0.58150 FN=0.88526 -0.46476 0.01753 G=0 M=9 U=0.25781 V=0.75000
A=0.247 0.216 -1.166 VN=0.41008 0.58296 -0.70138 FN=0.00010 -0.03762 -0.99929 G=0 M=0 U=1.00781 V=0.75781
314159265358979323846264338327950288
419716939937510582097494459230781640
628620899862803482534211706798214808

cat{ } { post_578 } { } 2009-2015 EIhIS Powered by WordPress