Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/ctm_export.py
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/e41cc3f7bc8621ff495f to your computer and use it in GitHub Desktop.
Save zeffii/e41cc3f7bc8621ff495f to your computer and use it in GitHub Desktop.
note..this doen't work yet. go away
import os
from ctypes import *
from ctm.openctm import *
from contextlib import contextmanager
import bpy
import bmesh
# http://openctm.sourceforge.net/
# gleaned from http://prideout.net/blog/?p=44
def pydata_from_bmesh(bm):
v = [tuple(v.co[:]) for v in bm.verts]
# e = [[i.index for i in e.verts] for e in bm.edges[:]]
p = [tuple([i.index for i in p.verts]) for p in bm.faces[:]]
return v, p
@contextmanager
def ctm_features():
def make_blob(geom, T):
"""Convert into a ctypes flat pointer-to-array"""
size = len(geom) * len(geom[0])
Blob = T * size
geom_data = [c for v in geom for c in v]
blob = Blob(*geom_data)
return cast(blob, POINTER(T))
yield ctmDefineMesh, make_blob
def write_ctm(filename, verts, faces):
print(verts, '||', faces)
with ctm_features() as (ctmDefineMesh, make_blob):
pVerts = make_blob(verts, c_float)
pFaces = make_blob(faces, c_uint)
pNormals = POINTER(c_float)()
ctm = ctmNewContext(CTM_EXPORT)
ctmDefineMesh(ctm, pVerts, len(verts), pFaces, len(faces), pNormals)
ctmSave(ctm, filename)
ctmFreeContext(ctm)
def get_triangulated_mesh(out_dir, obj_name):
me = bpy.data.objects[obj_name].data
bm = bmesh.new()
bm.from_mesh(me)
bmesh.ops.triangulate(bm, faces=bm.faces, quad_method=0, ngon_method=0)
filename = obj_name + '.ctm'
full_path = os.path.join(out_dir, filename)
geometry = pydata_from_bmesh(bm)
write_ctm(filename, *geometry)
bm.free() # free and prevent further access
out_dir = "/home/zeffii/Desktop"
obj_name = "Suzanne"
get_triangulated_mesh(out_dir, obj_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment