Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Last active June 7, 2021 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRoy/78952c37e8494457f85502667a55483a to your computer and use it in GitHub Desktop.
Save BigRoy/78952c37e8494457f85502667a55483a to your computer and use it in GitHub Desktop.
Example of how to cache Maya outliner (and other maya resources) icons to use in a Qt widget or view.
import os
from maya import cmds
from PySide2 import QtWidgets, QtGui, QtCore
class IconCache(object):
"""Simple Icon cache for Maya resources"""
def __init__(self):
self._paths_cache = {}
self._icons_cache = {}
def __get_key(self, fname):
return os.path.splitext(fname)[0]
def refresh(self):
self._paths_cache.clear()
self._icons_cache.clear()
# Add internal Maya resources
for resource in cmds.resourceManager(nameFilter="*"):
key = self.__get_key(resource)
if key in self._paths_cache:
continue
self._paths_cache[key] = ":" + key
# Add icons on XBMLANGPATH
for root in os.environ.get("XBMLANGPATH", "").split(os.pathsep):
if not os.path.exists(root):
continue
_, _, filenames = next(os.walk(root))
for fname in filenames:
key = self.__get_key(fname)
if key in self._paths_cache:
continue
self._paths_cache[key] = os.path.join(root, fname)
def get_path(self, name):
return self._paths_cache.get(name, None)
def get_icon(self, name):
icon = self._icons_cache.get(name, None)
if not icon:
path = self.get_path(name)
if path:
icon = QtGui.QIcon(path)
self._icons_cache[name] = icon
return icon
def get_outliner_icon(self, node_type):
"""Helper function to get the outliner icon for a node type.
Outliner icons are named "out_{nodetype}" so we search for those
resources instead. When not found, fall back to "out_default".
"""
icon = self.get_icon("out_{}".format(node_type))
if icon:
return icon
else:
# Default icon
return self.get_icon("out_default")
# Example usage
cache = IconCache()
cache.refresh()
widget = QtWidgets.QListWidget()
widget.setIconSize(QtCore.QSize(20, 20)) # Maya outliner default sizes
for node_type in sorted(cmds.allNodeTypes()):
icon = cache.get_outliner_icon(node_type)
item = QtWidgets.QListWidgetItem(icon, node_type)
widget.addItem(item)
widget.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment