aboutsummaryrefslogtreecommitdiff
path: root/scripts/artv2/components/base_components/pose.py
blob: e9fe9c89bc4f9fb2c980f028351578fd975e6bd8 (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
# -*- coding: utf-8 -*-
"""
:author:
    Jeremy Ernst
:description:
    This module contains the class for a pose object, which has methods for gathering data for creating a pose, loading
    a pose, saving a pose, etc.
"""

import artv2.tools.system.logger.output_logger as logger
import artv2.utilities.general_utilities as utils
import artv2.utilities.rigging_utilities as rig_utils


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class Pose(object):
    """
    Creates a pose object, which can gather data for creating a pose, load a pose, or save a pose. The constructor is
    passed a list of controls for which to either gather data for or load data onto.
    """

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def __init__(self, controls):

        self.logger = logger.OutputLogger(self.__class__.__name__)
        self.controls = utils.convert_to_pynodes(controls)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def create_pose(self, name):
        """
        Gathers the data from this object's controls and returns that data in a dictionary.

        example usage:
            .. code-block:: python

                # create a pose instance with some joints as the "controls"
                import artv2.components.base_components.pose as pose
                cmds.select("root", hi=True)
                poseInst = pose.Pose(cmds.ls(sl=True))

                # with our pose instance, create a pose named "test".
                data = poseInst.create_pose("test")

        :param name: Name for the created pose.
        :return: Dictionary of pose data.
        """

        self.logger.info("{0}.create_pose: Saving pose with name: {1}".format(self.__class__.__name__, name))
        pose_data = {"name": name}

        attrs = ["translateX", "translateY", "translateZ", "rotateX", "rotateY", "rotateZ", "scaleX", "scaleY",
                 "scaleZ"]

        for control in self.controls:
            attr_data = {}
            for attribute in attrs:
                if control.attr(attribute).isKeyable():
                    attr_data[attribute] = control.attr(attribute).get()

            attr_data["worldMatrix"] = control.worldMatrix.get().__melobject__()
            pose_data[control.nodeName(stripNamespace=True)] = attr_data

        return pose_data

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def load_pose(self, pose_data):
        """
        Loads the given pose data on this object's controls.

        example usage:
            .. code-block:: python

                # create a pose instance with some joints as the "controls"
                import artv2.components.base_components.pose as pose
                cmds.select("root", hi=True)
                poseInst = pose.Pose(cmds.ls(sl=True))

                # with our pose instance, load the data from a created pose from earlier.
                poseInst.load_pose(data)

        :param pose_data: pose data dictionary to load onto controls.
        """

        self.logger.info("{0}.load_pose: Loading pose".format(self.__class__.__name__))

        for control in self.controls:
            name = control.name(stripNamespace=True)
            if name in pose_data:
                data = pose_data.get(name)
                for attribute in data:
                    if control.attr(attribute).isKeyable():
                        if rig_utils.attr_is_settable(control.attr(attribute)):
                            value = data.get(attribute)
                            control.attr(attribute).set(value)