Skip to content

Instantly share code, notes, and snippets.

@redglasses67
Last active December 23, 2022 19:21
Show Gist options
  • Save redglasses67/0e4bda3e01533a10f8408fdaddce17f8 to your computer and use it in GitHub Desktop.
Save redglasses67/0e4bda3e01533a10f8408fdaddce17f8 to your computer and use it in GitHub Desktop.
MFnNumericAttribute での addAttribute
import maya.api.OpenMaya as om2
selList = om2.MGlobal.getActiveSelectionList()
mObject = selList.getDependNode(0)
fnDepNode = om2.MFnDependencyNode(mObject)
floatAttr = om2.MFnNumericAttribute()
floatAttrObj = floatAttr.create("floatLongName", "floatShortName", om2.MFnNumericData.kFloat, 1.0)
# create後じゃないと設定できない
floatAttr.writable = True
floatAttr.keyable = True
fnDepNode.addAttribute(floatAttrObj)
float3Attr = om2.MFnNumericAttribute()
#float3AttrObj = float3Attr.create("float3LongName", "float3ShortName", om2.MFnNumericData.k3Float, (2.0, 3.0, 4.0)) # こんな感じにはデフォルト値を設定できない
float3AttrObj = float3Attr.create("float3LongName", "float3ShortName", om2.MFnNumericData.k3Float)
# create後じゃないと設定できない
float3Attr.writable = True
float3Attr.keyable = True
float3Attr.default = (2.0, 3.0, 4.0) # Tuple で指定
fnDepNode.addAttribute(float3AttrObj)
int2Attr = om2.MFnNumericAttribute()
#int2AttrObj = int2Attr.create("int2LongName", "int2ShortName", om2.MFnNumericData.k2Int, [5, 6]) # こんな感じにはデフォルト値を設定できない
int2AttrObj = int2Attr.create("int2LongName", "int2ShortName", om2.MFnNumericData.k2Int)
# create後じゃないと設定できない
int2Attr.writable = True
int2Attr.keyable = True
int2Attr.default = (5, 6) # Tuple で指定
fnDepNode.addAttribute(int2AttrObj)
double4Attr = om2.MFnNumericAttribute()
#double4AttrObj = double4Attr.create("double4LongName", "double4ShortName", om2.MFnNumericData.k2Int, (7.0, 8.0, 9.0, 10.0)) # こんな感じにはデフォルト値を設定できない
double4AttrObj = double4Attr.create("double4LongName", "double4ShortName", om2.MFnNumericData.k4Double)
# create後じゃないと設定できない
double4Attr.writable = True
double4Attr.keyable = True
double4Attr.default = (7.0, 8.0, 9.0, 10.0) # Tuple で指定
fnDepNode.addAttribute(double4AttrObj)
boolAttr = om2.MFnNumericAttribute()
boolAttrObj = boolAttr.create("boolLongName", "boolShortName", om2.MFnNumericData.kBoolean, False)
# create後じゃないと設定できない
boolAttr.writable = True
boolAttr.keyable = True
fnDepNode.addAttribute(boolAttrObj)
colorAttr = om2.MFnNumericAttribute()
colorAttrObj = colorAttr.createColor("colorLongName", "colorShortName")
# create後じゃないと設定できない
colorAttr.writable = True
colorAttr.keyable = True
colorAttr.default = (0.25, 0.5, 0.75)
fnDepNode.addAttribute(colorAttrObj)
pointAttr = om2.MFnNumericAttribute()
pointAttrObj = pointAttr.createPoint("pointLongName", "pointShortName")
# create後じゃないと設定できない
pointAttr.writable = True
pointAttr.keyable = True
pointAttr.default = (20.0, 30.0, 40.0)
fnDepNode.addAttribute(pointAttrObj)
# Adress Attribute というのが何に使うのか分かってないですが…
addressAttr = om2.MFnNumericAttribute()
addressAttrObj = addressAttr.createAddr("addressLongName", "addressShortName")
# create後じゃないと設定できない
addressAttr.writable = True
addressAttr.keyable = True
fnDepNode.addAttribute(addressAttrObj)
# MDGModifier を使う方法もあります
# dgModifier = om2.MDGModifier()
# floatAttr2 = om2.MFnNumericAttribute()
# floatAttrObj2 = floatAttr.create("floatLongName 2", "floatShortName 2", om2.MFnNumericData.kFloat, 1.0)
# # create後じゃないと設定できない
# floatAttr2.writable = True
# floatAttr2.keyable = True
# dgModifier.addAttribute(fnDepNode.object(), floatAttrObj2)
# dgModifier.doIt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment