aboutsummaryrefslogtreecommitdiff
path: root/tests/test_chain_component.py
blob: 1206947cdf6bd30966ad486b5fa3be14b972ed6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
"""
:author:
    Jeremy Ernst
:description:
    This module contains unit tests for the chain component.
"""

import unittest
import maya.cmds as cmds
import artv2.components.base_components.rig_asset as rig_asset
import artv2.components.root as root
import artv2.components.chain as chain
import artv2.components.joint as joint


def format_position(pos):
    return [float("{0:.3f}".format(pos[0])), float("{0:.3f}".format(pos[1])), float("{0:.3f}".format(pos[2]))]


class Chain_Component_Tests(unittest.TestCase):
    """
    Class containing tests for chain module creation and manipulation.
    """

    def _create_asset(self):
        new_asset = rig_asset.RigAsset()
        self.assertEquals(new_asset.name, "asset")
        self.assertEquals(new_asset.network_node, "asset_metadata")
        return new_asset

    def _create_chain_inst(self, prefix):
        if not cmds.objExists("master_metadata"):
            root.Root()
        chain_inst = chain.Chain(prefix=prefix)
        chain_inst.parent = "root"
        return chain_inst

    def test_create_chain(self):

        self._create_asset()
        chain_inst = self._create_chain_inst("")
        self.assertEquals(len(chain_inst.joint_mover.get_created_joints()), 3)

    def test_delete_chain(self):

        self._create_asset()
        chain_inst = self._create_chain_inst("")
        joint_inst = joint.Joint()
        joint_inst.parent = "chain_03"

        component_containers = chain_inst.joint_mover.get_containers()
        guide_nodes = component_containers[1].getNodeList()
        utility_nodes = component_containers[2].getNodeList()
        chain_inst.delete()

        self.assertFalse(cmds.objExists("chain_metadata"))

        for each in guide_nodes:
            self.assertFalse(cmds.objExists(each.nodeName()))
        for each in utility_nodes:
            self.assertFalse(cmds.objExists(each.nodeName()))
        self.assertEquals(joint_inst.parent, "root")

    def test_bake_offsets(self):

        self._create_asset()
        chain_inst = self._create_chain_inst("")
        chain_inst.joint_mover.aim_helper.toggle_aim_mode()
        cmds.setAttr("chain_chain_02_mover_offset.translateY", 10)

        pre_offset_position = format_position(cmds.xform("chain_chain_02_mover_offset", q=True, ws=True, t=True))
        pre_main_position = format_position(cmds.xform("chain_chain_02_mover", q=True, ws=True, t=True))
        pre_end_position = format_position(cmds.xform("chain_chain_03_mover", q=True, ws=True, t=True))

        self.assertNotEquals(pre_main_position, pre_offset_position)

        chain_inst.joint_mover.bake_offsets()

        post_offset_position = format_position(cmds.xform("chain_chain_02_mover_offset", q=True, ws=True, t=True))
        post_main_position = format_position(cmds.xform("chain_chain_02_mover", q=True, ws=True, t=True))
        post_end_position = format_position(cmds.xform("chain_chain_03_mover", q=True, ws=True, t=True))

        self.assertEquals(post_main_position, post_offset_position)
        self.assertEquals(pre_end_position, post_end_position)

    def test_pin_component(self):

        self._create_asset()
        chain_inst = self._create_chain_inst("")

        pre_position = format_position(cmds.xform("chain_chain_01_mover", q=True, ws=True, t=True))
        chain_inst.joint_mover.toggle_pin_component()
        self.assertTrue(chain_inst.network_node.pinned.get())

        cmds.setAttr("master_root_mover.translateX", 100)
        post_position = format_position(cmds.xform("chain_chain_01_mover", q=True, ws=True, t=True))
        self.assertEquals(pre_position, post_position)

        chain_inst.joint_mover.toggle_pin_component()
        cmds.setAttr("master_root_mover.translateX", 0)
        new_position = format_position(cmds.xform("chain_chain_01_mover", q=True, ws=True, t=True))
        self.assertNotEquals(post_position, new_position)

    def test_aim_mode(self):

        self._create_asset()
        chain_inst = self._create_chain_inst("")
        chain_inst.joint_mover.aim_helper.toggle_aim_mode()

        self.assertEquals(chain_inst.network_node.isAiming.get(), True)
        self.assertNotEquals(len(chain_inst.network_node.aimModeConstraints.connections()), 0)

        original_orientation = cmds.xform("chain_chain_01_mover_offset", q=True, ws=True, ro=True)
        cmds.setAttr("chain_chain_02_mover.translateY", 10)
        new_orientation = cmds.xform("chain_chain_01_mover_offset", q=True, ws=True, ro=True)

        self.assertNotEquals(original_orientation, new_orientation)

        chain_inst.joint_mover.aim_helper.toggle_aim_mode()
        self.assertEquals(chain_inst.network_node.isAiming.get(), False)
        self.assertEquals(len(chain_inst.network_node.aimModeConstraints.connections()), 0)

        original_orientation = cmds.xform("chain_chain_01_mover_offset", q=True, ws=True, ro=True)
        cmds.setAttr("chain_chain_02_mover.translateY", 0)
        new_orientation = cmds.xform("chain_chain_01_mover_offset", q=True, ws=True, ro=True)

        self.assertEquals(original_orientation, new_orientation)

    def test_duplicate(self):

        self._create_asset()
        chain_inst = self._create_chain_inst("")

        cmds.setAttr("chain_chain_01_mover.translateY", -10)
        cmds.setAttr("chain_chain_01_mover.rotateY", 30)
        cmds.setAttr("chain_chain_01_mover.rotateZ", 42)
        cmds.setAttr("chain_chain_01_mover.scaleY", 1.319)
        cmds.setAttr("chain_chain_01_mover.scaleX", 1.319)
        cmds.setAttr("chain_chain_01_mover.scaleZ", 1.319)
        cmds.setAttr("chain_chain_02_mover.rotateZ", -25)

        duplicate_inst = chain_inst.duplicate()

        self.assertEquals(duplicate_inst.parent, "root")
        self.assertEquals(cmds.getAttr("chain_01_chain_01_mover.ty"), cmds.getAttr("chain_chain_01_mover.ty"))
        self.assertEquals(cmds.getAttr("chain_01_chain_01_mover.ry"), cmds.getAttr("chain_chain_01_mover.ry"))
        self.assertEquals(cmds.getAttr("chain_01_chain_01_mover.rz"), cmds.getAttr("chain_chain_01_mover.rz"))
        self.assertEquals(cmds.getAttr("chain_01_chain_01_mover.sx"), cmds.getAttr("chain_chain_01_mover.sx"))
        self.assertEquals(cmds.getAttr("chain_01_chain_01_mover.sy"), cmds.getAttr("chain_chain_01_mover.sy"))
        self.assertEquals(cmds.getAttr("chain_01_chain_01_mover.sz"), cmds.getAttr("chain_chain_01_mover.sz"))

        self.assertEquals(cmds.getAttr("chain_01_chain_02_mover.rz"), cmds.getAttr("chain_chain_02_mover.rz"))
        chain_inst.num_joints = 5
        dupe2 = chain_inst.duplicate()
        dupe3 = chain_inst.duplicate()
        dupe4 = chain_inst.duplicate()
        self.assertEquals(dupe2.suffix, "_02")
        self.assertEquals(dupe3.suffix, "_03")
        self.assertEquals(dupe4.suffix, "_04")

    def test_invalid_property_values(self):

        self._create_asset()
        chain_inst = self._create_chain_inst("")

        with self.assertRaises(RuntimeError):
            chain_inst.num_joints = 100

        with self.assertRaises(RuntimeError):
            chain_inst.num_joints = 3.5

        with self.assertRaises(RuntimeError):
            chain_inst.num_joints = "3"

        with self.assertRaises(RuntimeError):
            chain_inst.num_joints = 0

    def test_num_spine_bones(self):

        self._create_asset()
        chain_inst = self._create_chain_inst("")
        chain_inst.num_joints = 10
        joint_inst = joint.Joint()
        joint_inst.parent = "chain_10"
        chain_inst.num_joints = 3
        self.assertEquals(joint_inst.parent, "root")
        chain_inst.num_joints = 5

    def test_right_side(self):

        self._create_asset()
        l_chain_inst = self._create_chain_inst("l")
        r_chain_inst = self._create_chain_inst("r")
        r_chain_inst.set_side("right")
        test_chain_inst = self._create_chain_inst("test")

        l_joints = sorted(l_chain_inst.joint_mover.get_driven_joints())
        r_joints = sorted(r_chain_inst.joint_mover.get_driven_joints())
        test_joints = sorted(test_chain_inst.joint_mover.get_driven_joints())

        for i in range(len(l_joints)):

            l_joint = cmds.xform(l_joints[i].nodeName(), q=True, ws=True, ro=True)
            r_joint = cmds.xform(r_joints[i].nodeName(), q=True, ws=True, ro=True)
            test_joint = cmds.xform(test_joints[i].nodeName(), q=True, ws=True, ro=True)

            self.assertNotEquals(l_joint, r_joint)
            self.assertEquals(l_joint, test_joint)

    def test_set_side(self):

        self._create_asset()
        l_chain_inst = self._create_chain_inst("l_index")
        l_chain_inst.set_side("right")
        l_chain_inst.set_side("left")

        l_chain_inst.create_mirror(prefix="r", suffix="", parent="root")

    def test_create_mirror(self):

        self._create_asset()
        left_chain_inst = self._create_chain_inst("l")

        cmds.setAttr("l_chain_chain_01_mover.translateZ", -20)
        cmds.setAttr("l_chain_chain_01_mover.rotateZ", 40)
        cmds.setAttr("master_root_mover.scaleX", 2.5)
        cmds.setAttr("master_root_mover.scaleY", 2.5)
        cmds.setAttr("master_root_mover.scaleZ", 2.5)
        cmds.setAttr("master_root_mover.translateZ", 60)

        right_chain_inst = left_chain_inst.create_mirror(prefix="r", suffix="", parent="root")

        left_movers = left_chain_inst.joint_mover.get_movers()
        left_global_movers = sorted(left_movers.get("global"))
        right_movers = right_chain_inst.joint_mover.get_movers()
        right_global_movers = sorted(right_movers.get("global"))

        self.assertEquals(cmds.getAttr("r_chain_chain_01_mover.translateZ"), 20)
        self.assertEquals(cmds.getAttr("r_chain_chain_01_mover.rotateZ"), 40)
        self.assertEquals(cmds.getAttr("r_chain_chain_01_mover_grp.scaleX"), 2.5)

        for mover in left_global_movers:
            index = left_global_movers.index(mover)
            loc = cmds.spaceLocator()[0]
            cmds.delete(cmds.parentConstraint(mover.nodeName(), loc)[0])
            group = cmds.group(empty=True)
            cmds.parent(loc, group)
            cmds.setAttr(group + ".scaleX", -1)

            loc_pos = format_position(cmds.xform(loc, q=True, ws=True, t=True))
            mirror_pos = format_position(cmds.xform(right_global_movers[index].nodeName(), q=True, ws=True, t=True))
            self.assertEquals(loc_pos, mirror_pos)

    def test_delete_mirror(self):

        self._create_asset()
        left_chain_inst = self._create_chain_inst("l")
        right_chain_inst = left_chain_inst.create_mirror(prefix="r", suffix="", parent="root")

        left_chain_inst.delete()

        self.assertEquals(right_chain_inst.network_node.has_mirror.get(), False)
        self.assertEquals(len(right_chain_inst.network_node.mirror_component.connections()), 0)

    def test_mirror_transforms(self):

        self._create_asset()
        left_chain_inst = self._create_chain_inst("l")
        left_chain_inst.create_mirror(prefix="r", suffix="", parent="root")

        cmds.setAttr("l_chain_chain_01_mover.translateZ", -20)
        cmds.setAttr("l_chain_chain_01_mover.rotateZ", 40)

        left_chain_inst.joint_mover.mirror_transforms()

        self.assertEquals(cmds.getAttr("r_chain_chain_01_mover.translateZ"), 20)
        self.assertEquals(cmds.getAttr("r_chain_chain_01_mover.rotateZ"), 40)