Skip to content

Instantly share code, notes, and snippets.

Forked from anonymous/
Last active August 29, 2015 14:20
Show Gist options
  • 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
# gleaned from
def pydata_from_bmesh(bm):
v = [tuple([:]) 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
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)
def get_triangulated_mesh(out_dir, obj_name):
me =[obj_name].data
bm =
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) # 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