Created
April 29, 2015 08:32
-
-
Save anonymous/785522e4bf34f9afafdf to your computer and use it in GitHub Desktop.
test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import * | |
from openctm import * | |
from contextlib import contextmanager | |
import bpy | |
import bmesh | |
def pydata_from_bmesh(bm): | |
v = [v.co[:] for v in bm.verts] | |
# e = [[i.index for i in e.verts] for e in bm.edges[:]] | |
p = [[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_type = [c for v in geom for c in v] | |
blob = Blob(*geom_data_type) | |
return cast(blob, POINTER(T)) | |
ctmDefineMesh = _lib.ctmDefineMesh | |
ctmDefineMesh.argtypes = [ | |
CTMcontext, | |
POINTER(CTMfloat), CTMuint, # * to vertex data & vertex count | |
POINTER(CTMuint), CTMuint, # * to index data & triangle count | |
POINTER(CTMfloat)] # Optional * to surface normals | |
yield ctmDefineMesh | |
yield make_blob | |
def write_ctm(filename, 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(obj): | |
# Get the active mesh | |
me = bpy.context.object.data | |
bm = bmesh.new() | |
bm.from_mesh(me) | |
bmesh.ops.triangulate(bm, faces=bm.faces, quad_method=0, ngon_method=0) | |
geometry = pydata_from_bmesh(bm) | |
write_ctm(filename, *geometry) | |
bm.free() # free and prevent further access |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment