def set_numeric_attribute_value(plug, value): nData = OpenMaya.MFnNumericData fn = OpenMaya.MFnNumericAttribute(plug.attribute()) t = fn.unitType() if t == nData.kBoolean: MayaUndoHelper.dg_modifier.newPlugValueBool(plug, value) return if t in [ nData.kShort, nData.kInt, nData.kLong, nData.kByte, nData.kChar ]: MayaUndoHelper.dg_modifier.newPlugValueInt(plug, value) return if t in [nData.kDouble, nData.kFloat]: MayaUndoHelper.dg_modifier.newPlugValueDouble(plug, value) return if t in [nData.k2Short, nData.k2Int, nData.k2Long]: for i in range(2): MayaUndoHelper.dg_modifier.newPlugValueInt( plug.child(i), value[i]) return if t in [nData.k2Double, nData.k2Float]: for i in range(2): MayaUndoHelper.dg_modifier.newPlugValueDouble( plug.child(i), value[i]) return if t in [nData.k3Short, nData.k3Int, nData.k3Long]: for i in range(3): MayaUndoHelper.dg_modifier.newPlugValueInt( plug.child(i), value[i]) return if t in [nData.k3Double, nData.k3Float]: for i in range(3): MayaUndoHelper.dg_modifier.newPlugValueDouble( plug.child(i), value[i]) return if t == nData.k4Double: for i in range(4): MayaUndoHelper.dg_modifier.newPlugValueDouble( plug.child(i), value[i]) return raise FacadeRuntimeError("Could not set numeric type for plug %s" % plug.partialName(True))
def get_channel_value(node_obj, channel_obj): c_mobj = channel_obj.attribute() if c_mobj.hasFn(OpenMaya.MFn.kUnitAttribute): return MayaFacadeHelper.get_unit_attribute_value(channel_obj) if c_mobj.hasFn(OpenMaya.MFn.kNumericAttribute): return MayaFacadeHelper.get_numeric_attribute_value(channel_obj) if c_mobj.hasFn(OpenMaya.MFn.kEnumAttribute): return channel_obj.asShort() if (c_mobj.hasFn(OpenMaya.MFn.kMatrixAttribute) or c_mobj.hasFn(OpenMaya.MFn.kFloatMatrixAttribute)): if not MayaFacadeHelper.can_get_mobject(channel_obj): return mobj = channel_obj.asMObject() mmatrix = OpenMaya.MFnMatrixData(mobj).matrix() return [ mmatrix[0][0], mmatrix[0][1], mmatrix[0][2], mmatrix[0][3], mmatrix[1][0], mmatrix[1][1], mmatrix[1][2], mmatrix[1][3], mmatrix[2][0], mmatrix[2][1], mmatrix[2][2], mmatrix[2][3], mmatrix[3][0], mmatrix[3][1], mmatrix[3][2], mmatrix[3][3] ] if c_mobj.hasFn(OpenMaya.MFn.kFloatAngleAttribute): return math.degrees(channel_obj.asFloat()) if c_mobj.hasFn(OpenMaya.MFn.kDoubleAngleAttribute): return math.degrees(channel_obj.asDouble()) if c_mobj.hasFn(OpenMaya.MFn.kTypedAttribute): fn = OpenMaya.MFnTypedAttribute(c_mobj) if fn.attrType() == OpenMaya.MFnData.kString: try: return channel_obj.asString() except: return raise FacadeRuntimeError("Could not find value for plug %s" % channel_obj.partialName(True))
def get_numeric_attribute_value(plug): nData = OpenMaya.MFnNumericData fn = OpenMaya.MFnNumericAttribute(plug.attribute()) t = fn.unitType() if t == nData.kBoolean: return plug.asBool() elif t == nData.kShort: return plug.asShort() elif t == nData.k2Short: return [plug.child(0).asShort(), plug.child(1).asShort()] elif t == nData.k3Short: return [ plug.child(0).asShort(), plug.child(1).asShort(), plug.child(2).asShort() ] elif t in [nData.kInt, nData.kLong]: return plug.asInt() elif t in [nData.k2Int, nData.k2Long]: return [plug.child(0).asInt(), plug.child(1).asInt()] elif t in [nData.k3Int, nData.k3Long]: return [ plug.child(0).asInt(), plug.child(1).asInt(), plug.child(2).asInt() ] elif t == nData.kDouble: return plug.asDouble() elif t == nData.k2Double: return [plug.child(0).asDouble(), plug.child(1).asDouble()] elif t == nData.k3Double: return [ plug.child(0).asDouble(), plug.child(1).asDouble(), plug.child(2).asDouble() ] elif t == nData.k4Double: return [ plug.child(0).asDouble(), plug.child(1).asDouble(), plug.child(2).asDouble(), plug.child(3).asDouble() ] elif t == nData.kFloat: return plug.asFloat() elif t == nData.k2Float: return [plug.child(0).asFloat(), plug.child(1).asFloat()] elif t == nData.k3Float: return [ plug.child(0).asFloat(), plug.child(1).asFloat(), plug.child(2).asFloat() ] raise FacadeRuntimeError("Could not find numeric type for plug %s" % plug.partialName(True))
def set_channel_value(node_obj, channel_obj, value): if channel_obj.isLocked(): warnings.warn( "Cannot set attribute %s. It is locked." % channel_obj.partialName(True), FacadeWarning) return connections = OpenMaya.MPlugArray() channel_obj.connectedTo(connections, True, False) if bool(connections.length()): warnings.warn( "Cannot set attribute %s. It is connected." % channel_obj.partialName(True), FacadeWarning) return if channel_obj.isChild(): parent_plug = channel_obj.parent() connections = OpenMaya.MPlugArray() parent_plug.connectedTo(connections, True, False) if bool(connections.length()): warnings.warn( "Cannot set attribute %s. It is connected." % parent_plug.partialName(True), FacadeWarning) return if channel_obj.isCompound(): warnings.warn( "Cannot set attribute %s. It is a compound." % channel_obj.partialName(True), FacadeWarning) return c_mobj = channel_obj.attribute() if c_mobj.hasFn(OpenMaya.MFn.kUnitAttribute): MayaFacadeHelper.set_unit_attribute_value(channel_obj, value) MayaUndoHelper.dg_modifier.doIt() return if c_mobj.hasFn(OpenMaya.MFn.kNumericAttribute): MayaFacadeHelper.set_numeric_attribute_value(channel_obj, value) MayaUndoHelper.dg_modifier.doIt() return if c_mobj.hasFn(OpenMaya.MFn.kEnumAttribute): MayaUndoHelper.dg_modifier.newPlugValueShort(channel_obj, value) MayaUndoHelper.dg_modifier.doIt() return if (c_mobj.hasFn(OpenMaya.MFn.kMatrixAttribute) or c_mobj.hasFn(OpenMaya.MFn.kFloatMatrixAttribute)): matrix = OpenMaya.MMatrix(value) MayaUndoHelper.dg_modifier.newPlugValue( channel_obj, OpenMaya.MFnMatrixData().create(matrix)) MayaUndoHelper.dg_modifier.doIt() return if (c_mobj.hasFn(OpenMaya.MFn.kFloatAngleAttribute) or c_mobj.hasFn(OpenMaya.MFn.kDoubleAngleAttribute)): MayaUndoHelper.dg_modifier.newPlugValueDouble(channel_obj, value) MayaUndoHelper.dg_modifier.doIt() return if c_mobj.hasFn(OpenMaya.MFn.kTypedAttribute): fn = OpenMaya.MFnTypedAttribute(c_mobj) if fn.attrType() == OpenMaya.MFnData.kString: MayaUndoHelper.dg_modifier.newPlugValueString( channel_obj, value) MayaUndoHelper.dg_modifier.doIt() return raise FacadeRuntimeError("Could not set value on plug %s" % channel_obj.partialName(True))