Last active
November 20, 2015 10:57
-
-
Save BigRoy/871fda9cc71f71843528 to your computer and use it in GitHub Desktop.
Get the options supported by a families' plug-ins by retrieving their "options" property and assembling them. The "options" property should be provided by the plug-in. For example this is done in Extractors in Pyblish Magenta: https://github.com/pyblish/pyblish-magenta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyblish.api | |
import logging | |
log = logging.getLogger(__name__) | |
def get_family_options(plugins, family): | |
"""Return the user-specifiable options for this family | |
The options name and types are gathered through | |
`options` properties on all the plug-ins that support | |
the family. The options property is assumed to be a | |
dictionary where the key is the name of the setting | |
and the value is the type it should hold as data. | |
Defaults are gathered from a property called | |
`default_options` on the plug-ins. | |
Args: | |
family (str): The name of the family | |
Returns: | |
dict: The options (name, setting) pairs | |
""" | |
options = {} | |
types = {} | |
defaults = {} | |
family_plugins = pyblish.api.plugins_by_family(plugins, family) | |
for plugin in family_plugins: | |
if hasattr(plugin, 'options'): | |
plugin_options = plugin().options | |
types.update(plugin_options) | |
if hasattr(plugin, 'default_options'): | |
plugin_options = plugin().default_options | |
defaults.update(plugin_options) | |
# Safety check types | |
for key, value in types.items(): | |
if not isinstance(key, basestring): | |
log.warning("Setting {0} invalid, " | |
"key is not a string".format(key)) | |
del options[key] | |
continue | |
# Safety check defaults | |
for key, value in defaults.items(): | |
if not isinstance(key, basestring): | |
log.warning("Setting {0} invalid, " | |
"key is not a string".format(key)) | |
del defaults[key] | |
continue | |
for name, datatype in types.items(): | |
settings = {'type': datatype} | |
if name in defaults: | |
settings['default'] = defaults[name] | |
options[name] = settings | |
return options | |
# Example: Add all attributes to the selected instances | |
if __name__ == '__main__': | |
import maya.cmds as mc | |
import pymel.core as pm | |
from collections import defaultdict | |
instances = mc.ls("*_INST", sl=1, type='objectSet') | |
reset = False # set to default value | |
plugins = pyblish.api.discover() | |
# Group the instances per family (optimization for lookup) | |
family_instances = defaultdict(list) | |
for instance in instances: | |
family = mc.getAttr(instance + '.family') | |
family_instances[family].append(instance) | |
for family, instances in family_instances.items(): | |
options = get_family_options(plugins, family) | |
for instance in instances: | |
for name, settings in options.iteritems(): | |
value_type = settings['type'] | |
# If multiple types are allowed we | |
# take the first one and assume | |
# it's the most appropriate one | |
if isinstance(value_type, (tuple, list)): | |
value_type = value_type[0] | |
default = settings.get('default', None) | |
if default is None: | |
value = value_type() | |
else: | |
value = value_type(default) | |
if not mc.attributeQuery(name, node=instance, exists=True) or reset: | |
# We force the attribute with pymel so the attribute | |
# gets created based on the type of it doesn't exist yet. | |
pm.setAttr(instance + '.' + name, value, | |
force=True, | |
channelBox=True, | |
keyable=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment