import os import sys import fcntl import stat import json import math import subprocess import fnmatch import re import tempfile import shutil OUTDIR = os.getcwd() CAMFILE = OUTDIR + "/json/cameras/shotCam.json" LIGHTFILE = OUTDIR + "/json/lights/lights.json" OBJ2PBRT = shutil.which("obj2pbrt") OCEAN2PBRT = shutil.which("obj2pbrt") TMPDIR = '/tmp' IDENTITY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] # DISNEY: Solids has scattrDistR,G and B; Thin has flatness and diffTrans # PBRT: difftrans and flatness are specific to the BSDF, enabled by the thin parameter DIS2PBRT = { "metallic": "float metallic", "roughness": "float roughness", "anisotropic": "float anisotropic", "specularTint": "float speculartint", "sheen": "float sheen", "sheenTint": "float sheentint", "clearcoat" : "float clearcoat", "clearcoatGloss": "float clearcoatgloss", "specTrans" : "float spectrans", "ior" : "float eta" } def mon2lin(color): return [math.pow(color[0], 2.2), math.pow(color[1], 2.2), math.pow(color[2], 2.2)] def readJsonFile(jsonFile): """ Wrapper script to import json file as dict. :param jsonFile: file as string :return: dict """ with open(os.path.abspath(jsonFile), "r") as jf: jsonDict = json.load(jf) return jsonDict if jsonDict else {} def writeMainPbrtFile(outPbrtFile="pbrt/islandX.pbrt"): """ Create the main pbrt file with has all the camera, lights and geometry includes and WorldBegin/End :param outPbrtFile: file to create :return: success """ pbrtFile = os.path.abspath(outPbrtFile) pbrtFileObj = os.open(pbrtFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) fcntl.flock(pbrtFileObj, fcntl.LOCK_EX) try: f = os.fdopen(pbrtFileObj,'w') except IOError as (errno, strerror): print "Couldn't open %s for write: %s" % (pbrtFile, strerror) return False f.writelines(createDefaultHeader()) f.writelines(createCamera(CAMFILE)) # recommended after much testing... f.writelines('Sampler "sobol" "integer pixelsamples" 1024\n') # went up to 8192 f.writelines('Integrator "volpath" "integer maxdepth" [10]\n') f.writelines("\nWorldBegin\n") f.writelines(createLights(LIGHTFILE)) f.writelines(insertElements()) f.writelines("\nWorldEnd\n") os.fsync(pbrtFileObj) fcntl.flock(pbrtFileObj, fcntl.LOCK_UN) f.close() os.chmod(pbrtFile, stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IWGRP|stat.S_IROTH|stat.S_IWOTH) print "Writing main pbrt file: ", pbrtFile return True def createDefaultHeader(): header = 'Scale -1 1 1\n' header += 'Film "image" "string filename" ["island.png"]\n' header += ' "integer xresolution" [1024] "integer yresolution" [429]\n' header += ' "float maxsampleluminance" 10\n\n' return header def createCamera(jsonCamFile): """ :param: camFile: Json file with raw camera data from maya. :return: """ with open(jsonCamFile, "r") as cf: cameraDict = json.load(cf) cameraTxt = "LookAt %f %f %f" % (cameraDict["eye"][0], cameraDict["eye"][1], cameraDict["eye"][2]) cameraTxt += " %f %f %f" % (cameraDict["look"][0], cameraDict["look"][1], cameraDict["look"][2]) cameraTxt += " %f %f %f\n" % (cameraDict["up"][0], cameraDict["up"][1], cameraDict["up"][2]) cameraTxt += """Camera "perspective" "float fov" [%f]\n""" % cameraDict["fov"] cameraTxt += """ "float screenwindow" [%f %f %f %f]\n""" % (cameraDict["screenwindow"][0], cameraDict["screenwindow"][1], cameraDict["screenwindow"][2], cameraDict["screenwindow"][3]) cameraTxt += """ "float frameaspectratio" [%f]\n""" % cameraDict["ratio"] cameraTxt += """ "float lensradius" [%f]\n""" % cameraDict["lensRadius"] cameraTxt += """ "float focaldistance" [%f]\n""" % cameraDict["centerOfInterest"] return cameraTxt def createLights(jsonLightsFile): lightText = "" with open(os.path.abspath(jsonLightsFile), "r") as lf: lightsDict = json.load(lf) for lightName, lightDict in lightsDict.iteritems(): # common values lc = mon2lin(lightDict["color"]) le = math.pow(2, lightDict["exposure"]) rot = lightDict["rotation"] loc = lightDict["location"] tm = lightDict["translationMatrix"] if lightDict["type"] == "dome": #mapname = lightDict["map"] mapname = os.path.basename(lightDict["envmapCamera"]) lightText += "# skydome: %s\n" % lightName lightText += "AttributeBegin\n" #lightText += " Translate %f %f %f\n" % (loc[0], loc[1], loc[2]) #lightText += " Rotate %f %f %f 1\n" % (rot[0], rot[1], rot[2]) # This is what it took to line up our HDR image. YMMV lightText += " Scale -1 1 1\n" lightText += " Rotate 90 -1 0 0\n" lightText += " Rotate 65 0 0 1\n" lightText += """ LightSource "infinite" "string mapname" "%s"\n""" % os.path.join("..","textures",mapname) lightText += "AttributeEnd\n\n" elif lightDict["type"] == "distant": lightText += "# distant light: %s\n" % lightName lightText += "AttributeBegin\n" lightText += " Translate %f %f %f\n" % (loc[0], loc[1], loc[2]) lightText += " Rotate %f %f %f 1\n" % (rot[0], rot[1], rot[2]) lightText += """ LightSource "distant" "point from" [ 0.0 0.0 0.0]\n""" lightText += """ "point to" [ 0.0 0.0 -1.0 ]\n""" lightText += """ "rgb L" [ %f %f %f ]\n""" % (lc[0], lc[1], lc[2]) lightText += "AttributeEnd\n\n" elif lightDict["type"] == "quad": hw = float(lightDict["width"]) / 2 hh = float(lightDict["height"]) / 2 lightText += "# quad light w/ simple triangle mesh: %s\n" % lightName lightText += "AttributeBegin\n" lightText += """ AreaLightSource "diffuse" "rgb L" [%f %f %f]\n""" % (lc[0]*le, lc[1]*le, lc[2]*le) lightText += " Transform [%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f]\n" % tuple(tm) lightText += """ Shape "trianglemesh" "integer indices" [0 2 1 0 3 2] "float alpha" 0\n""" lightText += """ "point P"[%f %f 0 %f %f 0 %f %f 0 %f %f 0]\n""" % (hw, hh, -hw, hh, -hw, -hh, hw, -hh) lightText += "AttributeEnd\n\n" else: print "Light type not supported:", lightName, lightDict["type"] return lightText def createDefaultTail(): return "\nWorldEnd\n" def createGeometryHeader(gp, jsonFile, materialsName): gp.write("# Created from %s\n" % jsonFile) gp.write("AttributeBegin\n") gp.write(" Include \"%s\"\n\n" % materialsName) def createGeometryTail(gp): gp.write("AttributeEnd\n") def insertElements(): elementText = '\n' for element in os.listdir(os.path.abspath("obj")): elementText += 'Include "%s"\n'% os.path.join(element, element + ".pbrt") return elementText def geomToPbrt(objFile, pbrtFile, matFile): """ That's an obj file and converts it to pbrt using 'obj2pbrt'. In addition, to support a binding hack, it comments out the MakeNamedMaterial lines left by 'obj2pbrt'. We are just using the bindings, since OBJ format does not support our materials. [NEW] Added hardcoded materials where a mesh-specific texture is required. :param objFile: :param pbrtFile: :param bakeTextures: :return: """ # Make sure we have absolute path to both files. fpObjFile = os.path.abspath(objFile) fpPbrtFile = os.path.abspath(pbrtFile) # in order to find the mtl file, we need to be >in< the obj directory. dwd = os.getcwd() os.chdir(os.path.dirname(fpObjFile)) print "Creating pbrt geometry from", objFile if "osOcean" in fpObjFile: cmd = [OCEAN2PBRT, fpObjFile, fpPbrtFile] else: cmd = [OBJ2PBRT, "--ptexquads", fpObjFile, fpPbrtFile] subprocess.call(cmd) # the final pbrt geometry file will have embedded MakeNamedMaterial nodes. We just need to comment those out. # sed is your friend. print "Removing unneeded material definitions from", pbrtFile cmd = ['sed', '-i', 's/MakeNamedMaterial/#MakeNamedMaterial/g', fpPbrtFile] subprocess.call(cmd) os.chdir(dwd) # if we're using textures, we need to insert the texture overrides directly into the geom. insertTextureOverrides(fpPbrtFile, matFile) def matToPbrt(element, matFile, pbrtFile): """ Take a json file and convert it into a pbrt "Named" material file which will be used for reference. :param element: Element name -- Used for error codes and top-of-file comments. :param matFile: Json material file to be parsed. :param pbrtFile: Pbrt material file to be output. :return: None """ with open(matFile, "r") as jf: matDict = json.load(jf) if not matDict: print "ERROR: Unable to parse matfile: ", matFile return print "Converting ", matFile pbrtFileObj = os.open(pbrtFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) fcntl.flock(pbrtFileObj, fcntl.LOCK_EX) try: f = os.fdopen(pbrtFileObj,'w') except IOError as (errno, strerror): print "Couldn't open %s for write: %s" % (pbrtFile, strerror) return f.writelines("# Materials for %s\n"%element) f.writelines("# Converted from %s\n\n"%matFile) for matName, valueDict in matDict.iteritems(): if matName == "hidden": f.writelines('MakeNamedMaterial "hidden"\n') f.writelines(' "string type" ["none"]\n') f.writelines('#End MakeNamedMaterial "hidden"\n\n') continue f.writelines('MakeNamedMaterial "%s"\n'%matName) f.writelines(' "string type" ["disney"]\n') if 'baseColor' in valueDict: color = mon2lin(valueDict['baseColor']) f.writelines(' "rgb color" [%1.2f %1.2f %1.2f]\n'% (color[0], color[1],color[2])) else: print "WARNING WARNING WARNING: Can not find 'baseColor' in", element, matName f.writelines(' "rgb color" [1 0 0]\n') for val in valueDict: if val in DIS2PBRT: # common parameters f.writelines(' "%s" [%1.2f]\n' % (DIS2PBRT[val], valueDict[val])) if "type" in valueDict and valueDict["type"] == "thin": f.writelines(' "bool thin" ["true"]\n') f.writelines(' "float difftrans" [%1.2f]\n' % valueDict["diffTrans"]) f.writelines(' "float flatness" [%1.2f]\n\n' % valueDict['flatness']) else: # solid f.writelines(' "rgb scatterdistance" [%1.2f %1.2f %1.2f]\n\n' % (valueDict["scatterDistance"][0], valueDict["scatterDistance"][1], valueDict["scatterDistance"][2])) f.writelines('#End MakeNamedMaterial "%s"\n\n'%matName) os.fsync(pbrtFileObj) fcntl.flock(pbrtFileObj, fcntl.LOCK_UN) f.close() os.chmod(pbrtFile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH) print "Writing pbrt file: ", pbrtFile def jsonPrimitiveToPbrt(primType, element, primitiveName, iDict): if primType == "curve": return jsonPrimitiveCurveToPbrt(element, primitiveName, iDict) elif primType == "element": return jsonPrimitiveElementToPbrt(element, primitiveName, iDict) else: return jsonPrimitiveArchiveToPbrt(element, primitiveName, primitiveJsonFile=iDict["jsonFile"]) def jsonPrimitiveArchiveToPbrt(element, primitiveName, primitiveJsonFile): """ Create the pbrt reference file for a particular primitive instance. This will be a raw list of all the instances that make up this particular primitive with the transformations. :param element: The name of the element (used the build paths) :param primitiveName: The name of the primitive description we're converting. :param primitiveJsonFile: Json file containing the archives and all instances of them. :return: The 'short' path name, which is the partial path without the pbrt directory in front """ fileName = os.path.basename(primitiveJsonFile)[:-4] + "pbrt" if primitiveJsonFile else primitiveName + ".pbrt" pbrtPrimitiveFile = os.path.join("pbrt", element, primitiveName, fileName) shortFileName = os.path.join(element, primitiveName, fileName) iDict = readJsonFile(primitiveJsonFile) pbrtFileObj = os.open(pbrtPrimitiveFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) fcntl.flock(pbrtFileObj, fcntl.LOCK_EX) try: f = os.fdopen(pbrtFileObj,'w') except IOError as (errno, strerror): print "Couldn't open %s for write: %s" % (pbrtPrimitiveFile, strerror) return f.writelines("# Converted from %s\n\n" % primitiveJsonFile) for archive, primitiveDict in iDict.iteritems(): # Create one file per archive xgen Description archiveName = os.path.basename(archive)[:-4] for instanceName, transform in primitiveDict.iteritems(): writeInstance(f, archiveName, instanceName, transform, concat=True) os.fsync(pbrtFileObj) fcntl.flock(pbrtFileObj, fcntl.LOCK_UN) f.close() os.chmod(pbrtPrimitiveFile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH) print "Writing primitive archive pbrt file: ", pbrtPrimitiveFile return shortFileName def jsonPrimitiveCurveToPbrt(element, primitiveName, cDict): """ Convert json primitive curve to pbrt curve :param element: used to find json files :param primitiveName: used to find json files :param cDict: dictionary of all curves :return: """ primitiveJsonFile = cDict["jsonFile"] matFile = os.path.join("json", element, "materials.json") fileName = os.path.basename(primitiveJsonFile)[:-4] + "pbrt" if primitiveJsonFile else primitiveName + ".pbrt" pbrtPrimitiveFile = os.path.join("pbrt", element, primitiveName, fileName) shortFileName = os.path.join(element, primitiveName, fileName) curveList = readJsonFile(primitiveJsonFile) pbrtFileObj = os.open(pbrtPrimitiveFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) fcntl.flock(pbrtFileObj, fcntl.LOCK_EX) try: f = os.fdopen(pbrtFileObj,'w') except IOError as (errno, strerror): print "Couldn't open %s for write: %s" % (pbrtPrimitiveFile, strerror) return f.writelines("# Converted from %s\n\n"%primitiveJsonFile) # Curves don't inherit materials like the OBJs so, so we need to insert the material at the top. f.writelines("AttributeBegin\n") mat = findMaterial(primitiveName, readJsonFile(matFile)) if mat: f.writelines('NamedMaterial "%s"\n\n'%mat) for curve in curveList: # Create one file per archive xgen Description writeCurve(f, curve, cDict) f.writelines("AttributeEnd\n") os.fsync(pbrtFileObj) fcntl.flock(pbrtFileObj, fcntl.LOCK_UN) f.close() os.chmod(pbrtPrimitiveFile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH) print "Writing primitive curve pbrt file: ", pbrtPrimitiveFile return shortFileName def jsonPrimitiveElementToPbrt(element, primitiveName, eDict): """ The primitive being instanced around is a copy of another element, which could include its own primitive data. :param element: The name of the element (used the build paths) :param primitiveName: The name of the primitive description we're converting. :param eDict: Dictionary includes reference element, possible variant and the source json file with the transforms by variant. "base" variant is assumed to the the top level element definition. :return: The 'short' path name, which is the partial path without the pbrt directory in front """ primitiveJsonFile = eDict["jsonFile"] fileName = os.path.basename(primitiveJsonFile)[:-4] + "pbrt" if primitiveJsonFile else primitiveName + ".pbrt" pbrtPrimitiveFile = os.path.join("pbrt", element, primitiveName, fileName) shortFileName = os.path.join(element, primitiveName, fileName) iDict = readJsonFile(primitiveJsonFile) # we also need to load the source element json file so we can access it's information for inclusion. sourceJsonFile = os.path.abspath(os.path.join("json", eDict["element"], eDict["element"] + ".json")) sourceDict = readJsonFile(sourceJsonFile) pbrtFileObj = os.open(pbrtPrimitiveFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) fcntl.flock(pbrtFileObj, fcntl.LOCK_EX) try: f = os.fdopen(pbrtFileObj,'w') except IOError as (errno, strerror): print "Couldn't open %s for write: %s" % (pbrtPrimitiveFile, strerror) return f.writelines("# Converted from %s\n\n" % primitiveJsonFile) for variant, primitiveDict in iDict.iteritems(): # each variant is the key to a dictionary of instance name/transforms inclFileList = [] # TODO: if variant == "base": instanceRef = os.path.basename(sourceDict["geomObjFile"])[:-4] if "instancedPrimitiveJsonFiles" in sourceDict: for primitiveName, ipDict in sourceDict["instancedPrimitiveJsonFiles"].iteritems(): fileName = os.path.basename(ipDict["jsonFile"])[:-4] + "pbrt" inclFileList.append(os.path.join(eDict["element"], primitiveName, fileName)) elif variant in sourceDict["variants"]: instanceRef = os.path.basename(sourceDict["variants"][variant]["geomObjFile"])[:-4] if "instancedPrimitiveJsonFiles" in sourceDict["variants"][variant]: for primitiveName, ipDict in sourceDict["variants"][variant]["instancedPrimitiveJsonFiles"].iteritems(): fileName = os.path.basename(ipDict["jsonFile"])[:-4] + "pbrt" inclFileList.append(os.path.join(eDict["element"], primitiveName, fileName)) else: print "Error: Problems finding variant %s for element %s\n" %(variant, eDict["element"]) sys.exit() for instanceName, transform in primitiveDict.iteritems(): writeInstance(f, instanceRef, instanceName, transform, inclFileList=inclFileList, concat=True) os.fsync(pbrtFileObj) fcntl.flock(pbrtFileObj, fcntl.LOCK_UN) f.close() os.chmod(pbrtPrimitiveFile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH) print "Writing primitive archive pbrt file: ", pbrtPrimitiveFile return shortFileName def writeObject(f, objectName, pbrtGeometryFile, matFile=None): """ Create named object definition around single geometry file. :param f: :param objectName: What to call the object :param pbrtGeometryFile: The file being referenced :param matFile: Some variants need a separate matFile, include override for that here. :return: """ f.writelines(' AttributeBegin\n') f.writelines(' ObjectBegin "%s"\n' % objectName) if matFile: f.writelines(' Include "%s"\n' % matFile) f.writelines(' Include "%s"\n' % pbrtGeometryFile) f.writelines(' ObjectEnd\n') f.writelines(' AttributeEnd\n\n') def writeInstance(f, objectName, instanceName, transform, inclFileList=[], concat=False): """ Create instances of previously named object :param f: :param objectName: Name of object :param instanceName: Name of instance (for reference back to json file) :param transform: Transform :param inclFileList: For primitive files that need to be referenced after object :param concat: Use ConcatTransform to properly inherit final instance position of copies of elements. :return: """ f.writelines('# %s\n' % instanceName) f.writelines(' AttributeBegin\n') if concat: f.writelines(' ConcatTransform [%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f]\n' % tuple(transform)) else: f.writelines(' Transform [%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f]\n' % tuple(transform)) f.writelines(' ObjectInstance "%s"\n' % objectName) for inclFile in inclFileList: f.writelines(' Include "%s"\n' % inclFile) f.writelines(' AttributeEnd\n\n') def writeCurve(f, curve, cDict): """ Updated to work with new pbrt bspline format. :param f: :return: """ w0 = cDict["widthRoot"] w1 = cDict["widthTip"] # TODO: Should be change cylinder to flat? ctype = "cylinder" if cDict["faceCamera"] else "flat" f.writelines(' Shape "curve" "string type" ["%s"]\n'% ctype) f.writelines(' "string basis" ["bspline"]\n') f.writelines(' "integer degree" [%d]\n'% cDict["degrees"]) f.writelines(' "point P" [\n') # Converting maya clamped bsplines to pbrt non-clamped requires stacking the first and last points. f.writelines(' %f %f %f\n' % (curve[0][0], curve[0][1], curve[0][2])) # stack for p in curve: f.writelines(' %f %f %f\n' % (p[0], p[1], p[2])) f.writelines(' %f %f %f\n' % (curve[-1][0], curve[-1][1], curve[-1][2])) # stack f.writelines(' ]\n') if w0 and w1: f.writelines('"float width0" [%1.2f] "float width1" [%1.2f]\n\n' % (w0, w1)) def writeObjectFile(element, pbrtOutGeometry, instCopies = None, primDict = None): """ Keep all the object references in a single file to keep the main element file readable. Because we can't embed instances in pbrt, we need to include all references here, both the main element and all primitives or variants it might be referring too. :param element: main element :param pbrtOutGeometry: main element geometry file :param instCopies: if an instanced copy has its own geometry, we need to reference that as well. :param primDict: And all primitives used by the geometry. :return: """ # put all the primitive objects at the top of the file since we can embed objects within objects. if not pbrtOutGeometry and not primDict and not instCopies: return "" shortName = os.path.join(element, "objects.pbrt") pbrtOutObjects = os.path.abspath(os.path.join("pbrt", shortName)) pbrtFileObj = os.open(pbrtOutObjects, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) fcntl.flock(pbrtFileObj, fcntl.LOCK_EX) try: f = os.fdopen(pbrtFileObj,'w') except IOError as (errno, strerror): print "Couldn't open %s for write: %s" % (pbrtOutObjects, strerror) return # there are all the archives used by the primitive description if primDict: for primName, primInstanceDict in primDict.iteritems(): # for each xgen description f.writelines("\n# archives used by %s\n" % primName) # for each archives used by this description if "archives" in primInstanceDict: for archive in primInstanceDict["archives"]: # the object reference is the archive or geo file base name archiveName = os.path.basename(archive)[:-4] # the object file is the archive or geo file base name + _geometry pbrtArchiveFile = os.path.join(element, primName, archiveName + "_geometry.pbrt") # if this is type 'element', we need to drop in the element's material file #HACKY matFile = os.path.join(primInstanceDict["element"], "materials.pbrt")\ if primInstanceDict["type"] == "element" else "" writeObject(f, archiveName, pbrtArchiveFile, matFile=matFile) # This is the main element object if pbrtOutGeometry: f.writelines("\n# main object for the element: %s\n" % element) writeObject(f, element, pbrtOutGeometry) # Instanced copies here. if instCopies: f.writelines("\n# instanced copies of the element with their own geometry\n") for instanceName, icDict in instCopies.iteritems(): if "geomObjFile" in icDict and icDict["geomObjFile"]: # convert obj name to pbrt location pbrtOutFile =icDict["geomObjFile"][4:-4] + "_geometry.pbrt" writeObject(f, icDict["name"], pbrtOutFile) os.fsync(pbrtFileObj) fcntl.flock(pbrtFileObj, fcntl.LOCK_UN) f.close() os.chmod(pbrtOutObjects, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH) print "Writing pbrt file: ", pbrtOutObjects return shortName def writeMainElement(pbrtOutElement, pbrtOutMaterials, pbrtOutGeometry, primitiveFiles, sourceJsonFile=""): jDict = readJsonFile(sourceJsonFile) element = jDict["name"] transform = jDict["transformMatrix"] pbrtFileObj = os.open(pbrtOutElement, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) fcntl.flock(pbrtFileObj, fcntl.LOCK_EX) try: f = os.fdopen(pbrtFileObj,'w') except IOError as (errno, strerror): print "Couldn't open %s for write: %s" % (pbrtOutElement, strerror) return if sourceJsonFile: f.writelines("# Converted from %s\n\n" % sourceJsonFile) ################################## Materials f.writelines("AttributeBegin\n\n") f.writelines("Include \"%s\"\n" % pbrtOutMaterials) ################################## Objects # Notes about objects: Since pbrt can instance within an instance, only _geometry files should be defined as objects. # For simplicity, lets defines all _geometry files as objects. primitiveDict = jDict["instancedPrimitiveJsonFiles"] if "instancedPrimitiveJsonFiles" in jDict else {} instancedCopiesDict = jDict["instancedCopies"] if "instancedCopies" in jDict else {} objectsFile = writeObjectFile(element, pbrtOutGeometry, instCopies=instancedCopiesDict, primDict=primitiveDict) if objectsFile: f.writelines("Include \"%s\"\n" % objectsFile) ############################ Main elements f.writelines("\n# Main element\n\n") primList = [] # a list of all the primitive names associated with this element, i.e. xgBonsai, xgGrass, xgShells primFileList = [] # a list of the primitive files, i.e. isBayCedarA1/xgBonsai/isBayCedarA1_xgBonsai.pbrt if primitiveDict: for primName, primInstanceDict in primitiveDict.iteritems(): primList.append(primName) primFileList.append(os.path.join(element, primName, element + "_" + primName + ".pbrt")) # create the default instance (we have at least once instance of every element) writeInstance(f, element, element, transform, primFileList) if instancedCopiesDict: f.writelines("\n# Instanced copies of main elementt\n\n") # all the other instances of the element and all primitive files used by element # Note: we can't instance the primitive files themsevles, because they contain references to instanced archives. for instance, icDict in instancedCopiesDict.iteritems(): primOverrides = icDict["instancedPrimitiveJsonFiles"].keys() if "instancedPrimitiveJsonFiles" in icDict else [] includedPrimFileList = [] for prim in primList: # For each primitive associated with this element, # is the primName the instance or element? Depends on is the instancedCopies instance if prim in primOverrides: primName = instance + '_' + prim + ".pbrt" else: primName = element + '_' + prim + ".pbrt" includedPrimFileList.append(os.path.join(element, prim, primName)) # isBayCedarA2/xgBonsai/isBayCedarA2_xgBonsai.pbrt if "geomObjFile" in icDict and icDict["geomObjFile"]: # print "This instance has its own geometry." writeInstance(f, instance, instance, icDict["transformMatrix"], includedPrimFileList) else: writeInstance(f, element, instance, icDict["transformMatrix"], includedPrimFileList) f.writelines("AttributeEnd\n") os.fsync(pbrtFileObj) fcntl.flock(pbrtFileObj, fcntl.LOCK_UN) f.close() os.chmod(pbrtOutElement, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH) print "Writing pbrt file: ", pbrtOutElement return pbrtOutElement def findMaterial(mesh, matDict, hierDict="", quiet=False): """ Given a patch, a materials dictionary and an optional hierarchy dictionary, this finds the correct material for that patch. :param mesh: The name of the mesh :param matDict: A dictionary of materials, created from the json materials file. :param hierDict: A dictionary of hierarchy expansions, created from the json .hier file. :param quiet: Suppress the no bindings error message :return: Matching material as string """ longNameMesh = hierDict[mesh] + '|' + mesh if mesh in hierDict else mesh found = [] for k, v in matDict.iteritems(): # find an assignment that matches bindings = v['assignment'] for binding in bindings: if fnmatch.fnmatchcase(longNameMesh, "*" + binding + "*"): found.append(k) if found: # if there's only one match, we found the binding. if len(found) == 1: return found[0] else: # if one is an exact string match, return that one (or the first # occurrence of an exact string match) for f in found: if mesh in matDict[f]['assignment']: return f # if there's not an exact string match, go for the match furthest up the hierarchy chain (this will solve # a problem with the bonsai matching for trunks vs branches vs tips. for f in found: print "f" sys.exit() # otherwise, return the last match. #makingshitup return found[-1] else: if not quiet: print "ERROR: Unable to find binding for mesh: ", mesh return "" def insertTextureOverrides(pbrtGeomFile, matFile): """ Find a mesh that's using a material with a texture and insert the texture override that includes the specific mesh name. i.e. textures/isGardeniaA/Color/archiveflowerbud0001_geo.ptx :param pbrtGeomFile: The pbrt geometry file created with obj2pbrt :param matFile: The json material file :param hierFile: The file containing the full hierarchy of the meshes (stripped out to create the obj files) :return: None """ COMMENT = "# Inserted materials directly above Shape for custom texture per mesh. v2.\n" handle, tempPbrtFile = tempfile.mkstemp(prefix='IS', suffix='.pbrt', dir=TMPDIR) tof = open(tempPbrtFile,"w") print "Inserting pbrt materials into: ", tempPbrtFile # Create a sub-dictionary of just the materials with textures. mDict = readJsonFile(matFile) # keys are primitive description name, i.e. xgGrass. textureMats = [key for key in mDict if "colorMap" in mDict[key] and mDict[key]["colorMap"]] # keys with materials matDict = {mat: mDict[mat] for mat in textureMats} # warning. these can be big files! with open(os.path.abspath(pbrtGeomFile), "r") as pgf: tof.write(COMMENT) meshName = "" colorName = "" dispName = "" colorMap = "" dispMap = "" material = "" for line in pgf.xreadlines(): # use generator if line.startswith(COMMENT[:20]): # Don't duplicate the comment. continue if line.startswith("# Name"): m = re.search('# Name "(.+?)"$', line) if not m: tof.write(line) continue meshName = m.group(1) tof.write(line) continue if line.startswith("NamedMaterial"): m = re.search('^NamedMaterial "(.+?)"$', line) if not m: tof.write(line) continue material = m.group(1) if material not in matDict: material = "" tof.write(line) continue colorMap = matDict[material]["colorMap"] if "colorMap" in matDict[material] else "" dispMap = matDict[material]["displacementMap"]if "displacementMap" in matDict[material] else "" if colorMap: colorName = material + "_Color" texPath = os.path.join("..", os.path.dirname(colorMap),meshName + ".ptx") tof.write('Texture "%s" "color" "ptex"\n' % colorName) tof.write(' "string filename" ["%s"]\n\n'%texPath) if dispMap: dispName = material + "_Disp" texPath = os.path.join("..", os.path.dirname(dispMap),meshName + ".ptx") tof.write('Texture "%s" "float" "ptex"\n' % dispName) tof.write(' "string filename" ["%s"]\n\n'%texPath) # write back the original NamedMaterial line tof.write(line) continue if line.startswith("Shape") and meshName and material: tof.write(line) m = re.search('^Shape "trianglemesh"$', line) if not m: continue if colorMap: tof.write(' "texture color" "%s"\n' % colorName) colorMap = "" if dispMap: tof.write(' "texture bumpmap" "%s"\n' % dispName) dispMap = "" continue # everything else just goes into the tempfile tof.write(line) tof.close() # copy temp file over old one print "Replacing ", pbrtGeomFile shutil.copy2(tempPbrtFile, os.path.abspath(pbrtGeomFile))