#!/usr/bin/python # -*- coding: utf-8 -*- from __future__ import print_function import mesh_pb2 as pobj def dump_mesh(mesh): """ Dump mesh data up to, at most, 10 vertices and 10 faces. The 10 vert/face limit is simply to limit the amount of data on screen. """ print("Mesh name: " + mesh.name) print("Num. Verts: %d"%(len(mesh.vertices))) print("Num. Faces: %d"%(len(mesh.faces))) for index,vertex in enumerate(mesh.vertices): print("V[%d]: %f %f %f"%(index,vertex.x,vertex.y,vertex.z)) if index == 10: break for index,face in enumerate(mesh.faces): print("f[%d count: %d]: %d %d %d"%(index,face.poly_type,face.vert_index[0],face.vert_index[1],face.vert_index[2])) if index == 10: break def test_protobuf_read(): """ Test protocol buffer reader """ with open("tmp.pb","rb") as fr: mesh = pobj.Mesh() mesh.ParseFromString(fr.read()) dump_mesh(mesh) def test_protobuf_write(): """ Test protocol buffer write Assumes you have a valid `load_mesh` function. """ with open("tmp.pb","wb") as fw: # load vertex and face data however you like verts,faces = load_mesh("test.ply") mesh = pobj.Mesh() mesh.name = "protobj_py_test" for v in verts: #protobuf repeated message fields are RepeatedMessageFields and are created with add() mv = mesh.vertices.add() mv.x = v[0] mv.y = v[1] mv.z = v[2] for f in faces: mf = mesh.faces.add() # protobuf repeated non-message fields are treated as RepeatedScalarFieldContainers # so use 'append', not 'add' mf.vert_index.append(f[0]) mf.vert_index.append(f[1]) mf.vert_index.append(f[2]) test_protobuf_write() test_protobuf_read()