Skip to content

Commit

Permalink
Updated AL Python tests to avoid tempfiles and use fixturesUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjondev committed Dec 7, 2021
1 parent d4879a1 commit 294d0fb
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 79 deletions.
30 changes: 12 additions & 18 deletions plugin/al/plugin/AL_USDMayaTestPlugin/py/testLayerManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,24 @@

from pxr import Sdf, Usd

import fixturesUtils


class TestLayerManagerSerialisation(unittest.TestCase):
"""Test cases for layer manager serialisation and deserialisation"""

@classmethod
def setUpClass(cls):
# Reset the output directory
cls._outputPath = os.path.join(os.path.abspath('.'), os.path.splitext(__file__)[0] + "Output")
if os.path.exists(cls._outputPath):
shutil.rmtree(cls._outputPath)
os.mkdir(cls._outputPath)
cls._mayaFilePath = os.path.join(cls._outputPath, 'LayerManagerSerialisation.ma')
cls._usdFilePath = os.path.join(cls._outputPath, 'LayerManagerSerialisation.usda')

# Load the plugin
cmds.loadPlugin("AL_USDMayaPlugin", quiet=True)
# Load the plugin and setup test output
fixturesUtils.setUpClass(__file__, pluginName="AL_USDMayaPlugin")
className = cls.__name__
cls._mayaFilePath = os.path.abspath(className + ".ma")
cls._usdFilePath = os.path.abspath(className + ".usda")

@classmethod
def tearDownClass(cls):
# Unload the plugin
cmds.unloadPlugin("AL_USDMayaPlugin", force=True)
# Unload the plugin and exit test output
fixturesUtils.tearDownClass(pluginName="AL_USDMayaPlugin")

app = 'maya'
def setUp(self):
Expand Down Expand Up @@ -152,7 +150,7 @@ def _initialiseLayers(usdFilePath):
layers = []
for ext in ["usd", "usda", "usdc"]:
_format = Sdf.FileFormat.FindByExtension(ext)
filePath = os.path.join(self._outputPath, 'TestLayer.{}'.format(ext))
filePath = 'TestLayer.{}'.format(ext)
layer = Sdf.Layer.CreateNew(filePath)
layer.comment = ext
layer.Save()
Expand Down Expand Up @@ -209,8 +207,4 @@ def _reloadAndAssert(usdFilePath, proxyName):


if __name__ == "__main__":

tests = [unittest.TestLoader().loadTestsFromTestCase(TestLayerManagerSerialisation),]
results = [unittest.TextTestRunner(verbosity=2).run(test) for test in tests]
exitCode = int(not all([result.wasSuccessful() for result in results]))
cmds.quit(exitCode=(exitCode))
fixturesUtils.runTests(globals(), verbosity=2)
93 changes: 50 additions & 43 deletions plugin/al/plugin/AL_USDMayaTestPlugin/py/testProxyShape.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import json
import os
import tempfile
import shutil
import unittest

Expand All @@ -30,10 +29,21 @@
from maya import cmds
from maya.api import OpenMaya as om2

import fixturesUtils


class TestProxyShapeGetUsdPrimFromMayaPath(unittest.TestCase):
"""Test cases for static function: AL.usdmaya.ProxyShape.getUsdPrimFromMayaPath"""

@classmethod
def setUpClass(cls):
# Setup for tests for output
fixturesUtils.setUpClass(__file__, loadPlugin=False)

@classmethod
def tearDownClass(cls):
fixturesUtils.tearDownClass(unloadPlugin=False)

def __init__(self, *args, **kwargs):
super(TestProxyShapeGetUsdPrimFromMayaPath, self).__init__(*args, **kwargs)

Expand All @@ -48,17 +58,16 @@ def setUp(self):
cmds.loadPlugin("AL_USDMayaPlugin", quiet=True)
self.assertTrue(cmds.pluginInfo("AL_USDMayaPlugin", query=True, loaded=True))

_tmpfile = tempfile.NamedTemporaryFile(delete=True, suffix=".usda")
_tmpfile.close()
_usdFilePath = os.path.abspath(type(self).__name__ + ".usda")

