def main(argv=None): if argv is None: argv = sys.argv parser = OptionParser() parser.add_option("-n", "--normals", dest="normals", help="regenerate normals with provided degree smoothing") parser.add_option("-b", "--binormals", dest="binormals", default=False, help="make binormals") parser.add_option("-c", "--coordinate-system", dest="coordinate", help="specific coordinate system") parser.add_option("-s", "--show", action="store_false", dest="show", default=False, help="show in pview") (options, args) = parser.parse_args() for infile in args: try: if ".obj" not in infile: print("WARNING", infile, "does not look like a valid obj file") continue obj = ObjFile(infile) egg = obj.toEgg(verbose=False) f, e = os.path.splitext(infile) outfile = f + ".egg" if options.normals is not None: egg.recomputeVertexNormals(float(options.normals)) if options.binormals: egg.recomputeTangentBinormal(GlobPattern("")) if options.coordinate is not None: a = options.coordinate if a == 'z-up' or a == 'z-up-right': coordSys = CSZupRight elif a == 'z-up-left': coordSys = CSZupLeft elif a == 'y-up' or a == 'y-up-right': coordSys = CSYupRight elif a == 'y-up-left': coordSys = CSYupLeft else: raise Exception('Unsupported coordinate system: %s' % (a)) egg.setCoordinateSystem(coordSys) egg.removeUnusedVertices(GlobPattern("")) #egg.triangulatePolygons(EggData.TConvex & EggData.TPolygon) #egg.recomputePolygonNormals() egg.writeEgg(Filename(outfile)) if options.show: os.system("pview " + outfile) except Exception as e: print(e) return 0
def main(argv=None): objfiles = [ f for f in os.listdir('./') if os.path.isfile(os.path.join('./', f)) and f.endswith(".obj") ] if argv is None: argv = sys.argv try: opts, args = getopt.getopt(argv[1:], "hn:bs", ["help", "normals", "binormals", "show"]) except getopt.error as msg: print(msg) print(__doc__) show = False for (o, a) in opts: if o in ("-h", "--help"): print(__doc__) elif o in ("-s", "--show"): show = True # for infile in args: for infile in objfiles: print("Working with : ", infile) obj = ObjFile(infile) egg = obj.toEgg() (f, e) = os.path.splitext(infile) outfile = f + ".egg" for o, a in opts: if o in ("-n", "--normals"): egg.recomputeVertexNormals(float(a)) elif o in ("-b", "--binormals"): egg.recomputeTangentBinormal(GlobPattern("")) egg.removeUnusedVertices(GlobPattern("")) if True: egg.triangulatePolygons(EggData.TConvex & EggData.TPolygon) if True: egg.recomputePolygonNormals() egg.writeEgg(Filename(outfile)) if show: os.system("pview " + outfile)
def DisplacementUVSphere(radius, heightmap, scale, rings=5, sectors=5, inv_texture_u=False, inv_texture_v=True): data = EggData() pool = EggVertexPool('pool') vertices = [] data.addChild(pool) R = 1. / (rings) S = 1. / (sectors) for r in range(0, rings + 1): for s in range(0, sectors + 1): cos_s = cos(2 * pi * s * S + pi) sin_s = sin(2 * pi * s * S + pi) sin_r = sin(pi * r * R) cos_r = cos(pi * r * R) x = cos_s * sin_r y = sin_s * sin_r z = cos_r vertex = EggVertex() u = s * S v = r * R height = radius + heightmap.get_height_uv(u, v) * scale vertex.setPos(LPoint3d(x * height, y * height, z * height)) if inv_texture_v: v = 1.0 - v if inv_texture_u: u = 1.0 - u vertex.setUv(LPoint2d(u, v)) pool.addVertex(vertex) vertices.append(vertex) index = 0 for r in range(0, rings): for s in range(0, sectors): poly = EggPolygon() data.addChild(poly) poly.addVertex(vertices[index + sectors + 1]) poly.addVertex(vertices[index]) poly.addVertex(vertices[index + sectors]) poly = EggPolygon() data.addChild(poly) poly.addVertex(vertices[index + sectors + 1]) poly.addVertex(vertices[index + 1]) poly.addVertex(vertices[index]) index += 1 data.removeUnusedVertices(True) data.recomputeVertexNormals(45) data.recomputeTangentBinormal(GlobPattern("")) node = loadEggData(data) path = NodePath(node) path.flattenStrong() return path
def obj2bam(infile, outfile, coordinateSystem='z-up', recomputeVertexNormals=False, recomputeTangentBinormal=False, recomputePolygonNormals=False, triangulatePolygons=False, degreeSmoothing=30.0): if coordinateSystem == 'z-up' or coordinateSystem == 'z-up-right': coordSys = CSZupRight elif coordinateSystem == 'z-up-left': coordSys = CSZupLeft elif coordinateSystem == 'y-up' or coordinateSystem == 'y-up-right': coordSys = CSYupRight elif coordinateSystem == 'y-up-left': coordSys = CSYupLeft else: raise Exception('Unsupported coordinate system: %s' % (coordinateSystem)) os.chdir(os.path.dirname(infile)) obj = ObjFile(infile) egg = obj.toEgg() egg.setCoordinateSystem(coordSys) egg.removeUnusedVertices(GlobPattern("")) if recomputeVertexNormals: egg.recomputeVertexNormals(float(degreeSmoothing)) if recomputeTangentBinormal: egg.recomputeTangentBinormal(GlobPattern("")) if recomputePolygonNormals: egg.recomputePolygonNormals() if triangulatePolygons: egg.triangulatePolygons(EggData.TConvex & EggData.TPolygon) np = NodePath(loadEggData(egg)) np.writeBamFile(outfile) np.removeNode()
def main(argv=None): if argv is None: argv = sys.argv try: opts, args = getopt.getopt(argv[1:], "hn:bs", ["help", "normals", "binormals", "show"]) except getopt.error as msg: print(msg) print(__doc__) return 2 show = False for o, a in opts: if o in ("-h", "--help"): print(__doc__) return 0 elif o in ("-s", "--show"): show = True for infile in args: try: if ".obj" not in infile: print("WARNING", finfile, "does not look like a valid obj file") continue obj = ObjFile(infile) egg = obj.toEgg() f, e = os.path.splitext(infile) outfile = f + ".egg" for o, a in opts: if o in ("-n", "--normals"): egg.recomputeVertexNormals(float(a)) elif o in ("-b", "--binormals"): egg.recomputeTangentBinormal(GlobPattern("")) egg.removeUnusedVertices(GlobPattern("")) if True: egg.triangulatePolygons(EggData.TConvex & EggData.TPolygon) if True: egg.recomputePolygonNormals() egg.writeEgg(Filename(outfile)) if show: os.system("pview " + outfile) except Exception as e: print(e) return 0
def obj2egg(infile, outfile=None): os.chdir(os.path.dirname(infile)) obj = ObjFile(infile) egg = obj.toEgg(verbose=False) if outfile is None: f, e = os.path.splitext(infile) outfile = f + ".egg" egg.removeUnusedVertices(GlobPattern("")) #egg.triangulatePolygons(EggData.TConvex & EggData.TPolygon) #egg.recomputePolygonNormals() #egg.recomputeVertexNormals(30) #egg.recomputeTangentBinormal(GlobPattern("")) egg.writeEgg(Filename(outfile))
def test_globpattern_matches_file(): patt = GlobPattern('/a/b/c') assert patt.matches_file('/a/b/c') assert patt.matches_file('///a////b//c') assert patt.matches_file('/a/b/././c') assert not patt.matches_file('') assert not patt.matches_file('/') assert not patt.matches_file('/a/b/d') assert not patt.matches_file('/A/b/c') assert not patt.matches_file('/a/b/c/') assert not patt.matches_file('/a/b/c/.') assert not patt.matches_file('a/b/c') assert not patt.matches_file('./a/b/c') # Test regular pattern patt = GlobPattern('*a') assert patt.matches_file('a') assert patt.matches_file('aa') assert patt.matches_file('xa') assert not patt.matches_file('A') assert not patt.matches_file('ax') assert not patt.matches_file('xax') # Test path ending in directory for patt in GlobPattern('/a/b/c/'), \ GlobPattern('/a/b/c/.'), \ GlobPattern('/a/b//c//'), \ GlobPattern('/a/b/./c/./'): assert patt.matches_file('/a/b/c/') assert patt.matches_file('///a////b//c//') assert patt.matches_file('/a/b/././c/') assert patt.matches_file('/a/b/c/.') assert not patt.matches_file('/a/b/c') assert not patt.matches_file('/a/b/c/./d') assert not patt.matches_file('a/b/c/') assert not patt.matches_file('./a/b/c/') # Test globstar in middle for patt in GlobPattern('/a/**/c'), GlobPattern('/a/**/**/c'): assert patt.matches_file('/a/c') assert patt.matches_file('/a/b/c') assert patt.matches_file('/a/b/d/c') assert not patt.matches_file('/a/b/c/d') assert not patt.matches_file('/d/b/c') assert not patt.matches_file('/a/b/d') # Test globstar in beginning for patt in GlobPattern('/**/b/c'), GlobPattern('/**/**/**/b/c'): assert patt.matches_file('/a/b/c') assert patt.matches_file('/a/d/b/c') assert patt.matches_file('/a/b/c') assert patt.matches_file('/a/b/c/./b//c') assert not patt.matches_file('/a/b/c/d') assert not patt.matches_file('/a/c') assert not patt.matches_file('/a/b/d') # Test globstar at end for patt in GlobPattern('/a/b/**'), \ GlobPattern('/a/b/**/**'), \ GlobPattern('/a/b//**//**/**'): assert patt.matches_file('/a/b/') assert patt.matches_file('/a/b/.') assert patt.matches_file('/a/b//') assert patt.matches_file('/a/b/c') assert patt.matches_file('/a/b/c/d/e/f/g/h') assert patt.matches_file('/a/b/d/c') assert not patt.matches_file('/a/') assert not patt.matches_file('/a/c/b') # Test multiple globstars at multiple locations patt = GlobPattern('/a/**/b/**/c') assert patt.matches_file('/a/b/c') assert patt.matches_file('/a/./b/./c') assert patt.matches_file('/a//b//c') assert patt.matches_file('/a/x/y/b/c') assert patt.matches_file('/a/b/x/y/c') assert patt.matches_file('/a/b/c/a/b/c') assert patt.matches_file('/a/x/y/b/x/y/c') assert not patt.matches_file('/a/b/x') assert not patt.matches_file('/a/b/c/x') assert not patt.matches_file('/a/b/c/') assert not patt.matches_file('/a/b/c/.')