Skip to content

Instantly share code, notes, and snippets.

@pmolodo
Last active August 31, 2017 19:03
Show Gist options
  • Save pmolodo/b7b1a1a36331ce6d6148439a1999939c to your computer and use it in GitHub Desktop.
Save pmolodo/b7b1a1a36331ce6d6148439a1999939c to your computer and use it in GitHub Desktop.
Export and timed import test of usdmaya xforms
# create a bunch of transforms with varying transform attrs set
from random import Random
import itertools
import os
import pprint
import maya.cmds as cmds
ATTRS = {
'translate': (.01, 5),
# we do rotates separately, so we can see "rotateY" and "rotateXYZ" ops
'rotateX': (.01, 359.99),
'rotateY': (.01, 359.99),
'rotateZ': (.01, 359.99),
'scale': (1.01, 2.0),
'shear': (1.01, 2.0),
'rotateOrder': (1, 5),
# it seems that internally rotateAxis is stored as a quaternion...
# so to ensure proper roundtripping, keep values 0 < x < 90
'rotateAxis': (.01, 89.99),
'rotatePivot': (.01, 5),
'scalePivot': (.01, 5),
'rotatePivotTranslate': (.01, 5),
'scalePivotTranslate': (.01, 5),
}
rand = Random(3)
if not cmds.pluginInfo('pxrUsd', q=1, loaded=1):
cmds.loadPlugin('pxrUsd')
cmds.file(new=1, f=1)
allNodes = []
allExpected = {}
topPrim = cmds.createNode('transform', name='topPrim')
# Iterate through all combinations of whether each attr in ATTRS is set or not
for i, enabledArray in enumerate(itertools.product((False, True), repeat=len(ATTRS))):
# name will be like: mayaXform_000111010001
node = 'mayaXform_{}'.format(''.join(str(int(x)) for x in enabledArray))
node = cmds.createNode('transform', name=node, parent=topPrim)
attrVals = {}
allNodes.append(node)
allExpected[node] = attrVals
for enabled, (attr, (valMin, valMax)) in itertools.izip(enabledArray,
ATTRS.iteritems()):
if not enabled:
if attr in ('rotateOrder', 'rotateX', 'rotateY', 'rotateZ'):
attrVals[attr] = 0
elif attr == 'scale':
attrVals[attr] = (1, 1, 1)
else:
attrVals[attr] = (0, 0, 0)
else:
if attr == 'rotateOrder':
# 1 - 5 because 0 (xyz) would correspond to "not enabled"
val = rand.randint(1, 5)
elif attr in ('rotateX', 'rotateY', 'rotateZ'):
val = rand.uniform(valMin, valMax)
else:
val = (rand.uniform(valMin, valMax),
rand.uniform(valMin, valMax),
rand.uniform(valMin, valMax))
attrVals[attr] = val
#node.setAttr(attr, val)
if isinstance(val, tuple):
cmds.setAttr("{}.{}".format(node, attr), *val)
else:
cmds.setAttr("{}.{}".format(node, attr), val)
# Now write out a usd file with all our xforms...
cmds.select(allNodes)
usdPath = os.path.expanduser('~/UsdImportMayaXformVariationsTest.usda')
cmds.usdExport(selection=1, file=usdPath)
# time import of usd with lots of xforms
import os
from datetime import datetime, timedelta
import pymel.core as pm
if not pm.pluginInfo('pxrUsd', q=1, loaded=1):
pm.loadPlugin('pxrUsd')
numRepeats = 100
times = []
pm.newFile(f=1)
usdaPath = os.path.expanduser('~/UsdImportMayaXformVariationsTest.usda')
# do one untimed stage import to get it in the usd cache...
pm.usdImport(file=usdaPath)
pm.delete('topPrim')
for i in xrange(numRepeats):
start = datetime.now()
pm.usdImport(file=usdaPath)
end = datetime.now()
times.append(end - start)
pm.delete('topPrim')
print "Time to import ({} / {}): {}" .format(i + 1, numRepeats, times[-1])
totalTime = sum(times, timedelta())
averageTime = totalTime / len(times)
print "total time ({} trials): {}".format(numRepeats, totalTime)
print "average time: {}".format(averageTime)
# average w/o change (release, 100x): 0:00:02.298148
# average w/ change (release, 100x): 0:00:02.251315
# average w/o change (release, 100x): 0:00:02.312542
# average w/ change (release, 100x): 0:00:02.276570
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment