aboutsummaryrefslogtreecommitdiff
path: root/scripts/artv2/tools/rigging/rig_builder/rig_builder_ui.py
blob: 33807bde0ceb032159c8dfac3bdf9541aefc0897 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# -*- coding: utf-8 -*-

"""
:author:
    Jeremy Ernst
:description:
    This module contains the class that provides the main user interface that is used to build up an asset with
    different rig components.
"""

import os
from functools import partial

import maya.cmds as cmds
import pymel.core as pm
import artv2.utilities.interface_utilities as interface_utils
import artv2.utilities.component_utilities as component_utils
import artv2.utilities.rigging_utilities as rigging_utils
import artv2.utilities.general_utilities as utils
import artv2.tools.system.logger.output_logger as logger
from artv2.tools.rigging.rig_builder.installed_components_widget import InstalledComponentsWidget
from artv2.tools.rigging.rig_builder.available_components_widget import AvailableComponentsWidget
from artv2.tools.rigging.rig_builder.component_context_menu import ComponentContextMenu
from artv2.third_party.Qt import QtWidgets, QtCore, QtGui
import artv2.components.base_components.template as template


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class RigBuilderUI(interface_utils.ARTv2Window):
    """
    This class provides the main user interface that is used to build up an asset with different rig components. It is
    invoked from the ART v2 menu on Maya's main menu bar.

    :param parent: Parent for this widget, which will be Maya's main window.

    .. figure:: /images/rig_builder.png
        :width: 495px
        :align: center
        :height: 659px
        :figclass: align-center

        the rig builder interface.
    """

    SETTINGS_NAME = "RigBuilderWIN"
    WINDOW_NAME = "artv2_rig_builder_window"

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

        super(RigBuilderUI, self).__init__(400, 600, "Rig Builder", parent)

        self.logger = logger.OutputLogger(self.__class__.__name__)

        # define some attributes the class (or other classes) will need to be able to access
        self.toggle_offset = None
        self.toggle_global = None
        self.toggle_lra = None
        self.component_list = None
        self.settings_layout = None

        # build the interface and set states
        self._build_interface()
        self.set_states()

        # select the first item in the list widget
        first_item = self.component_list.item(0)
        first_item.setSelected(True)
        self.component_list.itemClicked.emit(first_item)

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

        menu_bar = self.menuBar()
        menu_bar.addMenu("Settings")

        self.toolbar = self.addToolBar("Tools")

        self.main_widget = QtWidgets.QStackedWidget()
        self.setCentralWidget(self.main_widget)

        self._build_rigging_widget()
        self._build_deformation_widget()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _add_setup_tool_buttons(self, toolbar):

        toolbar.clear()

        load_template = toolbar.addAction("Load Template")
        load_template.setToolTip("Load a template")
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "load.png")))
        load_template.setIcon(icon)
        load_template.triggered.connect(self._load_template)

        save_template = toolbar.addAction("Save Template")
        save_template.setToolTip("Save a template")
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "save.png")))
        save_template.setIcon(icon)
        save_template.triggered.connect(self._save_template)

        toolbar.addSeparator()
        self.toggle_global = toolbar.addAction("G")
        self.toggle_global.setToolTip("Toggle Global Mover Visibility")
        self.toggle_global.setCheckable(True)
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "mover_global.png")))
        self.toggle_global.setIcon(icon)
        self.toggle_global.toggled.connect(self._toggle_global_visibility)

        self.toggle_offset = toolbar.addAction("O")
        self.toggle_offset.setToolTip("Toggle Offset Mover Visibility")
        self.toggle_offset.setCheckable(True)
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "mover_offset.png")))
        self.toggle_offset.setIcon(icon)
        self.toggle_offset.toggled.connect(self._toggle_offset_visibility)

        self.toggle_lra = toolbar.addAction("LRA")
        self.toggle_lra.setToolTip("Toggle Local Rotation Axis Visibility")
        self.toggle_lra.setCheckable(True)
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "lra.png")))
        self.toggle_lra.setIcon(icon)
        self.toggle_lra.toggled.connect(self._toggle_lra_visibility)

        toolbar.addSeparator()
        bone_counter = toolbar.addAction("Count")
        bone_counter.setToolTip("Launch the bone count tool to view current bone count and set targets.")
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "count.png")))
        bone_counter.setIcon(icon)
        bone_counter.triggered.connect(self._launch_bone_counter)

        group_mirror = toolbar.addAction("Group Mirror")
        group_mirror.setToolTip("Create mirrors of multiple components at once.")
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "mirror.png")))
        group_mirror.setIcon(icon)
        group_mirror.triggered.connect(self._launch_group_mirror)

        group_reset = toolbar.addAction("Group Reset")
        group_reset.setToolTip("Reset settings or transformations of multiple components at once.")
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "reset.png")))
        group_reset.setIcon(icon)
        group_reset.triggered.connect(self._launch_group_reset)

        group_mirror_xforms = toolbar.addAction("Group Mirror XForms")
        group_mirror_xforms.setToolTip("Mirror transformations of multiple components at once.")
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "mirror_transforms.png")))
        group_mirror_xforms.setIcon(icon)
        group_mirror_xforms.triggered.connect(self._launch_group_mirror_xforms)

        group_bake_offsets = toolbar.addAction("Group Mirror XForms")
        group_bake_offsets.setToolTip("Bake offsets on multiple components at once.")
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "bake_offsets.png")))
        group_bake_offsets.setIcon(icon)
        group_bake_offsets.triggered.connect(self._launch_group_bake_offsets)

        pose_manager = toolbar.addAction("Pose Manager")
        pose_manager.setToolTip("Open the pose manager for creating and setting bindposes.")
        icon = QtGui.QIcon(QtGui.QPixmap(os.path.join(self.icon_path, "general", "pose_manager.png")))
        pose_manager.setIcon(icon)
        pose_manager.triggered.connect(self._launch_pose_manager)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _build_rigging_widget(self):

        rig_widget = QtWidgets.QWidget()
        self.main_widget.addWidget(rig_widget)
        main_layout = QtWidgets.QVBoxLayout(rig_widget)
        splitter = QtWidgets.QSplitter(orientation=QtCore.Qt.Horizontal)
        main_layout.addWidget(splitter)

        # left column
        left_column_widget = QtWidgets.QWidget()
        left_column_layout = QtWidgets.QVBoxLayout(left_column_widget)

        add_component_button = QtWidgets.QPushButton("Add Component")
        add_component_button.setMinimumHeight(interface_utils.scale_by_dpi(40))
        add_component_button.setMaximumHeight(interface_utils.scale_by_dpi(40))
        add_component_button.setStyleSheet(self.style_sheet)

        component_menu = AvailableComponentsWidget(add_component_button)
        component_menu.component_added.connect(self.update_list)
        add_component_button.setMenu(component_menu)
        left_column_layout.addWidget(add_component_button)

        search_bar = QtWidgets.QLineEdit()
        search_bar.setPlaceholderText("Search..")
        left_column_layout.addWidget(search_bar)

        self.component_list = InstalledComponentsWidget(self)
        self.component_list.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.component_list.customContextMenuRequested.connect(self._item_menu_request)
        left_column_layout.addWidget(self.component_list)
        splitter.addWidget(left_column_widget)

        # right column
        right_column_widget = QtWidgets.QWidget()
        right_column_widget.setMinimumWidth(interface_utils.scale_by_dpi(250))
        right_column_widget.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        right_column_layout = QtWidgets.QVBoxLayout(right_column_widget)

        self.settings_layout = QtWidgets.QVBoxLayout()
        right_column_layout.addLayout(self.settings_layout)

        build_button = QtWidgets.QPushButton("Build Skeleton")
        build_button.setMinimumHeight(interface_utils.scale_by_dpi(40))
        build_button.setMaximumHeight(interface_utils.scale_by_dpi(40))
        build_button.clicked.connect(self._build_skeleton)
        right_column_layout.addWidget(build_button)

        splitter.addWidget(right_column_widget)
        completer = QtWidgets.QCompleter(self.component_list.get_items().keys())
        completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        search_bar.setCompleter(completer)
        search_bar.returnPressed.connect(partial(self.component_list.filter_list, search_bar))

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _build_deformation_widget(self):

        deformation_widget = QtWidgets.QWidget()
        self.main_widget.addWidget(deformation_widget)
        main_layout = QtWidgets.QVBoxLayout(deformation_widget)

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

        button = QtWidgets.QPushButton("Edit Components")
        button.clicked.connect(self._edit_components)
        main_layout.addWidget(button)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _item_menu_request(self, point):

        item = self.component_list.itemAt(point)
        if item is not None:
            node_name = item.data(QtCore.Qt.UserRole)

            menu = ComponentContextMenu(node_name, self.component_list, point, self)
            menu.component_added.connect(self.update_list)
            parent_position = self.component_list.mapToGlobal(QtCore.QPoint(0, 0))
            menu.move(parent_position + point)
            menu.exec_()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _build_skeleton(self):

        self.logger.interface("{0}._build_skeleton: Building skeleton.".format(self.__class__.__name__))
        skeleton = rigging_utils.build_skeleton()
        asset = component_utils.get_rig_asset_node()
        utils.set_attribute(asset, "has_skeleton", True)
        skeleton.asset.connect(asset.skeleton_hierarchy)

        components = component_utils.return_all_components()
        for each in components:
            inst = component_utils.get_component_instance(each)
            inst.joint_mover.toggle_visibility(False)
            del inst

        self.main_widget.setCurrentIndex(1)
        self.toolbar.clear()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _edit_components(self):

        self.logger.interface("{0}._edit_components: Removing skeleton to edit components."
                              "".format(self.__class__.__name__))

        asset = component_utils.get_rig_asset_node()
        utils.set_attribute(asset, "has_skeleton", False)
        root = asset.skeleton_hierarchy.connections()[0]
        root.unlock()
        joints = root.listRelatives(ad=True)
        for joint in joints:
            joint.unlock()
        pm.delete(root)

        components = component_utils.return_all_components()
        for each in components:
            inst = component_utils.get_component_instance(each)
            inst.joint_mover.toggle_visibility(True)
            del inst

        self.main_widget.setCurrentIndex(0)
        self._add_setup_tool_buttons(self.toolbar)
        self.update_list()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    @staticmethod
    def _toggle_global_visibility(*args):

        state = args[0]
        asset = component_utils.get_rig_asset_node()
        utils.set_attribute(asset, "global_mover_visibility", state)

        components = component_utils.return_all_components()
        for each in components:
            inst = component_utils.get_component_instance(each)
            inst.joint_mover.toggle_global_visibility(state)
            del inst

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    @staticmethod
    def _toggle_offset_visibility(*args):

        state = args[0]
        asset = component_utils.get_rig_asset_node()
        utils.set_attribute(asset, "offset_mover_visibility", state)

        components = component_utils.return_all_components()
        for each in components:
            inst = component_utils.get_component_instance(each)
            inst.joint_mover.toggle_offset_visibility(state)
            del inst

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    @staticmethod
    def _toggle_lra_visibility(*args):
        state = args[0]
        asset = component_utils.get_rig_asset_node()
        utils.set_attribute(asset, "lra_visibility", state)

        rigging_utils.toggle_local_axis(state)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _launch_bone_counter(self):

        self.logger.interface("{0}._launch_bone_counter: Launching bone counter interface."
                              "".format(self.__class__.__name__))

        import artv2.tools.rigging.bone_counter_ui as bone_counter
        bone_counter.show_window()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _launch_group_mirror(self):

        self.logger.interface("{0}._launch_group_mirror: Launching group mirror interface."
                              "".format(self.__class__.__name__))

        import artv2.tools.rigging.group_mirror_ui as group_mirror
        group_mirror.show_window(self)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _launch_group_reset(self):

        self.logger.interface("{0}._launch_group_reset: Launching group reset interface."
                              "".format(self.__class__.__name__))

        import artv2.tools.rigging.group_reset_ui as group_reset
        group_reset.show_window(self)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _launch_group_mirror_xforms(self):

        self.logger.interface("{0}._launch_group_mirror_xforms: Launching group mirror transforms interface."
                              "".format(self.__class__.__name__))

        import artv2.tools.rigging.group_mirror_xforms_ui as group_mirror_xforms
        group_mirror_xforms.show_window(self)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _launch_group_bake_offsets(self):

        self.logger.interface("{0}._launch_group_bake_offsets: Launching group bake offsets interface."
                              "".format(self.__class__.__name__))

        import artv2.tools.rigging.group_bake_offsets_ui as group_bake
        group_bake.show_window(self)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _launch_pose_manager(self):

        self.logger.interface("{0}._launch_pose_manager: Launching pose manager interface."
                              "".format(self.__class__.__name__))

        import artv2.tools.rigging.pose_manager_ui as pose
        pose.show_window(self)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _load_template(self):

        self.logger.interface("{0}._load_template: Launching load template interface.".format(self.__class__.__name__))

        template_path = os.path.join(self.tools_path, "resources", "templates")
        path = QtWidgets.QFileDialog.getOpenFileName(self, "Load Template", template_path)

        if os.path.exists(path[0]):
            import artv2.tools.rigging.load_template_ui as load_template
            load_template.show_window(self, utils.path_unify(path[0]))

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _save_template(self):

        self.logger.interface("{0}._save_template: Saving template.".format(self.__class__.__name__))

        template_path = os.path.join(self.tools_path, "resources", "templates")

        if not os.path.exists(template_path):
            os.makedirs(template_path)

        path = QtWidgets.QFileDialog.getSaveFileName(self, "Save Template", template_path, "*.json")
        if os.path.exists(os.path.dirname(path[0])):
            template_instance = template.Template(utils.path_unify(path[0]))
            template_instance.save_template()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def set_states(self):
        """
        Sets the state of the UI and of some of the widgets in the UI. For example, depending on the state of the asset,
        the UI will either display component settings and creation widgets, deformation widgets, or build widgets.
        Also, depending on the state of the asset, other buttons may be toggled on or off.
        """

        asset = component_utils.get_rig_asset_node()
        if asset.has_skeleton.get():
            self.main_widget.setCurrentIndex(1)
            self.toolbar.clear()
        else:
            self.main_widget.setCurrentIndex(0)
            self._add_setup_tool_buttons(self.toolbar)

            asset = component_utils.get_rig_asset_node()
            global_visibility = asset.global_mover_visibility.get()
            offset_visibility = asset.offset_mover_visibility.get()
            lra_visibility = asset.lra_visibility.get()
            self.toggle_global.setChecked(global_visibility)
            self.toggle_global.toggled.emit(global_visibility)
            self.toggle_offset.setChecked(offset_visibility)
            self.toggle_offset.toggled.emit(offset_visibility)
            self.toggle_lra.setChecked(lra_visibility)
            self.toggle_lra.toggled.emit(lra_visibility)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def update_list(self):
        """
        Updates the installed components list and then refreshes the state of the UI.
        """

        self.component_list.refresh_list()
        self.set_states()


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def create_new_asset():
    """
    Creates a new rig asset and root component if one does not exist.

    :return: returns True if the user decided to create a new asset. False if not.
    """

    message_box = interface_utils.ConfirmWidget("Do you want to create a new rig asset?", "", "Rig Builder")
    status = message_box.exec_()
    if status == QtWidgets.QMessageBox.Yes:
        import artv2.components.base_components.rig_asset as rig_asset
        import artv2.components.root as root

        rig_asset.RigAsset()
        root.Root()
        utils.frame_selected()
        return True

    return False


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def show_window():
    """
    Shows the RigBuilderUI instance, and if no asset exists in the scene at time of launch, then also will create a
    new rig asset and root component.
    """

    if cmds.window("artv2_rig_builder_window", exists=True):
        cmds.deleteUI("artv2_rig_builder_window")

    assets = component_utils.get_all_rig_asset_nodes()
    if not assets:
        status = create_new_asset()
        if status:
            _gui = RigBuilderUI(interface_utils.get_maya_window())
            _gui.show()

    else:
        _gui = RigBuilderUI(interface_utils.get_maya_window())
        _gui.show()