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)
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)
def build_objs(out_root, modeldict, outdict, angledict=None, f_tgz=True, f_force=False): # Temporary path in which to extract .obj files before conversion. tmp_root = os.path.join(os.environ["HOME"], "tmp", "scrap") # Raise exception if the out_root is an existing file if os.path.isfile(out_root): raise IOError("File already exists, cannot make: %s" % out_root) # Create out_root directory if necessary if not os.path.isdir(out_root): os.makedirs(out_root) imgdirname = "tex" out_pths = [] # Loop over models, doing the conversions for modelname, targzname in modeldict.iteritems(): # Set up file names objname = modelname + ".obj" outname = outdict[modelname] # Make new paths new_obj_pth = os.path.join(out_root, outname, outname + ".obj") new_tex_pth = os.path.join(out_root, outname, imgdirname) new_mtl_pth = os.path.join(out_root, outname, outname + ".mtl") if f_tgz: out_pth = os.path.join(out_root, outname, outname + ".tgz") else: out_pth = new_obj_pth out_pths.append(out_pth) if not f_force and os.path.isfile(out_pth): continue print "Building: %s" % objname # un-tar, un-gz into a temp directory tmp_tar_pth = os.path.join(tmp_root, "tartmp") allnames = mt.untar(targzname, tmp_tar_pth) # Get target's path names = [n for n in allnames if os.path.split(n)[1] == objname] # Raise exception if there are not exactly 1 if len(names) != 1: raise ValueError("Cannot find unique object file in tar. Found: %s" % ", ".join(names)) # Make obj, mtl, and tex paths obj_pth = os.path.join(tmp_tar_pth, names[0]) # Fix the .mtl and texture names and move them mtl_pth = os.path.splitext(obj_pth)[0] + ".mtl" mt.fix_tex_names(mtl_pth, imgdirname=imgdirname) tex_pth = os.path.join(os.path.dirname(obj_pth), imgdirname) ## Normalize the .obj coordinates # The params are the angles if angledict is not None: params = angledict[modelname] # Transform vertices rot = (params[0] + np.pi / 2., params[1] + np.pi / 2., params[2]) T0 = mt.build_rot(rot) # Normalize the obj and move it mt.transform_obj(obj_pth, new_obj_pth, T0=T0) else: # Normalize the obj and move it mt.transform_obj(obj_pth, new_obj_pth) # Copy .mtl file over shutil.copy2(mtl_pth, new_mtl_pth) # Copy the textures over mt.copy_tex(tex_pth, new_tex_pth) if f_tgz: # Convert the .obj/.mtl/tex to a .tgz with tarfile.open(out_pth, mode="w:gz") as tf: for out_pth in (new_obj_pth, new_mtl_pth, new_tex_pth): # Add to zip tf.add(out_pth, out_pth.split(out_root + "/")[1]) # Remove the files if os.path.isfile(out_pth): os.remove(out_pth) elif os.path.isdir(out_pth): shutil.rmtree(out_pth) # Remove tmp directory print "rm -rf %s" % tmp_root shutil.rmtree(tmp_root) out_pths.sort() return out_pths
def build_objs(out_root, modeldict, outdict, angledict=None, f_tgz=True, f_force=False): # Temporary path in which to extract .obj files before conversion. tmp_root = os.path.join(os.environ["HOME"], "tmp", "scrap") # Raise exception if the out_root is an existing file if os.path.isfile(out_root): raise IOError("File already exists, cannot make: %s" % out_root) # Create out_root directory if necessary if not os.path.isdir(out_root): os.makedirs(out_root) imgdirname = "tex" out_pths = [] # Loop over models, doing the conversions for modelname, targzname in modeldict.iteritems(): # Set up file names objname = modelname + ".obj" outname = outdict[modelname] # Make new paths new_obj_pth = os.path.join(out_root, outname, outname + ".obj") new_tex_pth = os.path.join(out_root, outname, imgdirname) new_mtl_pth = os.path.join(out_root, outname, outname + ".mtl") if f_tgz: out_pth = os.path.join(out_root, outname, outname + ".tgz") else: out_pth = new_obj_pth out_pths.append(out_pth) if not f_force and os.path.isfile(out_pth): continue print "Building: %s" % objname # un-tar, un-gz into a temp directory tmp_tar_pth = os.path.join(tmp_root, "tartmp") allnames = mt.untar(targzname, tmp_tar_pth) # Get target's path names = [n for n in allnames if os.path.split(n)[1] == objname] # Raise exception if there are not exactly 1 if len(names) != 1: raise ValueError( "Cannot find unique object file in tar. Found: %s" % ", ".join(names)) # Make obj, mtl, and tex paths obj_pth = os.path.join(tmp_tar_pth, names[0]) # Fix the .mtl and texture names and move them mtl_pth = os.path.splitext(obj_pth)[0] + ".mtl" mt.fix_tex_names(mtl_pth, imgdirname=imgdirname) tex_pth = os.path.join(os.path.dirname(obj_pth), imgdirname) ## Normalize the .obj coordinates # The params are the angles if angledict is not None: params = angledict[modelname] # Transform vertices rot = (params[0] + np.pi / 2., params[1] + np.pi / 2., params[2]) T0 = mt.build_rot(rot) # Normalize the obj and move it mt.transform_obj(obj_pth, new_obj_pth, T0=T0) else: # Normalize the obj and move it mt.transform_obj(obj_pth, new_obj_pth) # Copy .mtl file over shutil.copy2(mtl_pth, new_mtl_pth) # Copy the textures over mt.copy_tex(tex_pth, new_tex_pth) if f_tgz: # Convert the .obj/.mtl/tex to a .tgz with tarfile.open(out_pth, mode="w:gz") as tf: for out_pth in (new_obj_pth, new_mtl_pth, new_tex_pth): # Add to zip tf.add(out_pth, out_pth.split(out_root + "/")[1]) # Remove the files if os.path.isfile(out_pth): os.remove(out_pth) elif os.path.isdir(out_pth): shutil.rmtree(out_pth) # Remove tmp directory print "rm -rf %s" % tmp_root shutil.rmtree(tmp_root) out_pths.sort() return out_pths