def check_format(pth, imgdirname="tex"): """ Checks format of a model directory to make sure everything is formatted per the genthor standard. Exit codes: 0 : Pass 1 : Couldn't find model 2 : Couldn't find required texture images """ random_id = str(np.random.randint(1e8)) tmp_pth = os.path.join(os.environ["HOME"], "tmp", "scrap_%s" % random_id) def returnfunc(): # Remove tmp path if os.path.isdir(tmp_pth): shutil.rmtree(tmp_pth) # Temporary path try: # Get the model's path modelpth = resolve_model_path(pth) except: # Remove tmp path returnfunc() return 1 ## There is a unique model or zip file # Determine file name and extension, and unzip if necessary name, ext = gt.splitext2(os.path.basename(modelpth)) if ext in zip_exts: # un-tar, un-zip into a temp directory untar(os.path.join(modelpth), tmp_pth) pth = os.path.join(tmp_pth, name) try: # Get the unzipped model's path modelpth = resolve_model_path(pth) except: # Remove tmp path returnfunc() return 1 name, ext = gt.splitext2(os.path.basename(modelpth)) ## There is a unique model file if ext == ".obj": mtlname = name + ".mtl" # Is .mtl present? f_mtl = os.path.isfile(mtlname) if f_mtl: # Parse the .mtl file for the image names mtl_pth = os.path.join(pth, mtlname) mtlnames = parse_mtl_imgs(mtl_pth) # Directory for the images img_pth = os.path.join(pth, imgdirname) # Get image file names imgnames, imgpaths = parse_dir_imgs(img_pth) # Check that all .mtl img names are present if not set(mtlnames) <= set(imgnames): returnfunc() return 2 returnfunc() return 0
def main(f_egg=True): # Model root directory model_pth = "/home/pbatt/work/genthor/raw_models" # Destination directory for .<out> files obj_root = gt.OBJ_PATH # Get the necessary data for the models (names, angles, etc) modeldict, outdict, angledict = get_modeldata(model_pth) # Build the .obj database print "Building .obj database..." obj_pths = build_objs(obj_root, modeldict, outdict, angledict=angledict, f_tgz=True, f_force=False) print "Finishes building .obj database." if f_egg: ## Convert to .egg # Destination directory for .egg files egg_root = gt.EGG_PATH # Create the in-out paths dict egg_pths = [ os.path.join(egg_root, gt.splitext2(os.path.basename(obj_pth))[0]) for obj_pth in obj_pths ] inout_pths = dict(zip(obj_pths, egg_pths)) # Do the conversion print "Building .egg database from .obj database..." convert(inout_pths, ext=".egg") print "Finished building .egg database."
def resolve_model_path(modelpth0): """ Finds a valid model path. It will convert an .obj model to an .egg if necessary.""" valid_exts = model_exts + zip_exts # Find a valid file first if os.path.isdir(modelpth0): ## modelpth is a directory -- determine what kind of objects ## are in it, and check that one is .egg/.bam/.obj # Get dir listing ld = os.listdir(modelpth0) # Add all valid filenames to filepths filepths = [fn for fn in ld if gt.splitext2(fn)[1] in valid_exts] # Verify modelpths has exactly 1 filename if len(filepths) != 1: raise IOError("Cannot find valid model file in: %s" % modelpth0) # Create modelpth modelpth = os.path.join(modelpth0, filepths[0]) elif not os.path.isfile(modelpth0): ## modelpth0 is not a directory or file, which means it might ## be a model name. Search for it in the eggs and models ## directories. # model name name = gt.splitext2(os.path.basename(modelpth0))[0] # possible file paths, ordered by priority (bam > egg > obj) filepths = (os.path.join(gt.BAM_PATH, name, name + ".bam"), os.path.join(gt.EGG_PATH, name, name + ".egg"), os.path.join(gt.OBJ_PATH, name, name + ".obj")) # Look for a valid file path for filepth in filepths: if os.path.isfile(filepth): # Found a good one, save it and break modelpth = filepth break else: # Keep looking modelpth = None if modelpth is None: # Error if we can't find a valid file raise IOError("Cannot find a valid model name: %s" % name) else: modelpth = modelpth0 return modelpth
def untar(tarname, tmp_pth): # Make the tmp_pth directory if it doesn't exist already if not os.path.isdir(tmp_pth): os.makedirs(tmp_pth) if gt.splitext2(tarname)[1] not in zip_exts: raise ValueError("Invalid zip file extension: %s" % tarname) try: # Open the tar with tarfile.open(tarname, 'r') as tf: # Extract it tf.extractall(tmp_pth) # Get tar.gz's member names names = tf.getnames() except: pdb.set_trace() return names
def autogen_egg(model_pth): # modelpth is now a valid file, check its extension and create an # .egg if it is not a panda file cmp = os.path.commonprefix([model_pth, gt.EGG_PATH]) ln = len(gt.EGG_PATH[len(cmp):].split('/')) nm = '/'.join(model_pth[len(cmp):].split('/')[ln:]) name, ext = gt.splitext2(os.path.basename(model_pth)) dirn = os.path.split(nm)[0] if ext not in mt.panda_exts: # modelpth is not a panda3d extension # The .egg's path egg_pth = os.path.join(gt.EGG_PATH, dirn, name + ".egg") if not os.path.isfile(egg_pth): # The .egg doesn't exist, so convert the input file inout_pth = {model_pth: egg_pth} print('converting to', inout_pth) convert(inout_pth, ext=".egg") else: egg_pth = model_pth return egg_pth
def convert(inout_pths, ext=".egg", f_blender=True, f_force=False): if ext not in (".egg", ".bam"): raise ValueError("Unsupported output type: %s" % ext) # Temporary path in which to extract .obj files before conversion. random_id = str(np.random.randint(1e8)) + '_' + hashlib.sha1( str(inout_pths)).hexdigest() tmp_root = os.path.join(os.environ["HOME"], "tmp", "scrap_%s" % random_id) # Loop over models, converting for in_pth, out_pth in inout_pths.iteritems(): if not f_force and os.path.isfile(out_pth): continue if not os.path.isfile(in_pth): raise IOError("File does not exist: %s" % in_pth) # Determine file name and extension name, ext0 = gt.splitext2(os.path.basename(in_pth)) if ext0 in (".tgz", ".tar.gz", ".tbz2", ".tar.bz2"): # un-tar, un-gz into a temp directory tmp_tar_pth = os.path.join(tmp_root, "tartmp") allnames = mt.untar(in_pth, tmp_tar_pth) # Get target's path names = [ n for n in allnames if os.path.splitext(n)[1] in (".egg", ".obj") and not os.path.basename(n).startswith('._') ] # Raise exception if there are not exactly 1 if len(names) != 1: raise ValueError("Can't find unique file in tar. Found: %s" % ", ".join(names)) in_pth = os.path.join(tmp_tar_pth, names[0]) ext0 = os.path.splitext(names[0])[1] elif ext0 not in (".egg", ".obj"): raise ValueError("Can't handle extension: %s" % in_pth) # Make the output paths. If the specified path has an # extension, that takes precedence over the argument 'ext'. ext1 = os.path.splitext(out_pth)[1] if ext1 == "": # out_pth is a directory, make it a filename out_pth = os.path.join(out_pth, name + ext) ext1 = ext elif ext1 not in (".egg", ".bam"): raise ValueError("Unsupported output type: %s" % ext1) # Make output directory (if necessary) if not os.path.isdir(os.path.split(out_pth)[0]): os.makedirs(os.path.split(out_pth)[0]) # Do the conversion, depending on ext1 type if ext0 == ".obj": mtl_pth = resolve_mtl_path(in_pth) if mtl_pth: mt.fix_tex_names(mtl_pth, imgdirname='tex', f_verify=False) if ext1 == ".bam": # Two-step conversion, first to egg, then to bam egg_pth = os.path.join(tmp_root, "eggtmp", "tmp.egg") else: # One-step conversion, to egg. egg_pth = out_pth # Convert .obj to .egg if not os.path.isfile(egg_pth): obj2egg(in_pth, egg_pth=egg_pth, f_blender=f_blender) if ext1 == ".bam": # Convert .egg to .bam egg2bam(egg_pth, bam_pth=out_pth) elif ext0 == ".egg" and ext1 == ".bam": # Convert .egg to .bam egg2bam(in_pth, bam_pth=out_pth) else: raise ValueError("unsupported output type: %s" % ext) # Remove all tmp directories print "rm -rf %s" % tmp_root shutil.rmtree(tmp_root)