process_group_membership(polygon, export_mesh) ####################### End Pre-Processing ########################### #from pprint import pprint #pprint(export_mesh) #################### Use rhom Stub to Create Obn ################## if not False: import sys sys.path.append( '/muse/satishg/learning/soc/obnblender/blender/io_scene_obn') import rhomstub as rhom mesh = rhom.Mesh() for vertex in bmesh.vertices: x, y, z = vertex.co mesh.addPoint(rhom.Point(x, y, z)) for uvtag in export_mesh['uv_layers'].keys(): for uv in export_mesh['uv_layers'][uvtag]['table']: mesh.addTexCoord(uvtag, rhom.TexCoord(uv[0], uv[1])) for group_name in export_mesh['groups']['names']: mesh.addGroup(group_name) for polygon_index, polygon in export_mesh['polygons'].items(): element = mesh.addElement(polygon['vertices']['no']) mesh.setPointIndices(element, polygon['vertices']['array'])
def execute(self, context): object = context.active_object bmesh = object.data mesh = rhom.Mesh() for vertex in bmesh.vertices: x, y, z = vertex.co mesh.addPoint(rhom.Point(x, y, z)) for uvtag in bmesh.uv_layers.keys(): for datum in bmesh.uv_layers[uvtag].data: mesh.addTexCoord(uvtag, rhom.TexCoord(datum.uv.x, datum.uv.y)) for group_name in ("all", "triangleMtl", "triangleDeform"): mesh.addGroup(group_name) if rhom.writeMesh('Foo.obn', mesh) == 0: self.report({"ERROR"}, mesh.exportError) return {'CANCELLED'} _filename = self.directory + object.name _rhom_py_file = _filename + '.py' fd = open(_rhom_py_file, mode='w') # Start writing data to the py file _shebang = '#/usr/bin/python\n' _imports = 'import rhom\nimport rml\n' _mesh = 'mesh = rhom.Mesh()\n' fd.write(_shebang) fd.write(_imports) fd.write(_mesh) # Write the Blender mesh vertices as rhom mesh points for vtx in bmesh.vertices: rml_point = 'rml.geom.Point3({co.x}, {co.y}, {co.z})'.format( co=vtx.co) rmeshpoint = 'mesh.addPoint({0})'.format(rml_point) fd.write(rmeshpoint + '\n') # Write the uv layers # TODO # Pre process groups for later usage poly_group_layer_names = sorted(bmesh.polygon_layers_int.keys()) # The following dictionary contains key, value pairs corresponding to # Element index and a list of Boolean values that specify if an element # belongs to a polyGroup poly_groups_dict = {} for index in range(0, len(bmesh.polygons)): poly_groups_dict[index] = [] for key in poly_group_layer_names: is_present = bool( bmesh.polygon_layers_int[key].data[index].value) poly_groups_dict[index].append(is_present) # Get a list of vertex loops that build elements vertTable = [] for polygon in bmesh.polygons: temp = [] for vertex in polygon.vertices: temp.append(str(vertex)) vertTable.append('[{0}]'.format(','.join(temp))) elementTable = '[{0}]'.format(', '.join(vertTable)) # Create elements from the data above fd.write('for vertices in {0}:\n'.format(elementTable)) fd.write(' element = mesh.addElement(len(vertices))\n') fd.write(' mesh.setPointIndices(element, vertices)\n') # Create empty groups from polygon_layers_int for layername in poly_group_layer_names: fd.write( 'mesh.addGroup("{layername}")\n'.format(layername=layername)) # Create material groups groups_materials = filter(lambda g: g.endswith('Mtl'), poly_group_layer_names) for group_material in groups_materials: fd.write('mesh.addMaterial("{0}")\n'.format('xxx_' + group_material)) # Tag the elements with the polygroups that they belong to for findex in sorted(poly_groups_dict.keys()): poly_layer_status = poly_groups_dict[findex] for index, status in enumerate(poly_layer_status): if status: group_name = poly_group_layer_names[index] fd.write( 'mesh.addElementToGroup(mesh.getElement({0}), "{1}")\n' .format(findex, group_name)) if group_name.endswith('Mtl'): fd.write( 'mesh.setMaterial(mesh.getElement({0}), "{1}")\n'. format(findex, 'xxx_' + group_name)) # Finalize rhom script generation _obn_filename = _filename + '.obn' fd.write("rhom.writeMesh('{0}', mesh)\n".format(_obn_filename)) fd.write("print('ExportOBN:Success. rhom created {0}')".format( _obn_filename)) fd.flush() fd.close() self.report({'OPERATOR'}, ' '.join(['Exported', _rhom_py_file])) if self.dump_only == True: return {'FINISHED'} # Time to run the rhom python script that was generated previously import subprocess process = None data = None program = '/usr/bin/python' process = subprocess.Popen([program, _rhom_py_file], stdout=subprocess.PIPE) while True: import errno try: data = process.communicate() break except Exception as e: if e.errno == errno.EINTR: continue else: break datum, err = data datum = str(datum) print(datum) self.report({'OPERATOR'}, datum) return {'FINISHED'}