Skip to content

Commit

Permalink
allow using unresolved asset paths with command-line tools
Browse files Browse the repository at this point in the history
ie, can now do "usdview custom://asset_bigGuy/costume_red/version_0023" (assuming you have a custom ArResolver to handle "custom://" paths)

usdview, usdcat, usdcheck, and usddiff should now all work with unresolved paths; usdstitch already worked

usdstitchclips and usdGenSchema are untested, due to not having appropriate inputs on hand; usdrender is untested, due to not having a OpenGL.raw._GLX module
  • Loading branch information
pmolodo committed Mar 15, 2017
1 parent 483f8b1 commit 562ec2c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
12 changes: 0 additions & 12 deletions pxr/usd/bin/usdcat/usdcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,6 @@ def main():
exitCode = 0

for inputFile in args.inputFiles:
# Ignore nonexistent or empty files.
if not os.path.isfile(inputFile) or os.path.getsize(inputFile) == 0:
_Err("Ignoring nonexistent/empty file '%s'" % inputFile)
exitCode = 1
continue

# Ignore unrecognized file types.
if not Usd.Stage.IsSupportedFile(inputFile):
_Err("Ignoring file with unrecognized type '%s'" % inputFile)
exitCode = 1
continue

# Either open a layer or compose a stage, depending on whether or not
# --flatten was specified. Note that 'usdData' will be either a
# Usd.Stage or an Sdf.Layer.
Expand Down
4 changes: 3 additions & 1 deletion pxr/usd/bin/usdcheck/usdcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def status(ok, path):
exitCode = 0
for p in opts.filePaths:
try:
Sdf.Layer.FindOrOpen(p)
result = Sdf.Layer.FindOrOpen(p)
if result is None:
raise Exception("could not open path %r" % p)
status(True, p)
except Exception as e:
status(False, p)
Expand Down
75 changes: 53 additions & 22 deletions pxr/usd/bin/usddiff/usddiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,16 @@ def _findDiffTools():
return (usdcatCmd, diffCmd)

def _getFileFormat(path):
from pxr import Sdf
from pxr import Sdf, Ar

# Note that python's os.path.splitext retains the '.' portion
# when obtaining an extension, but Sdf's Fileformat API doesn't
# expect one. We also make sure to prune out any version specifiers.
_, ext = os.path.splitext(path)
path = Ar.GetResolver().Resolve(path)
if path is None:
return None

if len(ext) <= 1:
fileFormat = Sdf.FileFormat.FindByExtension('usd')
else:
Expand All @@ -108,24 +112,26 @@ def _getFileFormat(path):

fileFormat = Sdf.FileFormat.FindByExtension(prunedExtension)

# Allow an empty file.
if fileFormat and (os.stat(path).st_size == 0 or fileFormat.CanRead(path)):
return fileFormat.formatId

return None
# Don't check if file exists - this should be handled by resolver (and
# path may not exist / have been fetched yet)
return fileFormat.formatId

def _convertTo(inPath, outPath, usdcatCmd, flatten=None, fmt=None):
# Just copy empty files -- we want something to diff against but
# the file isn't valid usd.
if os.stat(inPath).st_size == 0:
import shutil
try:
shutil.copy(inPath, outPath)
return 0
except:
return 1
else:
return call(_generateCatCommand(usdcatCmd, inPath, outPath, flatten, fmt))
try:
if os.stat(inPath).st_size == 0:
import shutil
try:
shutil.copy(inPath, outPath)
return 0
except:
return 1
except (IOError, OSError):
# assume it's because file doesn't exist yet, because it's an unresolved
# path...
pass
return call(_generateCatCommand(usdcatCmd, inPath, outPath, flatten, fmt))

def _tryEdit(fileName, tempFileName, usdcatCmd, fileType, composed):
if composed:
Expand Down Expand Up @@ -205,6 +211,9 @@ def _findFiles(args):
baseline-only and comparison-only are lists of individual files, while
matching is a list of corresponding pairs of files.'''
import os
import stat
from pxr import Ar

join = os.path.join
basename = os.path.basename
exists = os.path.exists
Expand All @@ -222,20 +231,42 @@ def listFiles(dirpath):
if len(args) < 2:
raise err

for index, exist in enumerate(map(exists, args)):
if not exist:
raise ValueError("Error: %s does not exist" % args[index])
# For speed, since filestats can be slow, stat all args once, then reuse that
# to determine isdir/isfile
resolver = Ar.GetResolver()
stats = []
for arg in args:
try:
st = os.stat(arg)
except (OSError, IOError):
if not resolver.Resolve(arg):
raise ValueError("Error: %s does not exist, and cannot be "
"resolved" % arg)
st = None
stats.append(st)

def isdir(st):
return st and stat.S_ISDIR(st.st_mode)

# if any of the directory forms are used, no paths may be unresolved assets
def validateFiles():
for i, st in enumerate(stats):
if st is None:
raise ValueError("Error: %s did not exist on disk, and using a "
"directory comparison form" % args[i])

# DIR FILES...
if os.path.isdir(args[0]) and not any(map(os.path.isdir, args[1:])):
if isdir(stats[0]) and not any(map(isdir, stats[1:])):
validateFiles()
dirpath = args[0]
files = set(map(os.path.relpath, args[1:]))
dirfiles = listFiles(dirpath)
return ([],
[(join(dirpath, p), p) for p in files & dirfiles],
[p for p in files - dirfiles])
# FILES... DIR
elif not any(map(os.path.isdir, args[:-1])) and os.path.isdir(args[-1]):
elif not any(map(isdir, stats[:-1])) and isdir(stats[-1]):
validateFiles()
dirpath = args[-1]
files = set(map(os.path.relpath, args[:-1]))
dirfiles = listFiles(dirpath)
Expand All @@ -245,7 +276,7 @@ def listFiles(dirpath):
# FILE FILE or DIR DIR
elif len(args) == 2:
# DIR DIR
if all(map(os.path.isdir, args)):
if all(map(isdir, stats)):
ldir, rdir = args[0], args[1]
lhs, rhs = map(listFiles, args)
return (
Expand All @@ -256,7 +287,7 @@ def listFiles(dirpath):
# comparison only
sorted([join(rdir, p) for p in rhs - lhs]))
# FILE FILE
elif not any(map(os.path.isdir, args)):
elif not any(map(isdir, stats)):
return ([], [(args[0], args[1])], [])

raise err
Expand Down
5 changes: 0 additions & 5 deletions pxr/usdImaging/lib/usdviewq/mainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,11 +1186,6 @@ def _openStage(self, usdFilePath, populationMaskPaths):
self._pathResolverContext = \
Ar.GetResolver().CreateDefaultContextForAsset(usdFilePath)

if not os.path.isfile(usdFilePath):
print >> sys.stderr, "Error: File not found '" + usdFilePath + \
"'. Stage was not opened."
return None

if self._mallocTags != 'none':
Tf.MallocTag.Initialize()

Expand Down

0 comments on commit 562ec2c

Please sign in to comment.