コード例 #1
0
ファイル: pluginMaya.py プロジェクト: Leopardob/omtk
def _addAttr(_fnDependNode, _sName, _pValue):
    sType = core.getDataType(_pValue)

    # Skip empty list
    bIsMulti = sType == core.TYPE_LIST
    if bIsMulti and len(_pValue) == 0:
        return

    plug = None
    # Get attribute arguments
    try: # TODO: Is a try/catch really the best way to know if the plug exists?
        plug = _fnDependNode.findPlug(_sName)
    except:
        pass

    if plug is None:
        fnAtt = _createAttribute(_sName, _pValue)
        if fnAtt is None: return # In case of invalid value like missing pymel PyNode & Attributes
        fnAtt.setNiceNameOverride(_sName)
        moAtt = fnAtt.object()
        if moAtt is not None:
            _fnDependNode.addAttribute(moAtt)
            plug = OpenMaya.MPlug(_fnDependNode.object(), moAtt)

    if plug is not None:
        _setAttr(plug, _pValue)
コード例 #2
0
def _addAttr(_fnDependNode, _sName, _pValue):
    sType = core.getDataType(_pValue)

    # Skip empty list
    bIsMulti = sType == core.TYPE_LIST
    if bIsMulti and len(_pValue) == 0:
        return

    plug = None
    # Get attribute arguments
    try:  # TODO: Is a try/catch really the best way to know if the plug exists?
        plug = _fnDependNode.findPlug(_sName)
    except:
        pass

    if plug is None:
        fnAtt = _createAttribute(_sName, _pValue)
        if fnAtt is None:
            return  # In case of invalid value like missing pymel PyNode & Attributes
        fnAtt.setNiceNameOverride(_sName)
        moAtt = fnAtt.object()
        if moAtt is not None:
            _fnDependNode.addAttribute(moAtt)
            plug = OpenMaya.MPlug(_fnDependNode.object(), moAtt)

    if plug is not None:
        _setAttr(plug, _pValue)
コード例 #3
0
ファイル: pluginMaya.py プロジェクト: Leopardob/omtk
def _setAttr(_plug, _val):
    sType = core.getDataType(_val)
    if sType == core.TYPE_LIST:
        iNumElements = len(_val)

        _plug.setNumElements(iNumElements) # TODO: MAKE IT WORK # TODO: NECESSARY???

        for i in range(iNumElements):
            _setAttr(_plug.elementByLogicalIndex(i), _val[i])

    elif sType == core.TYPE_BASIC:
        # Basic types
        if isinstance(_val, bool):
            _plug.setBool(_val)
        elif isinstance(_val, int):
            _plug.setInt(_val)
        elif isinstance(_val, float):
            _plug.setFloat(_val)
        elif isinstance(_val, basestring):
            _plug.setString(_val)
        elif isinstance(_val, pymel.datatypes.Matrix):
            fn = OpenMaya.MFnMatrixData()
            mo = fn.create(_val.apicls(_val))
            _plug.setMObject(mo)

    elif sType == core.TYPE_COMPLEX:
        network = export_network(_val)
        plugMessage = network.__apimfn__().findPlug('message')

        # Use a dag modifier to connect the attribute. TODO: Is this really the best way?
        dagM = OpenMaya.MDagModifier()
        dagM.connect(plugMessage, _plug)
        dagM.doIt()

    elif sType == core.TYPE_DAGNODE:
        plug = None
        if isinstance(_val, pymel.Attribute): # pymel.Attribute
            # Hack: Don't crash with non-existent pymel.Attribute
            if not _val.exists():
                log.warning("Can't setAttr, Attribute {0} don't exist".format(_val))
                return
            plug = _val.__apimfn__()
        elif hasattr(_val, 'exists'): # pymel.PyNode
            # Hack: Don't crash with non-existent pymel.Attribute
            if not pymel.objExists(_val.__melobject__()):
                log.warning("Can't setAttr, PyNode {0} don't exist".format(_val))
                return
            plug = _val.__apimfn__().findPlug('message')

        if plug is not None:
            dagM = OpenMaya.MDagModifier()
            #if pymel.attributeQuery(pymel.Attribute(_val), writable=True):
            dagM.connect(plug, _plug)
            '''
            else:
                dagM.connect(_plug, plug)
            '''
            dagM.connect(plug, _plug)
            dagM.doIt()
        else:
            log.error('Unknow type for: {0} ({1})'.format(_val, type(_val)))

    else:
        log.error('Unknow value "{0}" of type {1}'.format(_val, sType))
コード例 #4
0
def _setAttr(_plug, _val):
    sType = core.getDataType(_val)
    if sType == core.TYPE_LIST:
        iNumElements = len(_val)

        _plug.setNumElements(
            iNumElements)  # TODO: MAKE IT WORK # TODO: NECESSARY???

        for i in range(iNumElements):
            _setAttr(_plug.elementByLogicalIndex(i), _val[i])

    elif sType == core.TYPE_BASIC:
        # Basic types
        if isinstance(_val, bool):
            _plug.setBool(_val)
        elif isinstance(_val, int):
            _plug.setInt(_val)
        elif isinstance(_val, float):
            _plug.setFloat(_val)
        elif isinstance(_val, basestring):
            _plug.setString(_val)
        elif isinstance(_val, pymel.datatypes.Matrix):
            fn = OpenMaya.MFnMatrixData()
            mo = fn.create(_val.apicls(_val))
            _plug.setMObject(mo)
            #pymel.Attribute(_plug).set(_val)

    elif sType == core.TYPE_COMPLEX:
        network = export_network(_val)
        plugMessage = network.__apimfn__().findPlug('message')

        # Use a dag modifier to connect the attribute. TODO: Is this really the best way?
        dagM = OpenMaya.MDagModifier()
        dagM.connect(plugMessage, _plug)
        dagM.doIt()

    elif sType == core.TYPE_DAGNODE:
        plug = None
        if isinstance(_val, pymel.Attribute):  # pymel.Attribute
            # Hack: Don't crash with non-existent pymel.Attribute
            if not _val.exists():
                log.warning(
                    "Can't setAttr, Attribute {0} don't exist".format(_val))
                return
            plug = _val.__apimfn__()
        elif hasattr(_val, 'exists'):  # pymel.PyNode
            # Hack: Don't crash with non-existent pymel.Attribute
            if not pymel.objExists(_val.__melobject__()):
                log.warning(
                    "Can't setAttr, PyNode {0} don't exist".format(_val))
                return
            plug = _val.__apimfn__().findPlug('message')

        if plug is not None:
            dagM = OpenMaya.MDagModifier()
            #if pymel.attributeQuery(pymel.Attribute(_val), writable=True):
            dagM.connect(plug, _plug)
            '''
            else:
                dagM.connect(_plug, plug)
            '''
            dagM.connect(plug, _plug)
            dagM.doIt()
        else:
            raise Exception("Unknow TYPE_DAGNODE {0}".format(_val))

    else:
        print _val, sType
        raise NotImplementedError