import Numeric, os import poser scene = poser.Scene() act = scene.CurrentActor() class Mesh: def __init__(self,act): self.verts = vertexPos(act.Geometry()) def vertexPos(geom,worldspace=1): """ Pre-fetch vertex coordinates for faster access. Returns a Numeric array of vert coords by vert indices Send worldspace=0 keyword when calling function, to use localspace """ numVerts = geom.NumVertices() verts = [[0.0,0.0,0.0] for i in range(numVerts)] verts = Numeric.array(verts,Numeric.Float) for i in range(numVerts): if worldspace: v = geom.WorldVertex(i) else: v = geom.Vertex(i) verts[i] = [v.X(),v.Y(),v.Z()] return verts def data_list(verts): data = "" for vi in range(len(verts)): v = verts[vi] for coord in range(len(v)): data += str(v[coord]) if coord < len(v)-1: data += " " else: if vi < len(verts)-1: data += "\n" return data def Qhull(mesh): """ Path handling for popen seems to be broken. It breaks off the submitted path with any whitespace character. Html whitespace formatting isn't the answer.... Why does it do this? Workaround: place the qconvex.exe directly in C:\. """ #qhull_path = os.path.dirname(poser.AppLocation())#.replace(" ","%20") #qhull_path = "" #qhull_path = "C:\\Program Files\\e frontier\\Poser 7\\Runtime\\Python\\poserScripts\\TDMT\\TDMT_script" qhull_path = "C:\\" if not os.path.exists(os.path.join(qhull_path,"qconvex.exe")): print "qconvex.exe not found." print "Download Qhull at http://www.qhull.org/" print "Place qconvex.exe directly in C:\\" return instring = data_list(mesh.verts) #compile the string instring = "3\n%s\n%s" %(len(mesh.verts),instring) #args = " o" #.off format returned as output #args = " o TO result.txt" # .off format to be written as file "result.txt" args = " m" # Mathematica format closely resembles AddTriangle or AddPolygon format inp, output = os.popen4(os.path.join(qhull_path, "qconvex.exe")+args) inp.write(instring) inp.close() data = output.readlines() #What a klunky mathematica parser, eh? remove = ["\n","[","]"," ",""] data2 = "" for line in data: for char in remove: line = line.replace(char,"") line = line.replace("{{","{") line = line.replace("}}","}") line = line.replace("{","[") line = line.replace("}","]") line = line.replace("]]","]") if line != "" and line != "[": data2 += line data2 = data2.replace("Polygon",",Polygon") data2 = data2.replace(",,Polygon",",Polygon") lines = data2.split(",Polygon") lines.remove(lines[0]) lines2 = [] for line in lines: line = line.replace("],[","##") line = line.replace("[","") line = line.replace("]","") line = line.split("##") lines2.append(line) lines = [] for line in lines2: poly = [] for p in line: p = p.split(",") p2 = [float(i) for i in p] poly.append(p2) lines.append(poly) #finally, lines is a list in which each line is a polygon listing the vertex coords for that poly. hullgeom = poser.NewGeometry() for line in lines: line.reverse() #fix the winding order hullgeom.AddPolygon(Numeric.array(line,Numeric.Float)) hullgeom.Weld() # weld the geometry hullObj = scene.CreatePropFromGeom(hullgeom, "ConvexHull") scene.ProcessSomeEvents() scene.DrawAll() mesh = Mesh(act) Qhull(mesh) del mesh