Skip to content

widgets

AssetOutliner

Bases: QWidget

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
 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
class AssetOutliner(QtWidgets.QWidget):
    refreshed = QtCore.Signal()
    selection_changed = QtCore.Signal()

    def __init__(self, parent=None):
        super(AssetOutliner, self).__init__(parent)

        title = QtWidgets.QLabel("Assets", self)
        title.setAlignment(QtCore.Qt.AlignCenter)
        title.setStyleSheet("font-weight: bold; font-size: 12px")

        model = AssetModel()
        view = View(self)
        view.setModel(model)
        view.customContextMenuRequested.connect(self.right_mouse_menu)
        view.setSortingEnabled(False)
        view.setHeaderHidden(True)
        view.setIndentation(10)

        from_all_asset_btn = QtWidgets.QPushButton(
            "Get All Assets", self
        )
        from_selection_btn = QtWidgets.QPushButton(
            "Get Assets From Selection", self
        )

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(title)
        layout.addWidget(from_all_asset_btn)
        layout.addWidget(from_selection_btn)
        layout.addWidget(view)

        # Build connections
        from_selection_btn.clicked.connect(self.get_selected_assets)
        from_all_asset_btn.clicked.connect(self.get_all_assets)

        selection_model = view.selectionModel()
        selection_model.selectionChanged.connect(self.selection_changed)

        self.view = view
        self.model = model

        self.log = logging.getLogger(__name__)

    def clear(self):
        self.model.clear()

        # fix looks remaining visible when no items present after "refresh"
        # todo: figure out why this workaround is needed.
        self.selection_changed.emit()

    def add_items(self, items):
        """Add new items to the outliner"""

        self.model.add_items(items)
        self.refreshed.emit()

    def get_selected_items(self):
        """Get current selected items from view

        Returns:
            list: list of dictionaries
        """

        selection_model = self.view.selectionModel()
        return [row.data(TreeModel.ItemRole)
                for row in selection_model.selectedRows(0)]

    def get_all_assets(self):
        """Add all items from the current scene"""

        with preserve_expanded_rows(self.view):
            with preserve_selection(self.view):
                self.clear()
                nodes = commands.get_all_asset_nodes()
                items = commands.create_items_from_nodes(nodes)
                self.add_items(items)
                return len(items) > 0

    def get_selected_assets(self):
        """Add all selected items from the current scene"""

        with preserve_expanded_rows(self.view):
            with preserve_selection(self.view):
                self.clear()
                nodes = commands.get_selected_nodes()
                items = commands.create_items_from_nodes(nodes)
                self.add_items(items)

    def get_nodes(self, selection=False):
        """Find the nodes in the current scene per folder."""

        items = self.get_selected_items()

        # Collect all nodes by hash (optimization)
        if not selection:
            nodes = cmds.ls(dag=True, long=True)
        else:
            nodes = commands.get_selected_nodes()
        id_nodes = commands.create_folder_id_hash(nodes)

        # Collect the asset item entries per folder
        # and collect the namespaces we'd like to apply
        folder_items = {}
        namespaces_by_folder_path = defaultdict(set)
        for item in items:
            folder_entity = item["folder_entity"]
            folder_id = folder_entity["id"]
            folder_path = folder_entity["path"]
            namespaces_by_folder_path[folder_path].add(item.get("namespace"))

            if folder_path in folder_items:
                continue

            folder_items[folder_path] = item
            folder_items[folder_path]["nodes"] = id_nodes.get(folder_id, [])

        # Filter nodes to namespace (if only namespaces were selected)
        for folder_path in folder_items:
            namespaces = namespaces_by_folder_path[folder_path]

            # When None is present there should be no filtering
            if None in namespaces:
                continue

            # Else only namespaces are selected and *not* the top entry so
            # we should filter to only those namespaces.
            nodes = folder_items[folder_path]["nodes"]
            nodes = [node for node in nodes if
                     commands.get_namespace_from_node(node) in namespaces]
            folder_items[folder_path]["nodes"] = nodes

        return folder_items

    def select_asset_from_items(self):
        """Select nodes from listed asset"""

        items = self.get_nodes(selection=False)
        nodes = []
        for item in items.values():
            nodes.extend(item["nodes"])

        commands.select(nodes)

    def right_mouse_menu(self, pos):
        """Build RMB menu for asset outliner"""

        active = self.view.currentIndex()  # index under mouse
        active = active.sibling(active.row(), 0)  # get first column
        globalpos = self.view.viewport().mapToGlobal(pos)

        menu = QtWidgets.QMenu(self.view)

        # Direct assignment
        apply_action = QtWidgets.QAction(menu, text="Select nodes")
        apply_action.triggered.connect(self.select_asset_from_items)

        if not active.isValid():
            apply_action.setEnabled(False)

        menu.addAction(apply_action)

        menu.exec_(globalpos)

