|
|
|
""" |
|
give a json element file, create pbrt scenes and objects. This should create the main element/element.xml, |
|
element/geometry.xml and element/materials.xml. In addition, if the element has instanced primitives, then |
|
for each primitive, a primitive subdirectory is created, along with a primitive.xml file and an archive_geometry |
|
pbrt file for each archive referenced by that primitive description: |
|
|
|
element/element.xml |
|
element/element_geometry.xml |
|
element/material.xml |
|
element/objects.xml |
|
|
|
element/primitive/primitive.xml |
|
element/primitive/archive_geometry.xml |
|
|
|
""" |
|
import sys |
|
import os |
|
import argparse |
|
import json |
|
import embreeutils as pu |
|
|
|
ISLANDDIR = os.getcwd() |
|
|
|
|
|
def parseArgs(): |
|
|
|
parser = argparse.ArgumentParser( |
|
description="Takes an element and creates pbrt element version of the geometry and scene.") |
|
|
|
parser.add_argument('element', metavar='ELEM', help="Element to load.") |
|
parser.add_argument('--frameRange', type=str, default="1", help="Frame range to export, i.e. 1 or 1-3 or 1-5:2") |
|
parser.add_argument('-o', '--outdir', default=ISLANDDIR, help="Where to write out the pbrt files(s).") |
|
parser.add_argument('--skipObj', action='store_true', default=False, help="Skip Obj Conversion") |
|
parser.add_argument('--skipCopies', action='store_true', default=False, help="Skip Instanced Copies Obj Generation") |
|
parser.add_argument('--skipArchives', action='store_true', default=False, help="Skip Archive Conversion") |
|
parser.add_argument('--skipMat', action='store_true', default=False, help="Skip Material Generation") |
|
parser.add_argument('--skipPrims', action='store_true', default=False, help="Skip Primitive(s) PBRT Generation") |
|
parser.add_argument('--skipMain', action='store_true', default=False, help="Skip Main PBRT Generation") |
|
parser.add_argument('--prim', default="", help="Specific json primitive file to load, i.e. xgGrass") |
|
|
|
return parser.parse_args() |
|
|
|
|
|
def main(): |
|
args = parseArgs() |
|
|
|
if args.element == "island": |
|
|
|
pu.writeMainPbrtFile() |
|
pu.writeEmbreeEcsFile() |
|
return |
|
|
|
jsonFile = os.path.join("json", args.element, args.element + ".json") |
|
with open(jsonFile, "r") as jf: |
|
jsonDict = json.load(jf) |
|
|
|
|
|
element = jsonDict["name"] |
|
objFile = jsonDict["geomObjFile"] |
|
matFile = jsonDict["matFile"] if "matFile" in jsonDict else "" |
|
instancedPrimitives = jsonDict["instancedPrimitiveJsonFiles"] if "instancedPrimitiveJsonFiles" in jsonDict else {} |
|
instancedCopies = jsonDict["instancedCopies"] if "instancedCopies" in jsonDict else {} |
|
primDesc = args.prim |
|
|
|
archiveFiles = {} |
|
primitiveFiles = [] |
|
|
|
print "Preparing embree export for element: ", element |
|
elementDir = os.path.join(args.outdir, "embree", element) |
|
|
|
|
|
for outDir in [elementDir] + [os.path.join(args.outdir, "embree", element, x) for x in instancedPrimitives]: |
|
if not os.path.exists(outDir): |
|
os.makedirs(outDir) |
|
|
|
|
|
pbrtRelOutElement = os.path.join(element, element + ".xml") |
|
pbrtRelOutGeometry = os.path.join(element, element + "_geometry.xml") |
|
pbrtRelOutMaterials = os.path.join(element, "materials.xml") |
|
pbrtOutElement = os.path.join(elementDir, element + ".xml") |
|
pbrtOutGeometry = os.path.join(elementDir, element + "_geometry.xml") |
|
pbrtOutMaterials = os.path.join(elementDir, "materials.xml") |
|
|
|
|
|
|
|
|
|
if not args.skipMat: |
|
print "################################################# Converting Materials: " |
|
pu.matToPbrt(element, matFile, pbrtOutMaterials) |
|
|
|
|
|
|
|
if not args.skipObj: |
|
print "################################################# Converting main element OBJ file: " |
|
print "Exporting: ", pbrtOutGeometry |
|
pu.geomToPbrt(objFile, pbrtOutGeometry, "", "../../") |
|
|
|
|
|
|
|
if instancedCopies: |
|
print "################################################# Converting Instanced copies OBJ files: " |
|
for instance, icDict in instancedCopies.iteritems(): |
|
if "geomObjFile" in icDict: |
|
pbrtOutInstGeometry = os.path.join(elementDir, instance + "_geometry.xml") |
|
print "Exporting: ", pbrtOutInstGeometry |
|
pu.geomToPbrt(icDict['geomObjFile'], pbrtOutInstGeometry , "", "../../") |
|
|
|
|
|
|
|
|
|
if not args.skipArchives or not instancedPrimitives: |
|
print "################################################# Converting Instanced JSON files and OBJ archives: " |
|
for primitiveName, ipDict in instancedPrimitives.iteritems(): |
|
|
|
if "archives" in ipDict: |
|
print "Create geometry data for all archives associated with ", primitiveName |
|
archiveFiles[primitiveName] = [] |
|
for archive in ipDict["archives"]: |
|
|
|
base = os.path.basename(archive)[:-4] |
|
pbrtOutArchiveGeometry = os.path.join(elementDir, primitiveName, base + "_geometry.xml") |
|
if not os.path.exists(pbrtOutArchiveGeometry): |
|
print "Exporting: ", pbrtOutArchiveGeometry |
|
if ipDict["type"] == "archive": |
|
pu.geomToPbrt(archive, pbrtOutArchiveGeometry, "", "../../../") |
|
else: |
|
|
|
variantMatFile = os.path.join("embree", ipDict["element"],"materials.xml" ) |
|
print "...Exporting variant with alternate material file: %s" % variantMatFile |
|
pu.geomToPbrt(archive, pbrtOutArchiveGeometry, variantMatFile,"../../../") |
|
archiveFiles[primitiveName].append(pbrtOutArchiveGeometry) |
|
|
|
else: |
|
|
|
if instancedPrimitives: |
|
for primitiveName, iDict in instancedPrimitives.iteritems(): |
|
archiveFiles[primitiveName] = [os.path.join(element, primitiveName, x) |
|
for x in os.listdir(os.path.join(elementDir, primitiveName)) |
|
if x.endswith("geometry.xml")] |
|
|
|
if not args.skipPrims: |
|
|
|
|
|
|
|
|
|
print "################################################# Creating Primitive reference files: " |
|
for primitiveName, ipDict in instancedPrimitives.iteritems(): |
|
if primDesc and primDesc != primitiveName: |
|
continue |
|
|
|
print "Create main primitive file for:", primitiveName, "using", ipDict["jsonFile"] |
|
primitiveFiles.append(pu.jsonPrimitiveToPbrt(ipDict["type"] if "type" in ipDict else "", element, primitiveName, ipDict)) |
|
|
|
|
|
|
|
|
|
if "type" in ipDict and ipDict["type"] == "element": |
|
sourceDict = pu.readJsonFile(os.path.join("json", ipDict["element"], ipDict["element"]+".json")) |
|
for variant, vDict in sourceDict["variants"].iteritems(): |
|
if "instancedPrimitiveJsonFiles" not in vDict: |
|
continue |
|
for primitiveName, vipDict in vDict["instancedPrimitiveJsonFiles"].iteritems(): |
|
primitiveFiles.append(pu.jsonPrimitiveToPbrt(vipDict["type"], ipDict["element"], primitiveName, vipDict)) |
|
|
|
|
|
|
|
if instancedCopies: |
|
print "################################################# Creating instanced copies primitives files: " |
|
for instance, icDict in instancedCopies.iteritems(): |
|
if "instancedPrimitiveJsonFiles" in icDict: |
|
for primitiveName, ipDict in icDict["instancedPrimitiveJsonFiles"].iteritems(): |
|
print "Create primitive file for:", primitiveName, "using", ipDict["jsonFile"] |
|
primitiveFiles.append(pu.jsonPrimitiveToPbrt(ipDict["type"] if "type" in ipDict else "", element, primitiveName, ipDict)) |
|
|
|
else: |
|
|
|
for primitiveName in instancedPrimitives: |
|
primitiveFiles += [os.path.join(element, primitiveName, x) |
|
for x in os.listdir(os.path.join(elementDir, primitiveName)) |
|
if x.endswith(".xml") and |
|
not x.endswith("geometry.xml")] |
|
|
|
if not args.skipMain: |
|
|
|
|
|
|
|
print "################################################# Creating main element reference: " |
|
print "Exporting:", pbrtOutElement |
|
pu.writeMainElement(pbrtOutElement, pbrtRelOutMaterials, pbrtRelOutGeometry, primitiveFiles, |
|
sourceJsonFile=jsonFile) |
|
|
|
|
|
if __name__ == "__main__": |
|
if main(): |
|
sys.exit(0) |
|
sys.exit(1) |
|
|
|
|
|
|
|
|
|
|