aboutsummaryrefslogtreecommitdiff
path: root/scripts/artv2/tools/rigging/pose_manager_ui.py
blob: 08c535150949f38a37bc1198a143a5b793e47cfd (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
# -*- coding: utf-8 -*-

"""
:author:
    Jeremy Ernst
:description:
    This module contains a class for creating a user interface for storing new bind poses, removing bind poses, and
    setting the active bind pose.
"""

import json
from functools import partial
import os

import artv2.utilities.interface_utilities as interface_utils
import artv2.utilities.component_utilities as component_utils
import artv2.utilities.general_utilities as utils
from artv2.third_party.Qt import QtWidgets, QtCore
import artv2.tools.system.logger.output_logger as logger
import artv2.components.base_components.rig_asset as rig
import pymel.core as pm


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class PoseManagerUI(interface_utils.ARTv2Window):
    """
    Creates a user interface that allows the user to create new bind poses, set the active bind pose, or remove
    bind poses. This is invoked from the RigBuilderUI toolbar.

    :param parent: the instance of the RigBuilderUI this widget will be parented to.

    .. figure:: /images/pose_manager_overview.gif
        :width: 717px
        :align: center
        :height: 557px
        :figclass: align-center

        this example shows two poses that have been saved with the pose manager. One for the pose the model was
        delivered in and one for the pose we want to build the rig in.

    """

    SETTINGS_NAME = "PoseManagerWIN"
    WINDOW_NAME = "artv2_pose_manager_window"

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def __init__(self, parent=None):

        super(PoseManagerUI, self).__init__(194, 236, "Pose Manager", parent)

        self.logger = logger.OutputLogger(self.__class__.__name__)
        self.parent_inst = parent
        self.contents_layout = None
        self.scroll_contents = None
        self.scroll_area = None

        self._build_interface()
        self._populate_scroll()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _build_interface(self):

        menu_bar = self.menuBar()
        menu = menu_bar.addMenu("Help")
        menu.addAction("Documentation", self._launch_help)

        main_widget = QtWidgets.QWidget()
        self.main_layout = QtWidgets.QVBoxLayout(main_widget)
        self.setCentralWidget(main_widget)

        self._build_scroll_layout()

        button = QtWidgets.QPushButton("Save New Pose")
        button.clicked.connect(self._save_pose)
        self.main_layout.addWidget(button)

        asset = component_utils.get_rig_asset_node()
        if asset.has_skeleton.get():
            button.setEnabled(False)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _build_scroll_layout(self):

        self.scroll_contents = QtWidgets.QFrame()
        self.scroll_contents.setObjectName("light")
        self.scroll_contents.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        self.scroll_area = QtWidgets.QScrollArea()
        self.main_layout.addWidget(self.scroll_area)
        self.scroll_area.setWidgetResizable(True)
        self.scroll_area.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scroll_area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.scroll_area.setWidget(self.scroll_contents)
        self.contents_layout = QtWidgets.QVBoxLayout(self.scroll_contents)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _populate_scroll(self):

        poses = self._get_poses()
        if poses:
            for each in poses:
                self._add_pose_widget(each)

        spacer = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        self.contents_layout.addSpacerItem(spacer)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _add_pose_widget(self, name):

        layout = QtWidgets.QHBoxLayout()
        layout.setSpacing(2)
        self.contents_layout.addLayout(layout)

        pose_button = QtWidgets.QPushButton(name)
        pose_button.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        pose_button.customContextMenuRequested.connect(partial(self._context_menu, pose_button))
        pose_button.clicked.connect(partial(self._set_pose, pose_button))
        layout.addWidget(pose_button)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _context_menu(self, button, point):

        menu = QtWidgets.QMenu(self)

        menu.addAction("Remove Pose", partial(self._remove_pose, button))
        parent_position = button.mapToGlobal(QtCore.QPoint(0, 0))
        menu.move(parent_position + point)
        menu.exec_()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _set_pose(self, button):

        name = button.text()
        self.logger.interface("{0}._set_pose: Setting pose {1}".format(self.__class__.__name__, name))
        asset = component_utils.get_rig_asset_node()
        asset_instance = rig.RigAsset(network_node=asset.name())
        asset_instance.load_dag_pose(name)
        del asset_instance

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _remove_pose(self, button):

        name = button.text()
        self.logger.interface("{0}._remove_pose: Removing pose {1}".format(self.__class__.__name__, name))
        asset = component_utils.get_rig_asset_node()
        asset_instance = rig.RigAsset(network_node=asset.name())
        asset_instance.remove_dag_pose(name)

        interface_utils.clear_layout(self.contents_layout)
        self._populate_scroll()
        del asset_instance

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _save_pose(self):

        name, okay = QtWidgets.QInputDialog.getText(self, "Add Pose", "Pose Name:", QtWidgets.QLineEdit.Normal, "")
        if okay and name:
            self.logger.interface("{0}._save_pose: Saving pose {1}".format(self.__class__.__name__, name))
            asset = component_utils.get_rig_asset_node()
            asset_instance = rig.RigAsset(network_node=asset.name())
            asset_instance.save_dag_pose(name)

            interface_utils.clear_layout(self.contents_layout)
            self._populate_scroll()
            del asset_instance

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    @staticmethod
    def _get_poses():

        asset = component_utils.get_rig_asset_node()
        if asset.hasAttr("dag_poses"):
            pose_data = json.loads(asset.dag_poses.get())
            return pose_data

        return None

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    @staticmethod
    def _launch_help():

        html_file = os.path.join(utils.return_settings()[0], "docs", "build", "sections", "rigging_tools",
                                 "pose_manager.html")
        utils.launch_web_docs(html_file)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def show_window(parent):
    """
    Shows the PoseManagerUI instance
    """
    if pm.window("artv2_pose_manager_window", exists=True):
        pm.deleteUI("artv2_pose_manager_window")

    gui = PoseManagerUI(parent)
    gui.show()