add_items(items)

Add new items to the outliner

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
73
74
75
76
77
def add_items(self, items):
    """Add new items to the outliner"""

    self.model.add_items(items)
    self.refreshed.emit()

get_all_assets()

Add all items from the current scene

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
90
91
92
93
94
95
96
97
98
99
def get_all_assets(self):
    """Add all items from the current scene"""

    with preserve_expanded_rows(self.view):
        with preserve_selection(self.view):
            self.clear()
            nodes = commands.get_all_asset_nodes()
            items = commands.create_items_from_nodes(nodes)
            self.add_items(items)
            return len(items) > 0

get_nodes(selection=False)

Find the nodes in the current scene per folder.

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
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
def get_nodes(self, selection=False):
    """Find the nodes in the current scene per folder."""

    items = self.get_selected_items()

    # Collect all nodes by hash (optimization)
    if not selection:
        nodes = cmds.ls(dag=True, long=True)
    else:
        nodes = commands.get_selected_nodes()
    id_nodes = commands.create_folder_id_hash(nodes)

    # Collect the asset item entries per folder
    # and collect the namespaces we'd like to apply
    folder_items = {}
    namespaces_by_folder_path = defaultdict(set)
    for item in items:
        folder_entity = item["folder_entity"]
        folder_id = folder_entity["id"]
        folder_path = folder_entity["path"]
        namespaces_by_folder_path[folder_path].add(item.get("namespace"))

        if folder_path in folder_items:
            continue

        folder_items[folder_path] = item
        folder_items[folder_path]["nodes"] = id_nodes.get(folder_id, [])

    # Filter nodes to namespace (if only namespaces were selected)
    for folder_path in folder_items:
        namespaces = namespaces_by_folder_path[folder_path]

        # When None is present there should be no filtering
        if None in namespaces:
            continue

        # Else only namespaces are selected and *not* the top entry so
        # we should filter to only those namespaces.
        nodes = folder_items[folder_path]["nodes"]
        nodes = [node for node in nodes if
                 commands.get_namespace_from_node(node) in namespaces]
        folder_items[folder_path]["nodes"] = nodes

    return folder_items

get_selected_assets()

Add all selected items from the current scene

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
101
102
103
104
105
106
107
108
109
def get_selected_assets(self):
    """Add all selected items from the current scene"""

    with preserve_expanded_rows(self.view):
        with preserve_selection(self.view):
            self.clear()
            nodes = commands.get_selected_nodes()
            items = commands.create_items_from_nodes(nodes)
            self.add_items(items)

get_selected_items()

Get current selected items from view

Returns:

Name Type Description
list

list of dictionaries

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
79
80
81
82
83
84
85
86
87
88
def get_selected_items(self):
    """Get current selected items from view

    Returns:
        list: list of dictionaries
    """

    selection_model = self.view.selectionModel()
    return [row.data(TreeModel.ItemRole)
            for row in selection_model.selectedRows(0)]

right_mouse_menu(pos)