# Ensure sphere geometry exists
self._sphere = cmds.polySphere(constructionHistory=False, name="sphere")[0]
cmds.select(self._sphere)

# Export, new scene, import
cmds.file(_tmpfile.name, exportSelected=True, force=True, type="AL usdmaya export")
cmds.file(_usdFilePath, exportSelected=True, force=True, type="AL usdmaya export")
cmds.file(force=True, new=True)
self._proxyName = cmds.AL_usdmaya_ProxyShapeImport(file=_tmpfile.name)[0]
self._proxyName = cmds.AL_usdmaya_ProxyShapeImport(file=_usdFilePath)[0]

# Ensure proxy exists
self.assertIsNotNone(self._proxyName)
Expand All @@ -67,7 +76,7 @@ def setUp(self):
proxy = AL.usdmaya.ProxyShape.getByName(self._proxyName)
self._stage = proxy.getUsdStage()

os.remove(_tmpfile.name)
os.remove(_usdFilePath)

def tearDown(self):
"""Unload plugin, new Maya scene, reset class member variables."""
Expand Down Expand Up @@ -139,6 +148,15 @@ class MayaUsdTestData:
stage = None # Imported Usd stage that hosts prim
prim = None # Usd prim in stage

@classmethod
def setUpClass(cls):
fixturesUtils.setUpClass(__file__, loadPlugin=False)
cls._className = cls.__name__

@classmethod
def tearDownClass(cls):
fixturesUtils.tearDownClass(unloadPlugin=False)

def __init__(self, *args, **kwargs):
super(TestProxyShapeGetMayaPathFromUsdPrim, self).__init__(*args, **kwargs)

Expand All @@ -156,27 +174,25 @@ def setUp(self):
cmds.loadPlugin("AL_USDMayaPlugin", quiet=True)
self.assertTrue(cmds.pluginInfo("AL_USDMayaPlugin", query=True, loaded=True))

stageA_file = tempfile.NamedTemporaryFile(delete=True, suffix=".usda")
stageB_file = tempfile.NamedTemporaryFile(delete=True, suffix=".usda")
stageA_file.close()
stageB_file.close()
stageA_file = os.path.abspath(self._className + "-StageA" + ".usda")
stageB_file = os.path.abspath(self._className + "-StageB" + ".usda")

cube = cmds.polyCube(constructionHistory=False, name="cube")[0]
sphere = cmds.polySphere(constructionHistory=False, name="cube")[0]

cmds.select(cube, replace=True)
cmds.file(stageA_file.name, exportSelected=True, force=True, type="AL usdmaya export")
cmds.file(stageA_file, exportSelected=True, force=True, type="AL usdmaya export")

cmds.select(sphere, replace=True)
cmds.file(stageB_file.name, exportSelected=True, force=True, type="AL usdmaya export")
cmds.file(stageB_file, exportSelected=True, force=True, type="AL usdmaya export")

self._stageA.poly = cube
self._stageB.poly = sphere

cmds.file(force=True, new=True)

self._stageA.proxyName = cmds.AL_usdmaya_ProxyShapeImport(file=stageA_file.name)[0]
self._stageB.proxyName = cmds.AL_usdmaya_ProxyShapeImport(file=stageB_file.name)[0]
self._stageA.proxyName = cmds.AL_usdmaya_ProxyShapeImport(file=stageA_file)[0]
self._stageB.proxyName = cmds.AL_usdmaya_ProxyShapeImport(file=stageB_file)[0]

self._stageA.proxy = AL.usdmaya.ProxyShape.getByName(self._stageA.proxyName)
self._stageB.proxy = AL.usdmaya.ProxyShape.getByName(self._stageB.proxyName)
Expand All @@ -187,8 +203,8 @@ def setUp(self):
self._stageA.prim = self._stageA.stage.GetPrimAtPath("/{}".format(self._stageA.poly))
self._stageB.prim = self._stageB.stage.GetPrimAtPath("/{}".format(self._stageB.poly))

os.remove(stageA_file.name)
os.remove(stageB_file.name)
os.remove(stageA_file)
os.remove(stageB_file)

def tearDown(self):
"""New Maya scene, unload plugin, reset data."""
Expand Down Expand Up @@ -265,15 +281,14 @@ def test_getMayaPathFromUsdPrim_reopenImport(self):
"""Saving and reopening a Maya scene with dynamic translated prims should work."""

