import poser, Numeric scene = poser.Scene() def planar_map(verts): """Create a planar map if there are no UVs. Uses z axis.""" bbox = b_box_ock(verts) minx = bbox[0] maxx = bbox[1] sizex = maxx - minx miny = bbox[2] maxy = bbox[3] sizey = maxy - miny tverts = [[] for i in range(len(verts))] for vi in range(len(verts)): vert = verts[vi] u = (vert[0] - minx)/sizex v = (vert[1] - miny)/sizey tverts[vi] = [u,v] return tverts def b_box_ock(vertpos): """ create bounding boxes """ minX = 1000000.0; maxX = -1000000.0 minY = 1000000.0; maxY = -1000000.0 minZ = 1000000.0; maxZ = -1000000.0 for i in range(len(vertpos)): tx = vertpos[i][0] ty = vertpos[i][1] tz = vertpos[i][2] if tx < minX: minX = tx if tx > maxX: maxX = tx if ty < minY: minY = ty if ty > maxY: maxY = ty if tz < minZ: minZ = tz if tz > maxZ: maxZ = tz return [minX,maxX,minY,maxY,minZ,maxZ] def map_object(geom,replace=0): """ Apply planar mapping to the currently selected actor and create a new prop with the altered geometry. """ sets = [i for i in geom.Sets()] polys = [[p.Start(),p.NumVertices()] for p in geom.Polygons()] verts = [[i.X(),i.Y(),i.Z()] for i in geom.Vertices()] sets = Numeric.array(sets,Numeric.Int) polys = Numeric.array(polys,Numeric.Int) verts = Numeric.array(verts,Numeric.Float) tverts = planar_map(verts) tsets = [i for i in sets] tpolys = [[p.Start(),p.NumVertices()] for p in geom.Polygons()] tverts = Numeric.array(tverts,Numeric.Float) tsets = Numeric.array(tsets,Numeric.Int) tpolys = Numeric.array(tpolys,Numeric.Int) newgeom = poser.NewGeometry() newgeom.AddGeneralMesh(polys,sets,verts,tpolys,tsets,tverts) for mat in geom.Materials(): newgeom.AddMaterialName(mat.Name()) for pi in range(geom.NumPolygons()): poly = geom.Polygon(pi) newpoly = newgeom.Polygon(pi) newpoly.SetMaterialIndex(poly.MaterialIndex()) if replace: scene.CurrentActor().SetGeometry(newgeom) scene.CurrentActor().MarkGeomChanged() else: newprop = scene.CreatePropFromGeom(newgeom,"newprop") scene.SelectActor(newprop) scene.DrawAll() # To let the new material names sink in. for mat in geom.Materials(): matname = mat.Name() diffuse = mat.DiffuseColor() newprop.Material(matname).SetDiffuseColor(diffuse[0],diffuse[1],diffuse[2]) newprop.Material(matname).SetTextureMapFileName(mat.TextureMapFileName()) map_object(scene.CurrentActor().Geometry())