Build RMB menu for asset outliner

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def right_mouse_menu(self, pos):
    """Build RMB menu for asset outliner"""

    active = self.view.currentIndex()  # index under mouse
    active = active.sibling(active.row(), 0)  # get first column
    globalpos = self.view.viewport().mapToGlobal(pos)

    menu = QtWidgets.QMenu(self.view)

    # Direct assignment
    apply_action = QtWidgets.QAction(menu, text="Select nodes")
    apply_action.triggered.connect(self.select_asset_from_items)

    if not active.isValid():
        apply_action.setEnabled(False)

    menu.addAction(apply_action)

    menu.exec_(globalpos)

select_asset_from_items()

Select nodes from listed asset

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
156
157
158
159
160
161
162
163
164
def select_asset_from_items(self):
    """Select nodes from listed asset"""

    items = self.get_nodes(selection=False)
    nodes = []
    for item in items.values():
        nodes.extend(item["nodes"])

    commands.select(nodes)

LookOutliner

Bases: QWidget

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
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
class LookOutliner(QtWidgets.QWidget):
    menu_apply_action = QtCore.Signal()

    def __init__(self, parent=None):
        super(LookOutliner, self).__init__(parent)

        # Looks from database
        title = QtWidgets.QLabel("Looks", self)
        title.setAlignment(QtCore.Qt.AlignCenter)
        title.setStyleSheet("font-weight: bold; font-size: 12px")
        title.setAlignment(QtCore.Qt.AlignCenter)

        model = LookModel()

        # Proxy for dynamic sorting
        proxy = QtCore.QSortFilterProxyModel()
        proxy.setSourceModel(model)

        view = View(self)
        view.setModel(proxy)
        view.setMinimumHeight(180)
        view.setToolTip("Use right mouse button menu for direct actions")
        view.customContextMenuRequested.connect(self.right_mouse_menu)
        view.sortByColumn(0, QtCore.Qt.AscendingOrder)

        # look manager layout
        layout = QtWidgets.QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(10)
        layout.addWidget(title)
        layout.addWidget(view)

        self.view = view
        self.model = model

    def clear(self):
        self.model.clear()

    def add_items(self, items):
        self.model.add_items(items)

    def get_selected_items(self):
        """Get current selected items from view

        Returns:
            list: list of dictionaries
        """

        items = [i.data(TreeModel.ItemRole) for i in self.view.get_indices()]
        return [item for item in items if item is not None]

    def right_mouse_menu(self, pos):
        """Build RMB menu for look view"""

        active = self.view.currentIndex()  # index under mouse
        active = active.sibling(active.row(), 0)  # get first column
        globalpos = self.view.viewport().mapToGlobal(pos)

        if not active.isValid():
            return

        menu = QtWidgets.QMenu(self.view)

        # Direct assignment
        apply_action = QtWidgets.QAction(menu, text="Assign looks..")
        apply_action.triggered.connect(self.menu_apply_action)

        menu.addAction(apply_action)

        menu.exec_(globalpos)

get_selected_items()

Get current selected items from view

Returns:

Name Type Description
list

list of dictionaries

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
228
229
230
231
232
233
234
235
236
def get_selected_items(self):
    """Get current selected items from view

    Returns:
        list: list of dictionaries
    """

    items = [i.data(TreeModel.ItemRole) for i in self.view.get_indices()]
    return [item for item in items if item is not None]

right_mouse_menu(pos)

Build RMB menu for look view

Source code in client/ayon_maya/tools/mayalookassigner/widgets.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def right_mouse_menu(self, pos):
    """Build RMB menu for look view"""

    active = self.view.currentIndex()  # index under mouse
    active = active.sibling(active.row(), 0)  # get first column
    globalpos = self.view.viewport().mapToGlobal(pos)

    if not active.isValid():
        return

    menu = QtWidgets.QMenu(self.view)

    # Direct assignment
    apply_action = QtWidgets.QAction(menu, text="Assign looks..")
    apply_action.triggered.connect(self.menu_apply_action)

    menu.addAction(apply_action)

    menu.exec_(globalpos)