# Save
_file = tempfile.NamedTemporaryFile(delete=False, suffix=".ma")
_file = os.path.abspath(self._className + "-reopenImport" + ".ma")
with _file:
cmds.file(rename=_file.name)
cmds.file(rename=_file)
cmds.file(save=True, force=True)
self.assertFalse(cmds.ls(assemblies=True))
_file.close()

# Re-open
cmds.file(_file.name, open=True, force=True)
cmds.file(_file, open=True, force=True)

# Refresh data, stage wrapper is out of date
_stageC = self.MayaUsdTestData()
Expand All @@ -293,24 +308,20 @@ def test_getMayaPathFromUsdPrim_reopenImport(self):
else:
print('MAYAUSD_DISABLE_VP2_RENDER_DELEGATE enabled, ignoring VP2 render delegate check')

# Cleanup
os.remove(_file.name)

@unittest.skip("Not working")
def test_getMayaPathFromUsdPrim_reopenStaticImport(self):
"""Saving and reopening a Maya scene with static translated prims should work."""

cmds.AL_usdmaya_ProxyShapeImportPrimPathAsMaya(self._stageA.proxyName, primPath=str(self._stageA.prim.GetPath()))

# Save
_file = tempfile.NamedTemporaryFile(delete=False, suffix=".ma")
_file = os.path.abspath(self._className + "-reopenStaticImport" + ".ma")
with _file:
cmds.file(rename=_file.name)
cmds.file(rename=_file)
cmds.file(save=True, force=True)
_file.close()

# Re-open
cmds.file(_file.name, open=True, force=True)
cmds.file(_file, open=True, force=True)

# Refresh data, stage wrapper is out of date
_stageC = self.MayaUsdTestData()
Expand All @@ -330,18 +341,25 @@ def test_getMayaPathFromUsdPrim_reopenStaticImport(self):
else:
print('MAYAUSD_DISABLE_VP2_RENDER_DELEGATE enabled, ignoring VP2 render delegate check')

# Cleanup
os.remove(_file.name)


class TestProxyShapeAnonymousLayer(unittest.TestCase):
workingDir = None
fileName = 'foo.ma'
SC = pxr.UsdUtils.StageCache.Get()

@classmethod
def setUpClass(cls):
# Setup for test output
fixturesUtils.setUpClass(__file__, loadPlugin=False)

@classmethod
def tearDownClass(cls):
fixturesUtils.tearDownClass(unloadPlugin=False)

def setUp(self):
unittest.TestCase.setUp(self)
self.workingDir = tempfile.mkdtemp()
self.workingDir = os.path.abspath(type(self).__name__)
os.mkdir(self.workingDir)
cmds.file(new=True, save=False, force=True)
cmds.loadPlugin("AL_USDMayaPlugin", quiet=True)
self.assertTrue(cmds.pluginInfo("AL_USDMayaPlugin", query=True, loaded=True))
Expand All @@ -363,9 +381,6 @@ def setUp(self):
self.SC.Clear()

def tearDown(self):
if os.path.isdir(self.workingDir):
shutil.rmtree(self.workingDir)

self.SC.Clear()
unittest.TestCase.tearDown(self)

Expand Down Expand Up @@ -439,12 +454,4 @@ def test_fromSessionLayer(self):


if __name__ == "__main__":

tests = [unittest.TestLoader().loadTestsFromTestCase(TestProxyShapeGetUsdPrimFromMayaPath),
unittest.TestLoader().loadTestsFromTestCase(TestProxyShapeGetMayaPathFromUsdPrim),
unittest.TestLoader().loadTestsFromTestCase(TestProxyShapeAnonymousLayer),
unittest.TestLoader().loadTestsFromTestCase(TestProxyShapeVariantFallbacks),
]
results = [unittest.TextTestRunner(verbosity=2).run(test) for test in tests]
exitCode = int(not all([result.wasSuccessful() for result in results]))
cmds.quit(exitCode=(exitCode))
fixturesUtils.runTests(globals(), verbosity=2)
Loading

0 comments on commit 294d0fb

Please sign in to comment.