aboutsummaryrefslogtreecommitdiff
path: root/Core/Scripts/Interfaces/ART_AddModuleToCanvas.py
blob: a339b5b0f3991de5ed371411aa45fcaa12116ce3 (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
"""
Author: Jeremy Ernst
"""

# import statements
import maya.cmds as cmds

import System.utils as utils
import System.interfaceUtils as interfaceUtils
from ThirdParty.Qt import QtGui, QtCore, QtWidgets


class ART_AddModuleToCanvas(object):
    """
    This tool presents a UI that lists all modules not currently on the animation picker canvas, and allows the user
    to choose modules from the list to add to the animation picker canvas. This is an animator-facing tool.

    .. image:: /images/addModuleToCanvas.png

    """

    def __init__(self, animPickerUI, modulesToAdd, parent=None):
        """
        initialize the class, get the QSettings for the tool, and call on buildUI.

        :param animPickerUI: the instance of the animation picker class that launched this class.
        :param modulesToAdd: A list of the modules to add, which is a list of modules currently not on the animPickerUI

        """
        super(ART_AddModuleToCanvas, self).__init__()
        # get the directory path of the tools
        settings = QtCore.QSettings("Epic Games", "ARTv2")
        self.toolsPath = settings.value("toolsPath")
        self.iconsPath = settings.value("iconPath")
        self.scriptPath = settings.value("scriptPath")
        self.projectPath = settings.value("projectPath")

        self.pickerUI = animPickerUI
        self.modules = []
        self.modulesToAdd = modulesToAdd

        # assign close event
        self.closeEvent = self.closeWin

        # build the UI
        self.buildUI()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def buildUI(self):
        """
        Builds the interface for the tool, showing a list of modules not currently displayed on the picker.

        """

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

        # create the main window
        self.mainWin = QtWidgets.QMainWindow(self.pickerUI)

        # create the main widget
        self.mainWidget = QtWidgets.QWidget()
        self.mainWin.setCentralWidget(self.mainWidget)

        # create the mainLayout
        self.layout = QtWidgets.QVBoxLayout(self.mainWidget)

        # load stylesheet
        styleSheetFile = utils.returnNicePath(self.toolsPath, "Core/Scripts/Interfaces/StyleSheets/animPicker.qss")
        f = open(styleSheetFile, "r")
        self.style = f.read()
        f.close()

        self.mainWin.setStyleSheet(self.style)

        self.mainWin.setMinimumSize(QtCore.QSize(250, 400))
        self.mainWin.setMaximumSize(QtCore.QSize(250, 400))
        self.mainWin.resize(250, 400)

        # set qt object name
        self.mainWin.setObjectName("pyART_AddToCanvasWIN")
        self.mainWin.setWindowTitle("Add Module To Canvas")

        # label, listWidget, button
        label = QtWidgets.QLabel("Available Modules:")
        label.setProperty("boldFont", True)
        self.layout.addWidget(label)

        self.moduleList = QtWidgets.QListWidget()
        self.moduleList.setMaximumSize(230, 300)
        self.moduleList.setMinimumSize(230, 300)
        self.layout.addWidget(self.moduleList)

        # add modules to listWidget
        self.addModulesToList()

        # create add button
        button = QtWidgets.QPushButton("Add Selected To Canvas")
        self.layout.addWidget(button)
        button.setObjectName("blueButton")
        button.clicked.connect(self.addSelectedToCanvas)

        # show ui
        self.mainWin.show()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def addModulesToList(self):
        """
        Take the modules passed into the class, and if the modules do not already exist on the canvas, add them to
        the list widget.

        """

        existing = self.getExistingModules()

        for module in self.modulesToAdd:
            if module not in existing:
                modName = cmds.getAttr(module + ".moduleName")

                # add to listWIdget
                index = self.pickerUI.characterTabs.currentIndex()
                widget = self.pickerUI.characterTabs.widget(index)
                characterNode = widget.property("charNode")

                item = QtWidgets.QListWidgetItem(modName)
                item.setData(QtCore.Qt.UserRole, [module, characterNode])
                self.moduleList.addItem(item)

        # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
        # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
        # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
        # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
        # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def addSelectedToCanvas(self):
        """
        Takes the selected module in the list widget and adds it to the picker canvas. The picker UI is contained in
        the self.pickerUI.

        """

        selected = self.moduleList.currentItem()
        module = selected.data(QtCore.Qt.UserRole)[0]

        index = self.pickerUI.characterTabs.currentIndex()
        widget = self.pickerUI.characterTabs.widget(index)
        characterNode = widget.property("charNode")

        # get inst
        modType = cmds.getAttr(module + ".moduleType")
        modName = cmds.getAttr(module + ".moduleName")
        mod = __import__("RigModules." + modType, {}, {}, [modType])
        reload(mod)

        # get the class name from that module file (returns RigModules.ART_Root.ART_Root for example)
        moduleClass = getattr(mod, mod.className)

        # find the instance of that module
        moduleInst = moduleClass(self, modName)
        self.modules.append(moduleInst)

        scene = self.getCurrentCanvasTab()

        # find out if charNode has a namespace
        if cmds.objExists(characterNode + ".namespace"):
            namespace = cmds.getAttr(characterNode + ".namespace") + ":"
        else:
            namespace = ""

        # pass in the network node and the namespace
        picker = moduleInst.pickerUI(scene.sceneRect().center(), self.pickerUI, module, namespace)
        scene.addItem(picker[0])
        self.pickerUI.selectionScriptJobs.append(picker[2])

        # =======================================================================
        # #mirror the module's pickerBorderItem if needed
        # =======================================================================
        if picker[1] == True:
            picker[0].setTransformOriginPoint(picker[0].boundingRect().center())
            picker[0].setTransform(QtGui.QTransform(-1.0, 0.0, 0.0, 1.0, picker[0].boundingRect().width() * 2, 0.0))

            children = picker[0].childItems()
            if children is not None:
                self.mirrorChildren(children)

        row = self.moduleList.row(selected)
        self.moduleList.takeItem(row)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def mirrorChildren(self, children):
        """
        Take the passed in children and mirror the item so the text is not backwards. When a module is added to the
        canvas, if it was a right side module, it will be mirrored. This then unmirrors the text.

        :param children: List of QGraphicsSimpleTextItems that need to be mirrored.

        """

        # for mirroring text on any child items of a pickerBorderItem
        for child in children:
            if type(child) == QtWidgets.QGraphicsSimpleTextItem:
                child.setTransformOriginPoint(child.boundingRect().center())
                child.setTransform(QtGui.QTransform(-1.0, 0.0, 0.0, 1.0, child.boundingRect().width(), 0.0))

            children = child.childItems()
            if children is not None:
                self.mirrorChildren(children)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    def getCurrentCanvasTab(self):
        """
        Get the current tab of the current character of the animation picker.
        :return: returns the QGraphicsScene that items can then be added to.

        """
        # get the current tab index and the widget
        index = self.pickerUI.characterTabs.currentIndex()
        widget = self.pickerUI.characterTabs.widget(index)

        children = widget.children()
        for child in children:
            if type(child) == QtWidgets.QTabWidget:
                tab = child
                canvasIndex = tab.currentIndex()
                canvasWidget = tab.widget(canvasIndex)

                canvasChildren = canvasWidget.children()
                for canvasChild in canvasChildren:
                    if type(canvasChild) == QtWidgets.QGraphicsView:
                        view = canvasChild
                        scene = view.scene()

        return scene

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def getExistingModules(self):
        """
        Find all existing modules on the canvas and return those modules as a list.
        :return: List of modules whose picker exists on the canvas.

        """

        # get the current tab index and the widget
        index = self.pickerUI.characterTabs.currentIndex()
        widget = self.pickerUI.characterTabs.widget(index)
        characterNode = widget.property("charNode")
        characterNodeModules = cmds.listConnections(characterNode + ".rigModules")

        namespace = None
        if cmds.objExists(characterNode + ".namespace"):
            namespace = cmds.getAttr(characterNode + ".namespace") + ":"

        returnData = []

        # get the children of the current tab widget
        children = widget.children()
        for child in children:

            # if we find a tab widget, search for the gfxScene
            if type(child) == QtWidgets.QTabWidget:
                tab = child
                selectedTab = tab.currentIndex()

                for i in range(tab.count()):
                    tab.setCurrentIndex(i)
                    canvasIndex = tab.currentIndex()
                    canvasWidget = tab.widget(canvasIndex)
                    canvasChildren = canvasWidget.children()

                    for canvasChild in canvasChildren:
                        if type(canvasChild) == QtWidgets.QGraphicsView:
                            view = canvasChild
                            scene = view.scene()

                            # get all items in the gfxScene
                            itemsInScene = scene.items()

                            for item in itemsInScene:
                                # if we find our top level picker item (the borderItem), get it's data
                                if type(item) == interfaceUtils.pickerBorderItem or item.type() == 3:
                                    module = item.data(QtCore.Qt.UserRole)

                                    if namespace is None:
                                        if module not in returnData:
                                            returnData.append(module)
                                    else:
                                        if (namespace + module) not in returnData:
                                            returnData.append(namespace + module)

                tab.setCurrentIndex(selectedTab)

        return returnData

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def closeWin(self, event):
        """
        deletes the UI.

        """

        cmds.deleteUI("pyART_AddToCanvasWIN", wnd=True)