Hatena::ブログ(Diary)

Andy Roid’s Memorandum

2017-09-13

simple text exporter of vertices for Blender Python for CPP

a small modification of the last snippet for C++.

import bpy
import bmesh

def up(lineNum, v, f):
    for e in v.link_edges:
        ov = e.other_vert(v)
        if ov not in verts:
            verts.append(ov)
            lineNum = up(lineNum, ov, f)
    if lineNum%10 == 0:
        f.write("// line %d\n" %lineNum)            
    f.write("%f, %f, %f,\n"% (v.co.x, v.co.y, v.co.z))
    return lineNum+1
    
def down(lineNum, v, f):
    if lineNum%10 == 0:
        f.write("// line %d\n" %lineNum)            
    f.write("%f, %f, %f,\n"% (v.co.x, v.co.y, v.co.z))
    for e in v.link_edges:
        ov = e.other_vert(v)
        if ov not in verts:
            verts.append(ov)
            lineNum = down(lineNum, ov, f)
    return lineNum+1
    
obj = bpy.context.active_object
bm = bmesh.from_edit_mesh(obj.data)

verts = []

def main():
    lineNum = 0
    f = open("Obj.cpp", "w")
    f.write("GLfloat Obj::vtx[] = {\n")
    v0 = bm.verts[0]
    verts.append(v0)
    e0 = v0.link_edges
    u = e0[0].other_vert(v0)
    verts.append(u)
    lineNum = up(lineNum, u, f)
    if lineNum%10 == 0:
        f.write("// line %d\n" %lineNum)            
    f.write("%f, %f, %f,\n"% (v0.co.x, v0.co.y, v0.co.z))
    lineNum += 1
    if len(e0)>1:
        d = e0[1].other_vert(v0)
        verts.append(d)
        lineNum = down(lineNum, d, f)
    f.write("};\n")
    f.close()

はてなユーザーのみコメントできます。はてなへログインもしくは新規登録をおこなってください。

トラックバック - http://d.hatena.ne.jp/andyroid/20170913/1505300227