Skip to content

Instantly share code, notes, and snippets.

@fereria
Created March 13, 2014 13:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fereria/9528629 to your computer and use it in GitHub Desktop.
Save fereria/9528629 to your computer and use it in GitHub Desktop.
create sinwave
# -*- coding: utf-8 -*-
import maya.OpenMaya as om
import maya.OpenMayaMPx as omPx
import math
class sinWave(omPx.MPxNode):
kPluginNodeTypeName = "sinWave"
kPluginNodeId = om.MTypeId(0x87009)
waveLengthAttr = om.MObject()
amplitudeAttr = om.MObject()
outputAttr = om.MObject()
offsetAttr = om.MObject()
timeAttr = om.MObject()
waveLenLongName = "waveLength"
waveLenName = "wl"
ampLongName = "amplitude"
ampName = "amp"
outputLongName = "output"
outputName = "out"
offsetLongName = "offset"
offsetName = "off"
timeLongName = "time"
timeName = "t"
def __init__(self):
omPx.MPxNode.__init__(self)
def compute(self,plug,data):
if plug == sinWave.outputAttr:
#Attributeの値を取得
dHandle = data.inputValue(sinWave.waveLengthAttr)
waveLen = dHandle.asDouble()
dHandle = data.inputValue(sinWave.amplitudeAttr)
amplitude = dHandle.asDouble()
dHandle = data.inputValue(sinWave.offsetAttr)
offset = dHandle.asFloat()
dHandle = data.inputValue(sinWave.timeAttr)
time = dHandle.asTime()
#計算
#計算式は amplitude * sin( time * waveLength)
output = amplitude * math.sin( (time.value() + offset) * waveLen )
outHandle = data.outputValue(sinWave.outputAttr)
outHandle.setDouble(output)
return om.MStatus.kSuccess
else:
return om.MStatus.kUnknownParameter
@classmethod
def nodeCreator(cls):
return omPx.asMPxPtr(cls())
@classmethod
def nodeInitializer(cls):
nAttr = om.MFnNumericAttribute()
cls.waveLengthAttr = nAttr.create(cls.waveLenLongName,
cls.waveLenName,
om.MFnNumericData.kDouble,
1)
nAttr.setKeyable(True)
nAttr.setStorable(True)
nAttr = om.MFnNumericAttribute()
cls.amplitudeAttr = nAttr.create(cls.ampLongName,
cls.ampName,
om.MFnNumericData.kDouble,
1)
nAttr.setKeyable(True)
nAttr.setStorable(True)
nAttr = om.MFnNumericAttribute()
cls.offsetAttr = nAttr.create(cls.offsetLongName,
cls.offsetName,
om.MFnNumericData.kFloat,
0)
nAttr.setKeyable(True)
nAttr.setStorable(True)
nAttr = om.MFnNumericAttribute()
cls.outputAttr = nAttr.create(cls.outputLongName,
cls.outputName,
om.MFnNumericData.kDouble,
0)
nAttr.setWritable(False)
nAttr.setStorable(False)
nAttr = om.MFnUnitAttribute()
cls.timeAttr = nAttr.create(cls.timeLongName,
cls.timeName,
om.MFnUnitAttribute.kTime)
cls.addAttribute(cls.waveLengthAttr)
cls.addAttribute(cls.amplitudeAttr)
cls.addAttribute(cls.offsetAttr)
cls.addAttribute(cls.outputAttr)
cls.addAttribute(cls.timeAttr)
cls.attributeAffects(cls.waveLengthAttr , cls.outputAttr)
cls.attributeAffects(cls.amplitudeAttr , cls.outputAttr)
cls.attributeAffects(cls.offsetAttr , cls.outputAttr)
cls.attributeAffects(cls.timeAttr , cls.outputAttr)
#読み込み
def initializePlugin(obj):
plugin = omPx.MFnPlugin(
obj,
"Remiria",
"1.0","1.0",
)
try:
# ノード名,ID,クリエイタの関数名,ノード初期化関数名
plugin.registerNode(sinWave.kPluginNodeTypeName,
sinWave.kPluginNodeId,
sinWave.nodeCreator,
sinWave.nodeInitializer
)
except:
raise Exception(
"Failed tor register node: %s"%
sinWave.kPluginNodeTypeName
)
def uninitializePlugin(obj):
plugin = omPx.MFnPlugin(obj)
try:
plugin.deregisterNode(obj.kPluginNodeId)
except:
raise Exception(
'Failed to unregister node: %s' %
sinWave.kPluginNodeTypeName
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment