1##-*****************************************************************************
2##
3## Copyright (c) 2009-2011,
4##  Sony Pictures Imageworks, Inc. and
5##  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6##
7## All rights reserved.
8##
9## Redistribution and use in source and binary forms, with or without
10## modification, are permitted provided that the following conditions are
11## met:
12## *       Redistributions of source code must retain the above copyright
13## notice, this list of conditions and the following disclaimer.
14## *       Redistributions in binary form must reproduce the above
15## copyright notice, this list of conditions and the following disclaimer
16## in the documentation and/or other materials provided with the
17## distribution.
18## *       Neither the name of Sony Pictures Imageworks, nor
19## Industrial Light & Magic nor the names of their contributors may be used
20## to endorse or promote products derived from this software without specific
21## prior written permission.
22##
23## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34##
35##-*****************************************************************************
36
37from   maya import cmds as MayaCmds
38import maya.OpenMaya as OpenMaya
39import os
40import subprocess
41import unittest
42import util
43
44
45def createJoints():
46
47    name = MayaCmds.joint(position=(0, 0, 0))
48    MayaCmds.rotate(33.694356, 4.000428, 61.426019, r=True, ws=True)
49    MayaCmds.joint(position=(0, 4, 0), orientation=(0.0, 45.0, 90.0))
50    MayaCmds.rotate(62.153171, 0.0, 0.0, r=True, os=True)
51    MayaCmds.joint(position=(0, 8, -1), orientation=(90.0, 0.0, 0.0))
52    MayaCmds.rotate(70.245162, -33.242019, 41.673097, r=True, os=True)
53    MayaCmds.joint(position=(0, 12, 3))
54    MayaCmds.rotate(0.0, 0.0, -58.973851, r=True, os=True)
55
56    return name
57
58class JointTest(unittest.TestCase):
59
60    def setUp(self):
61        MayaCmds.file(new=True, force=True)
62        self.__files = []
63        self.__abcStitcher = [os.environ['AbcStitcher']]
64
65    def tearDown(self):
66        for f in self.__files:
67            os.remove(f)
68
69    def testStaticJointRW(self):
70
71        name = createJoints()
72
73        # write to file
74        self.__files.append(util.expandFileName('testStaticJoints.abc'))
75        MayaCmds.AbcExport(j='-root %s -file %s' % (name, self.__files[-1]))
76        MayaCmds.select(name)
77        MayaCmds.group(name='original')
78
79        # read from file
80        MayaCmds.AbcImport(self.__files[-1], mode='import')
81
82        # make sure the translate and rotation are the same
83        nodes1 = ["|original|joint1", "|original|joint1|joint2", "|original|joint1|joint2|joint3", "|original|joint1|joint2|joint3|joint4"]
84        nodes2 = ["|joint1", "|joint1|joint2", "|joint1|joint2|joint3", "|joint1|joint2|joint3|joint4"]
85        for i in range(0, 4):
86            self.failUnlessAlmostEqual(MayaCmds.getAttr(nodes1[i]+'.tx'), MayaCmds.getAttr(nodes2[i]+'.tx'), 4)
87            self.failUnlessAlmostEqual(MayaCmds.getAttr(nodes1[i]+'.ty'), MayaCmds.getAttr(nodes2[i]+'.ty'), 4)
88            self.failUnlessAlmostEqual(MayaCmds.getAttr(nodes1[i]+'.tz'), MayaCmds.getAttr(nodes2[i]+'.tz'), 4)
89
90    def testStaticIKRW(self):
91
92        name = createJoints()
93        MayaCmds.ikHandle(sj=name, ee='joint4')
94        MayaCmds.move(-1.040057, -7.278225, 6.498725, r=True)
95
96        # write to file
97        self.__files.append(util.expandFileName('testStaticIK.abc'))
98        MayaCmds.AbcExport(j='-root %s -f %s' % (name, self.__files[-1]))
99        MayaCmds.select(name)
100        MayaCmds.group(name='original')
101
102        # read from file
103        MayaCmds.AbcImport(self.__files[-1], mode='import')
104
105        # make sure the translate and rotation are the same
106        nodes1 = ["|original|joint1", "|original|joint1|joint2", "|original|joint1|joint2|joint3", "|original|joint1|joint2|joint3|joint4"]
107        nodes2 = ["|joint1", "|joint1|joint2", "|joint1|joint2|joint3", "|joint1|joint2|joint3|joint4"]
108        for i in range(0, 4):
109            self.failUnlessAlmostEqual(MayaCmds.getAttr(nodes1[i]+'.tx'), MayaCmds.getAttr(nodes2[i]+'.tx'), 4)
110            self.failUnlessAlmostEqual(MayaCmds.getAttr(nodes1[i]+'.ty'), MayaCmds.getAttr(nodes2[i]+'.ty'), 4)
111            self.failUnlessAlmostEqual(MayaCmds.getAttr(nodes1[i]+'.tz'), MayaCmds.getAttr(nodes2[i]+'.tz'), 4)
112
113    def testAnimIKRW(self):
114
115        name = createJoints()
116        handleName = MayaCmds.ikHandle(sj=name, ee='joint4')[0]
117        MayaCmds.currentTime(1, update=True)
118        MayaCmds.setKeyframe(handleName, breakdown=0, hierarchy='none', controlPoints=False, shape=False)
119        MayaCmds.currentTime(16, update=True)
120        MayaCmds.move(-1.040057, -7.278225, 6.498725, r=True)
121        MayaCmds.setKeyframe(handleName, breakdown=0, hierarchy='none', controlPoints=False, shape=False)
122
123        self.__files.append(util.expandFileName('testAnimIKRW.abc'))
124        self.__files.append(util.expandFileName('testAnimIKRW01_08.abc'))
125        self.__files.append(util.expandFileName('testAnimIKRW09-16.abc'))
126
127        # write to files
128        MayaCmds.AbcExport(j='-fr 1 8 -root %s -f %s' % (name, self.__files[-2]))
129        MayaCmds.AbcExport(j='-fr 9 16 -root %s -f %s' % (name, self.__files[-1]))
130        MayaCmds.select(name)
131        MayaCmds.group(name='original')
132
133        subprocess.call(self.__abcStitcher + self.__files[-3:])
134
135        # read from file
136        MayaCmds.AbcImport(self.__files[-3], mode='import')
137
138        # make sure the translate and rotation are the same
139        nodes1 = ["|original|joint1", "|original|joint1|joint2", "|original|joint1|joint2|joint3", "|original|joint1|joint2|joint3|joint4"]
140        nodes2 = ["|joint1", "|joint1|joint2", "|joint1|joint2|joint3", "|joint1|joint2|joint3|joint4"]
141
142        for t in range(1, 25):
143            MayaCmds.currentTime(t, update=True)
144            for i in range(0, 4):
145                self.failUnlessAlmostEqual(MayaCmds.getAttr(nodes1[i]+'.tx'), MayaCmds.getAttr(nodes2[i]+'.tx'), 4)
146                self.failUnlessAlmostEqual(MayaCmds.getAttr(nodes1[i]+'.ty'), MayaCmds.getAttr(nodes2[i]+'.ty'), 4)
147                self.failUnlessAlmostEqual(MayaCmds.getAttr(nodes1[i]+'.tz'), MayaCmds.getAttr(nodes2[i]+'.tz'), 4)
148