def export_selected_mesh(meshname): print("Exporting [{0}]...".format(meshname)) selected = pm.ls(selection=True) if len(selected) != 1: gui_message = "CowByte: Failed to export - nothing/or too many selected! One mesh at a time!" show_message_popup("Export Failure", gui_message) print(gui_message) return False # TODO: error checking, this is assuming selected has first child as Mesh. mesh = pm.listRelatives(selected[0])[0] pm.polyTriangulate( mesh) # triangulate the mesh first, since CBE only takes triangles. faces = mesh.faces export_meshfile(meshname, export_folder + meshname + ".mesha") export_pos_buf(faces, export_folder + meshname + "_pos.bufa") export_index_buf(faces, export_folder + meshname + "_index.bufa") export_normal_buf(faces, export_folder + meshname + "_normal.bufa") export_uv(faces, export_folder + meshname + "_uv.bufa") print("Finished exporting [{0}].".format(meshname)) show_message_popup( "Export Success", "Files have been exported to {0}.".format(export_folder)) return True
def __init__(self, mesh): # HACK: I should really do the triangulation in memory rather than changing the scene. # However, this is fine for now. native.polyTriangulate(mesh) self.vertices = tuple(tuple(v) for v in mesh.getPoints()) self.indices = tuple(self._compute_indices(mesh)) self.uvs = tuple(self._compute_uvs(mesh)) self.normals = tuple(self._compute_normals(mesh))
def writeMeshListToFile(meshList, spaceType, grouping, materials, myFile): nrOfPos = 0 nrOfUVs = 0 nrOfNor = 0 for obj in meshList: #Triangulate mesh so that the object(s) can be imported correctly later pm.polyTriangulate(obj) myFile.write('g default\r\n') #I don't know what this is for #Save all positions for the mesh for v in obj.vtx: pos = v.getPosition(space=spaceType) myFile.write('v ' + str(round(pos.x, 6)) + ' ' + str(round(pos.y, 6)) + ' ' + str(round(pos.z, 6)) + '\r\n') #Save all UVs for the mesh U, V = obj.getUVs() UVs = zip(U, V) for uv in UVs: myFile.write('vt ' + str(round(uv[0], 6)) + ' ' + str(round(uv[1], 6)) + '\r\n') #Save all normals for the mesh for n in obj.getNormals(): myFile.write('vn ' + str(round(n.x, 6)) + ' ' + str(round(n.y, 6)) + ' ' + str(round(n.z, 6)) + '\r\n') #Write this here if grouping is true if grouping is True: myFile.write('g ' + obj + '\r\n') #Write this here if materials is true if materials is True and len(obj.shadingGroups()) > 0: myFile.write('usemtl ' + obj.shadingGroups()[0] + '\r\n') #Match all values so that they together build triangles #Triangles are described like v/vt/vn v/vt/vn v/vt/vn for f in obj.faces: v_idxs = [v + 1 + nrOfPos for v in f.getVertices()] t_idxs = [f.getUVIndex(i) + 1 + nrOfUVs for i in range(3)] n_idxs = [f.normalIndex(i) + 1 + nrOfNor for i in range(3)] s = 'f ' + str(v_idxs[0]) + '/' + str(t_idxs[0]) + '/' + str( n_idxs[0]) + ' ' s += str(v_idxs[1]) + '/' + str(t_idxs[1]) + '/' + str( n_idxs[1]) + ' ' s += str(v_idxs[2]) + '/' + str(t_idxs[2]) + '/' + str(n_idxs[2]) myFile.write( s + '\r\n') #The result here describes a triangle is a triangle myFile.write( '\r\n') #To separate info from this mesh from the next one #Increase the nr of pos, uvs & normals for the next mesh nrOfPos += len(obj.vtx) nrOfUVs += len(UVs) nrOfNor += len(obj.getNormals())
def duplicate_triangulate_mesh(skinned_mesh, dup_namespace=None, dup_parent=None): dup_namespace = dup_namespace or pm.namespaceInfo(currentNamespace=True) with nsutils.preserve_namespace(dup_namespace): dup_skinned_mesh_tri = nsutils.duplicate_to_namespace( skinned_mesh, dup_namespace=dup_namespace, dup_parent=dup_parent)[0] pm.polyTriangulate(dup_skinned_mesh_tri, ch=True) pm.delete(dup_skinned_mesh_tri, constructionHistory=True) dup_skin_cluster = bind_mesh_like_mesh(skinned_mesh, dup_skinned_mesh_tri) copy_weights(skinned_mesh, dup_skinned_mesh_tri) return dup_skinned_mesh_tri, dup_skin_cluster
def make_bake_mesh( meshes, name = '' ): """ Turn one or more meshes into a single mesh for export to xNormal """ arg_string = '' meshes = list( set( meshes ) ) pmc.select( clear = True ) if len( meshes ) == 0: return False if len( meshes ) > 1: if name == '': name = 'bake_mesh' merged = pmc.polyUnite( meshes, ch = False, name = name ) else: merged = meshes[ 0 ] tr = pmc.polyTriangulate( merged, ch = False) return merged
def export(object_name, start, end): # select to export select_poly_list = pm.ls(selection=True) poly_num = len(select_poly_list) # triangulate for i in xrange(poly_num): poly_name = str(select_poly_list[i]) pm.polyTriangulate(poly_name) # select polygon pm.select(select_poly_list) # export to obj for time in xrange(int(start), int(end) + 1): pm.currentTime(time) obj_name = os.path.join( OBJPATH, '{name}_tri.{num:04}.obj'.format(name=object_name, num=time)) pm.exportSelected(obj_name, type='OBJexport')
def export_fbx(force, transf, triangulate, smooth): initial_selection = pmc.ls(sl=True) # Duplicate and triangulate if wanted dup = None if smooth and triangulate: dup = pmc.duplicate(transf) pmc.polySmooth(dup, mth=0, sdt=2, ovb=1, ofb=1, ofc=0, ost=0, ocr=0, dv=2, bnr=1, c=1, kb=1, ksb=1, khe=0, kt=1, kmb=1, suv=1, peh=0, sl=1, dpe=1, ps=0.1, ro=1, ch=1) pmc.polyTriangulate(dup) pmc.select(dup) elif smooth: dup = pmc.duplicate(transf) pmc.polySmooth(dup, mth=0, sdt=2, ovb=1, ofb=1, ofc=0, ost=0, ocr=0, dv=2, bnr=1, c=1, kb=1, ksb=1, khe=0, kt=1, kmb=1, suv=1, peh=0, sl=1, dpe=1, ps=0.1, ro=1, ch=1) pmc.select(dup) elif triangulate: dup = pmc.duplicate(transf) pmc.polyTriangulate(dup) pmc.select(dup) else: pmc.select(transf) export_path = build_export_path(transf=transf, extension=".fbx") try: pmc.exportSelected(export_path, force=force, preserveReferences=True, type="FBX export") success = True except RuntimeError as e: logger.error("Could not export node {}".format(transf)) logger.error(e) success = False # delete triangulated duplicate again (if created) if dup: pmc.delete(dup) pmc.select(initial_selection) return success
def export_obj(force, transf, triangulate, smooth): initial_selection = pmc.ls(sl=True) # Duplicate and triangulate if wanted dup = None if smooth and triangulate: dup = pmc.duplicate(transf) pmc.polySmooth(dup, mth=0, sdt=2, ovb=1, ofb=1, ofc=0, ost=0, ocr=0, dv=2, bnr=1, c=1, kb=1, ksb=1, khe=0, kt=1, kmb=1, suv=1, peh=0, sl=1, dpe=1, ps=0.1, ro=1, ch=1) pmc.polyTriangulate(dup) pmc.select(dup) elif smooth: dup = pmc.duplicate(transf) pmc.polySmooth(dup, mth=0, sdt=2, ovb=1, ofb=1, ofc=0, ost=0, ocr=0, dv=2, bnr=1, c=1, kb=1, ksb=1, khe=0, kt=1, kmb=1, suv=1, peh=0, sl=1, dpe=1, ps=0.1, ro=1, ch=1) pmc.select(dup) elif triangulate: dup = pmc.duplicate(transf) pmc.polyTriangulate(dup) pmc.select(dup) else: pmc.select(transf) export_path = build_export_path(transf=transf, extension=".obj") try: exported_file = pmc.exportSelected( export_path, force=force, preserveReferences=True, type="OBJExport", options="groups=1;ptgroups=1;materials=1;smoothing=1;normals=1") # MAYA BUG WORKAROUND (.obj extension missing) if not exported_file[-4:] == ".obj": os.rename(exported_file, export_path) os.rename(exported_file + "mtl", exported_file + ".mtl") success = True except RuntimeError as e: logger.error("Could not export node {}".format(transf)) logger.error(e) success = False # delete triangulated duplicate again (if created) if dup: pmc.delete(dup) pmc.select(initial_selection) return success
def doSelInvNRMFace( *args ): print "start!" # pm.delete( pm.ls(sl=True), ch=1 ) selShape = pm.ls( sl=True, dag=True, type='shape' ) faceInvertNRM = [] offset = pm.floatFieldGrp( 'valOffset', q=True, value1=True ) _x = pm.floatFieldGrp( 'valDir', q=True, value1=True ) _y = pm.floatFieldGrp( 'valDir', q=True, value2=True ) _z = pm.floatFieldGrp( 'valDir', q=True, value3=True ) testRayDir = dt.Vector(_x, _y, _z) for shape in selShape: ## test if selected object type is "mesh" if( pm.nodeType(shape) != 'mesh' ): pm.confirmDialog( t="Error", message= shape.getParent() + " is not a mesh object! ", icon='critical' ) return 0 #pm.delete( shape, ch=1 ) maxValue = float(len(shape.faces)) pm.progressWindow( title='normal test Calculation', progress=0, maxValue=maxValue, isInterruptable=True, status='calculating: 0%' ) for i, face in enumerate(shape.faces): try: pm.progressWindow( edit=True, progress=i, status=('calculating: ' + str( math.ceil( 100 * i/ maxValue) ) + '%') ) ## ESC to cancel progress windows if pm.progressWindow( query=True, isCancelled=True ): pm.progressWindow(endProgress=1) break curr_face = face obj_faces = shape.faces del_tmp_obj = [] #// Test if face is NGon if face.numTriangles() > 2: tmp_obj = pm.duplicate( selShape, rr=True )[0] del_face_id = [ id for id in range( len(obj_faces) ) if id != i ] pm.delete( tmp_obj.getShape().f[del_face_id] ) #// triangulate N-Gon pm.polyTriangulate( tmp_obj, ch=False ) #// curr_face = tmp_obj.getShape().f[0] #// del_tmp_obj.append(tmp_obj) pm.select( curr_face, r=1) cenPos = getFaceCenter() faceNRM = curr_face.getNormal('world') offsetPos = cenPos + faceNRM * offset #testObj = pm.polySphere( r=5, sx=4, sy=4 ) #testObj[0].setTranslation(offsetPos) ## test if point inside object testInside = isPointInsdeObject( shape, offsetPos, testRayDir ) pm.delete(del_tmp_obj) if( testInside ): faceInvertNRM.append( face ) except: continue pm.progressWindow(endProgress=1) pm.select( faceInvertNRM, r=1 ) return 1