1#!/pxrpythonsubst
2#
3# Copyright 2020 Pixar
4#
5# Licensed under the Apache License, Version 2.0 (the "Apache License")
6# with the following modification; you may not use this file except in
7# compliance with the Apache License and the following modification to it:
8# Section 6. Trademarks. is deleted and replaced with:
9#
10# 6. Trademarks. This License does not grant permission to use the trade
11#    names, trademarks, service marks, or product names of the Licensor
12#    and its affiliates, except as required to comply with Section 4(c) of
13#    the License and to reproduce the content of the NOTICE file.
14#
15# You may obtain a copy of the Apache License at
16#
17#     http://www.apache.org/licenses/LICENSE-2.0
18#
19# Unless required by applicable law or agreed to in writing, software
20# distributed under the Apache License with the above modification is
21# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22# KIND, either express or implied. See the Apache License for the specific
23# language governing permissions and limitations under the Apache License.
24
25import unittest
26
27from pxr import Gf
28from pxr import Usd
29from pxr import UsdAbc
30from pxr import UsdGeom
31
32
33class TestUsdAbcConversionHermiteCurves(unittest.TestCase):
34    @classmethod
35    def setUpClass(cls):
36        usdFile = 'original.usda'
37        abcFile = 'converted.abc'
38
39        UsdAbc._WriteAlembic(usdFile, abcFile)
40        cls.stage = Usd.Stage.Open(abcFile)
41
42    def _assertElementsAlmostEqual(self, seq1, seq2):
43        self.assertTrue(all(Gf.IsClose(e1, e2, 1e-5)
44                        for e1, e2 in zip(seq1, seq2)))
45
46    def _assertEmpty(self, sequence):
47        self.assertFalse(sequence)
48
49    def test_RoundTripHermite(self):
50        time = Usd.TimeCode.EarliestTime()
51        prim = self.stage.GetPrimAtPath('/Cubic/Ribbons/VaryingWidth')
52        schema = UsdGeom.HermiteCurves(prim)
53
54        # Interpolation metadata
55        normalsInterpolation = schema.GetNormalsInterpolation()
56        widthsInterpolation = schema.GetWidthsInterpolation()
57        self.assertEqual(normalsInterpolation, UsdGeom.Tokens.varying)
58        self.assertEqual(widthsInterpolation, UsdGeom.Tokens.varying)
59
60        # These attributes may be varying time sampled
61        curveVertexCounts = schema.GetCurveVertexCountsAttr().Get(time)
62        points = schema.GetPointsAttr().Get(time)
63        tangents = schema.GetTangentsAttr().Get(time)
64        widths = schema.GetWidthsAttr().Get(time)
65        normals = schema.GetNormalsAttr().Get(time)
66
67        self._assertElementsAlmostEqual(
68            points, [(0, 0, 0), (1, 1, 0), (2, 0, 0)])
69        self._assertElementsAlmostEqual(
70            tangents, [(0, 1, 0), (1, 0, 0), (0, -1, 0)])
71        self._assertElementsAlmostEqual(widths, [0, .5, 0])
72        self._assertElementsAlmostEqual(
73             normals, [(0, 0, 1), (0, 0, 1), (0, 0, 1)])
74        self.assertEqual(list(curveVertexCounts), [3])
75
76    def test_RoundTripHermiteWithVelocities(self):
77        """Round tripping velocities is ambiguous"""
78        time = Usd.TimeCode.EarliestTime()
79        prim = self.stage.GetPrimAtPath('/Cubic/Tubes/WithVelocities')
80        schema = UsdGeom.HermiteCurves(prim)
81
82        # Interpolation metadata
83        widthsInterpolation = schema.GetWidthsInterpolation()
84        self.assertEqual(widthsInterpolation, UsdGeom.Tokens.varying)
85
86        # These attributes may be varying time sampled
87        curveVertexCounts = schema.GetCurveVertexCountsAttr().Get(time)
88        points = schema.GetPointsAttr().Get(time)
89        velocities = schema.GetVelocitiesAttr().Get(time)
90        tangents = schema.GetTangentsAttr().Get(time)
91        widths = schema.GetWidthsAttr().Get(time)
92
93        self._assertElementsAlmostEqual(points, [(0, 0, 0), (1, 1, 0)])
94        self._assertElementsAlmostEqual(tangents, [(0, 1, 0), (1, 0, 0)])
95        self._assertElementsAlmostEqual(widths, [0, .5])
96        self._assertEmpty(velocities)
97        self.assertEqual(list(curveVertexCounts), [2])
98
99
100if __name__ == '__main__':
101    unittest.main()
102