Source code for bgdev.api.attribute

"""Attribute utilities using OpenMaya.

:author: Benoit Gielly (benoit.gielly@gmail.com)
"""
from collections import OrderedDict

from maya.api import OpenMaya

from . import core


[docs]def is_free(plug): """Get the plug's isFreeToChange state.""" return core.as_plug(plug).isFreeToChange() == OpenMaya.MPlug.kFreeToChange
[docs]def is_locked(plug): """Get the plug's locked state.""" return core.as_plug(plug).isLocked
[docs]def get_multi_indices(plug): """Get the plug's multi-indices.""" plug = core.as_plug(plug) indices = plug.getExistingArrayAttributeIndices() if plug.isArray else [] return indices or []
[docs]def get_next_index(plug): """Get next available index in given plug.""" i = -1 for i, j in enumerate(get_multi_indices(plug)): if i != j: return i return i + 1
[docs]def set_alias(plug, alias, remove=False): """Set or remove an alias on given plug.""" plug = core.as_plug(plug) node = core.as_node(plug) name = plug.partialName(useLongNames=True) return node.setAlias(alias, name, plug, add=not remove)
[docs]def get_alias(plug): """Get given plug's alias if any.""" plug = core.as_plug(plug) return core.as_node(plug).plugsAlias(plug)
[docs]def get_alias_list(node): """Get all alias attribute in pair from given node.""" return core.as_node(node).getAliasList()
[docs]def get_node_aliases(node, attr="weight", indices=False): """Get a list of given node's aliases ordered by their index. Args: node (str): The node whose aliases will be queried. attr (str): The name of the attribute to query. indices (bool): If True, returns a dict with indices as values. Returns: list or dict: List of aliases in index order. """ def get_index(name): """Sort function to return the index as an integer.""" return int(name.rsplit("[")[-1].split("]")[0]) pair_data = {y: get_index(x) for y, x in get_alias_list(node) if attr in x} targets = sorted(pair_data, key=pair_data.get) if indices: return OrderedDict((x, pair_data.get(x)) for x in targets) return targets
[docs]def get_attr(plug): """Get the given plug's value. Args: plug (str): Name of the plug (must be "node.attr") Returns: type: Depends on the plug type. """ plug = core.as_plug(plug) type_ = plug.attribute().apiTypeStr if type_ == "kTypedAttribute": return plug.asString() if type_ == "kDoubleAngleAttribute": return plug.asMAngle().asDegrees() return plug.asFloat()