Skip to content

Commit

Permalink
Updated assetslibrary code to fit new artellapipe framework
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoveda committed Nov 10, 2019
1 parent f772ce4 commit 8bf364d
Showing 1 changed file with 77 additions and 52 deletions.
129 changes: 77 additions & 52 deletions artellapipe/tools/assetslibrary/assetslibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,37 @@
__maintainer__ = "Tomas Poveda"
__email__ = "tpovedatd@gmail.com"

import weakref
import logging
import traceback
from functools import partial

from Qt.QtCore import *
from Qt.QtWidgets import *
from Qt.QtGui import *

import tpDccLib as tp

from tpQtLib.core import qtutils
from tpQtLib.widgets import splitters

from artellapipe.core import tool, defines
import artellapipe
from artellapipe.core import asset, tool
from artellapipe.utils import resource

LOGGER = logging.getLogger()


class ArtellaAssetsLibraryWidget(QWidget, object):
def __init__(self, project, supported_files=None, parent=None):

# # Necessary to support Maya dock
# name = 'ArtellaAssetsLibrary'
# title = 'Artella Assets Viewer'

# _instances = list()

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

self._supported_files = supported_files if supported_files else dict()
self._project = project

# main_window = tp.Dcc.get_main_window()
# if parent is None:
# parent = main_window

super(ArtellaAssetsLibraryWidget, self).__init__(parent=parent)

# if tp.is_maya():
# ArtellaAssetsLibraryWidget._delete_instances()
# self.__class__._instances.append(weakref.proxy(self))

self.ui()
self.resize(150, 800)

self._assets_viewer.update_assets()

# @staticmethod
# def _delete_instances():
# for ins in ArtellaAssetsLibraryWidget._instances:
# try:
# ins.setParent(None)
# ins.deleteLater()
# except Exception:
# pass
#
# ArtellaAssetsLibraryWidget._instances.remove(ins)
# del ins
self._menu = self._create_contextual_menu()

def ui(self):

Expand All @@ -75,19 +51,14 @@ def ui(self):
self.main_layout.setSpacing(0)
self.setLayout(self.main_layout)

# if tp.is_maya():
# self.parent().layout().addLayout(self.main_layout)
# else:
# self.setLayout(self.main_layout)

self._main_widget = QWidget()
main_layout = QVBoxLayout()
main_layout.setContentsMargins(0, 0, 0, 0)
main_layout.setSpacing(0)
self._main_widget.setLayout(main_layout)
self.main_layout.addWidget(self._main_widget)

self._assets_viewer = self._project.ASSETS_VIEWER_CLASS(
self._assets_viewer = artellapipe.AssetsViewer(
project=self._project,
column_count=2,
parent=self
Expand All @@ -102,13 +73,12 @@ def ui(self):

self._categories_menu_layout = QHBoxLayout()
self._categories_menu_layout.setContentsMargins(0, 0, 0, 0)
self._categories_menu_layout.setSpacing(0)
self._categories_menu_layout.setSpacing(5)
self._categories_menu_layout.setAlignment(Qt.AlignTop)
self._top_layout.addLayout(self._categories_menu_layout)

self._categories_btn_grp = QButtonGroup(self)
self._categories_btn_grp.setExclusive(True)
asset_categories = self._get_asset_categories()

main_layout.addWidget(self._assets_viewer)

Expand All @@ -120,49 +90,73 @@ def ui(self):

self._supported_types_btn_grp = QButtonGroup(self)
self._supported_types_btn_grp.setExclusive(True)
supported_types = self._project.asset_library_supported_files if self._project else list()

self._fit_camera_cbx = QCheckBox('Fit Camera')
self.main_layout.addLayout(splitters.SplitterLayout())
checkboxes_layout = QHBoxLayout()
checkboxes_layout.setContentsMargins(5, 5, 5, 5)
checkboxes_layout.setSpacing(2)
self.main_layout.addLayout(checkboxes_layout)
checkboxes_layout.addWidget(self._fit_camera_cbx)
checkboxes_layout.addItem(QSpacerItem(10, 0, QSizePolicy.Expanding, QSizePolicy.Preferred))
self.main_layout.addLayout(splitters.SplitterLayout())

self._assets_viewer.assetAdded.connect(self._on_asset_added)

self.update_asset_categories(asset_categories)
self.update_supported_types(supported_types)
self.update_asset_categories()
self.update_supported_types()

def contextMenuEvent(self, event):
if not self._menu:
return
self._menu.exec_(event.globalPos())

def update_asset_categories(self, asset_categories):
def update_asset_categories(self, asset_categories=None):
"""
Updates current categories with the given ones
:param asset_categories: list(str)
"""

if not asset_categories:
asset_categories = self._get_asset_categories()

for btn in self._categories_btn_grp.buttons():
self._categories_btn_grp.removeButton(btn)

qtutils.clear_layout(self._categories_menu_layout)

all_asset_categories = [defines.ARTELLA_ALL_CATEGORIES_NAME]
all_asset_categories = [asset.ArtellaAssetFileStatus.ALL]
all_asset_categories.extend(asset_categories)
for category in all_asset_categories:
new_btn = QPushButton(category)
new_btn.setMinimumWidth(QFontMetrics(new_btn.font()).width(category) + 10)
new_btn.setIcon(resource.ResourceManager().icon(category.lower()))
new_btn.setCheckable(True)
self._categories_menu_layout.addWidget(new_btn)
self._categories_btn_grp.addButton(new_btn)
if category == defines.ARTELLA_ALL_CATEGORIES_NAME:
if category == asset.ArtellaAssetFileStatus.ALL:
new_btn.setIcon(resource.ResourceManager().icon('home'))
new_btn.setChecked(True)
new_btn.toggled.connect(partial(self._change_category, category))

def update_supported_types(self, supported_types):
def update_supported_types(self):
"""
Updates current supported types
:param supported_types: dict(str, str)
"""

for btn in self._supported_types_btn_grp.buttons():
self._supported_types_btn_grp.removeButton(btn)

qtutils.clear_layout(self._supported_types_layout)

if not self._supported_files:
LOGGER.warning('No Supported Files for AssetsLibrary!')
return

total_buttons = 0
for type_name, type_extension in supported_types.items():
for type_name, type_extension in self._supported_files.items():
new_btn = QPushButton(type_name.title())
new_btn.setIcon(resource.ResourceManager().icon(type_name.lower()))
new_btn.setCheckable(True)
new_btn.extension = type_extension
self._supported_types_layout.addWidget(new_btn)
Expand All @@ -189,6 +183,27 @@ def _setup_asset_signals(self, asset_widget):

asset_widget.clicked.connect(self._on_asset_clicked)

def _create_contextual_menu(self):
"""
Returns custom contextual menu
:return: QMenu
"""

new_menu = QMenu(self)
get_thumbnails_action = QAction(
resource.ResourceManager().icon('picture'), 'Update Thumbnails', new_menu)
get_thumbnails_action.triggered.connect(self._on_update_thumbnails)
new_menu.addAction(get_thumbnails_action)

return new_menu

def _on_update_thumbnails(self):
"""
Internal callback function that is called when Update Thumbnails action is triggered
"""

self._assets_viewer.update_assets_thumbnails(force=True)

def _on_asset_added(self, asset_widget):
"""
Internal callback function that is called when a new asset widget is added to the assets viewer
Expand All @@ -206,7 +221,7 @@ def _get_asset_categories(self):
:return: list(str)
"""

return self._project.asset_types if self._project else list()
return artellapipe.AssetsMgr().config.get('types') or list()

def _on_asset_clicked(self, asset_widget):
"""
Expand All @@ -220,7 +235,16 @@ def _on_asset_clicked(self, asset_widget):
for btn in self._supported_types_btn_grp.buttons():
if btn.isChecked():
try:
asset_widget.asset.reference_file_by_extension(extension=btn.extension)
res = asset_widget.asset.reference_file_by_extension(
extension=btn.extension)
if res:
if self._fit_camera_cbx.isChecked():
try:
tp.Dcc.select_object(res)
tp.Dcc.fit_view(True)
tp.Dcc.clear_selection()
except Exception as exc:
LOGGER.warning('Impossible to fit camera view to referenced objects!')
except Exception as e:
LOGGER.warning('Impossible to reference asset!')
LOGGER.error('{} | {}'.format(e, traceback.format_exc()))
Expand All @@ -238,5 +262,6 @@ def __init__(self, project, config):
def ui(self):
super(ArtellaAssetsLibrary, self).ui()

self._library_widget = self.LIBRARY_WIDGET(project=self._project)
supported_files = self.config.get('supported_files')
self._library_widget = self.LIBRARY_WIDGET(project=self._project, supported_files=supported_files)
self.main_layout.addWidget(self._library_widget)

0 comments on commit 8bf364d

Please sign in to comment.