oneFoolsArmy
SOuP is still something I wish I had more excuses to use in daily work, but until such a time it has inspired me to start educating myself in the Maya API. I'm trying to write a custom DG node as a way to teach myself a little of the python API. 

The idea is a node which can be given a condition list and a message list, along with some needed string arguments. At render time the node should check on each frame for the conditions and dispatch the corresponding message via pythons smtplib module. Basically, I want to set up frame milestones so I can receive emails alerting me that my render has progressed to the given condition frame.

I'm having trouble finding examples of methods for getting the node to compute each frame only at render time. I believe I could simply connect the time node to gain my current frame and thus force the compute as that data updates, but this would also trigger during scrubbing and playblasts unless I'm very much mistaken.

If anyone knows a direction I could start my research in I would much appreciate the guidance. Thanks in advance. 
Reply 0 0
pshipkov
Check MGlobal::MMayaState
Reply 0 0
oneFoolsArmy
Many thanks for the direction.

How is this different from the OpenMayaRender.MRenderUtil.mayaRenderState() method? Is one preferable to the other?
Reply 0 0
pshipkov
They are pretty much the same. MayaRenderState is more descriptive.
Reply 0 0
oneFoolsArmy
Thanks again.

Follow up question: I'm wondering if there are any good tricks for forcing compute() during render? I think I understand the lazy evaluation system, but I'm having trouble getting my node to evaluate without building unneeded expressions to query it during render and force the evaluation.

I have read in the API documentation about the setInternalInContext() method, and this sounds like it could be my solution, but experimenting with this has yet to yield any results. I think I may be missing something in the concept of this method (probably due to a lack of C++ experience).

What it sounds like to me is:
-Set the time attribute of plugin MPxNode to internal and connect to time1.outTime when node is created
-When time1.outTime updates it should trigger the setInternalInContext() method right? (have yet to make this work)
-Or will it just send a dirty flag downstream? and if so, can I instruct the time attribute of my node to constantly request clean data whenever it gets a dirty flag?


 
Reply 0 0
pshipkov
Hard to say. Never tried that approach.

I think the safest way is to make your node MPxLocator and it will be automatically triggered by Maya.
Reply 0 0
oneFoolsArmy
Ah, that sounds like the best approach. I figured there had to be a node type which is simply automatically evaluated, but you might be surprised how unwilling the documentation is to point that bit out, or perhaps I simply missed it. Thanks for the tip, I'll try it out.
Reply 0 0
oneFoolsArmy
I'm sure I must be missing something still. I'm including my code in this response. If someone can see what I'm missing it would be greatly appreciated. Apologies in advance for the rough nature of this code, I'm quite new to all this and trying to pick it up as I go along.

The node loads just fine, and when a getAttr or other such query is being run on the dummyTrigger attribute than the compute() method works perfectly too. My problem is still getting the node to evaluate automatically without needing to instruct my end user to create worthless connections to trick maya.

 

# Assumes a 30FPS scene
import smtplib, sys
import maya.cmds as cmds
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
 
kPluginNodeTypeName = "nsAlertNode"
 
nsAlertNodeId = OpenMaya.MTypeId(0x0011C280)
 
# Node Def
class alertNode(OpenMayaMPx.MPxLocatorNode):
    # Class Vars
    toAddress = OpenMaya.MObject()
    fromAddress = OpenMaya.MObject()
    userName = OpenMaya.MObject()
    password = OpenMaya.MObject()
    time = OpenMaya.MObject()
    alertFrames = OpenMaya.MObject()
    smtp = OpenMaya.MObject()
    sceneName = OpenMaya.MObject()
    # Dummy to trigger compute()
    dummyTrigger = OpenMaya.MObject()
 
    def __init__(self):
        OpenMayaMPx.MPxLocatorNode.__init__(self)
 
    def compute(self,plug,data):
        timeHandle = data.inputValue( alertNode.time )
        tempFrame = timeHandle.asTime()
        currentFrame = int(tempFrame.asUnits(OpenMaya.MTime.kNTSCFrame))
        toHandle = data.inputValue( alertNode.toAddress )
        toAddr = toHandle.asString()
        fromHandle = data.inputValue( alertNode.fromAddress )
        fromAddr = fromHandle.asString()
        userHandle = data.inputValue( alertNode.userName )
        user = userHandle.asString()
        passHandle = data.inputValue( alertNode.password )
        passwordString = passHandle.asString()
        smtpHandle = data.inputValue( alertNode.smtp )
        smtpString = smtpHandle.asString()
        server = smtplib.SMTP(smtpString)
        sceneNameHandle = data.inputValue( alertNode.sceneName )
        sceneName = sceneNameHandle.asString()
        frameString = str(currentFrame)
        alertFramesHandler = data.inputValue( alertNode.alertFrames )
        alertFramesString = alertFramesHandler.asString()
        alertFramesList = [int(s) for s  in alertFramesString.split() if s.isdigit()]
        msg = ('Render of scene ' + sceneName + ' has reached frame ' + frameString)
        checkRenderState = OpenMaya.MGlobal.mayaState()
        if checkRenderState == 1:
            for checkFrame in alertFramesList:
                if currentFrame == checkFrame:
                        try:
                            server.starttls()
                            server.login(user, passwordString)
                            server.sendmail(fromAddr, toAddr, msg)
                            server.quit()
                        except:
                            sys.stderr.write('Failed to send message')
        data.setClean(plug)
 
# Creator
def nodeCreator():
    return  OpenMayaMPx.asMPxPtr( alertNode() )
 
# initializer
def nodeInitializer():
    # input Attrs
    # To Address
    nAttr = OpenMaya.MFnTypedAttribute()
    alertNode.toAddress = nAttr.create('toAddress', 'tAdd', OpenMaya.MFnData.kString)
    nAttr.setStorable(True)
    nAttr.setKeyable(False)
    # From Address
    nAttr = OpenMaya.MFnTypedAttribute()
    alertNode.fromAddress = nAttr.create('fromAddress', 'fAdd', OpenMaya.MFnData.kString)
    nAttr.setStorable(True)
    nAttr.setKeyable(False)
    # User Name
    nAttr = OpenMaya.MFnTypedAttribute()
    alertNode.userName = nAttr.create('userName', 'usr', OpenMaya.MFnData.kString)
    nAttr.setStorable(True)
    nAttr.setKeyable(False)
    # Password
    nAttr = OpenMaya.MFnTypedAttribute()
    alertNode.password = nAttr.create('password', 'pass', OpenMaya.MFnData.kString)
    nAttr.setStorable(True)
    nAttr.setKeyable(False)
    # smtp
    nAttr = OpenMaya.MFnTypedAttribute()
    alertNode.smtp = nAttr.create('smtpData', 'smtp', OpenMaya.MFnData.kString)
    nAttr.setStorable(True)
    nAttr.setKeyable(False)
    # Alert Frames
    nAttr = OpenMaya.MFnTypedAttribute()
    alertNode.alertFrames = nAttr.create('alertFrames', 'alFr', OpenMaya.MFnData.kString)
    nAttr.setStorable(True)
    nAttr.setKeyable(False)
    # Time
    nAttr = OpenMaya.MFnUnitAttribute()
    alertNode.time = nAttr.create('time', 'tm', OpenMaya.MFnUnitAttribute.kTime)
    nAttr.setStorable(False)
    nAttr.setKeyable(False)
    nAttr.setHidden(True)
    # Scene Name
    nAttr = OpenMaya.MFnTypedAttribute()
    alertNode.sceneName = nAttr.create('sceneName', 'sn', OpenMaya.MFnData.kString)
    nAttr.setStorable(True)
    nAttr.setKeyable(False)
    # Dummy Trigger
    nAttr = OpenMaya.MFnUnitAttribute()
    alertNode.dummyTrigger = nAttr.create('dummyTrigger', 'dumT', OpenMaya.MFnUnitAttribute.kTime)
    nAttr.setStorable(False)
    nAttr.setKeyable(False)
    nAttr.setHidden(True)
    nAttr
    # Add Attrs
    try:
        alertNode.addAttribute( alertNode.toAddress )
        alertNode.addAttribute( alertNode.fromAddress )
        alertNode.addAttribute( alertNode.userName )
        alertNode.addAttribute( alertNode.password )
        alertNode.addAttribute( alertNode.time )
        alertNode.addAttribute( alertNode.smtp )
        alertNode.addAttribute( alertNode.sceneName )
        alertNode.addAttribute( alertNode.alertFrames )
        alertNode.addAttribute( alertNode.dummyTrigger )
        # Time affects dummyTrigger for compute
        alertNode.attributeAffects ( alertNode.time, alertNode.dummyTrigger )
    except:
        sys.stderr.write( "Failed to create attributes of %s node\n", kPluginNodeTypeName )
   
        
# Creation and hook up command
 
kPluginCmdName = 'nsAlert'
 
class nsSetupAlertCmd(OpenMayaMPx.MPxCommand):
    def __init__(self):
        OpenMayaMPx.MPxCommand.__init__(self)
 
    def doIt(self,args):
        newNode = cmds.createNode('nsAlertNode')
        cmds.connectAttr('time1.outTime',(newNode + '.time'))
        cmds.setAttr((newNode + '.smtpData'), 'smtp.gmail.com:587', type='string')
        sceneName = cmds.file(query=True,sceneName=True,shortName=True)
        cmds.setAttr((newNode + '.sceneName'), sceneName, type='string')
 
def cmdCreator():
    return OpenMayaMPx.asMPxPtr( nsSetupAlertCmd() )
 
# Initialiaze the script plug-in
def initializePlugin(mobject):
    mplugin = OpenMayaMPx.MFnPlugin(mobject)
    try:
        mplugin.registerNode( kPluginNodeTypeName, nsAlertNodeId, nodeCreator, nodeInitializer )
        mplugin.registerCommand( kPluginCmdName, cmdCreator ) 
    except:
        sys.stderr.write( "Failed to register node: %s\n" % kPluginNodeTypeName )
 
# Uninitialize the script plug-in
def uninitializePlugin(mobject):
    mplugin = OpenMayaMPx.MFnPlugin(mobject)
    try:
        mplugin.deregisterNode( nsAlertNodeId )
        mplugin.deregisterCommand( kpluginCmdName )
    except:
        sys.stderr.write( "Failed to unregister node: %s\n" % kPluginNodeTypeName )
 
 
Reply 0 0
pshipkov
The MPxLocatorNode class has virtual ::draw method that you have to overload.
Then from this method you get a plug to static attribute on the same node that triggers the ::compute method. Let me know if something is not clear here. I can expand more if you need.
Reply 0 0
oneFoolsArmy
I think I understand, I'll be testing that as soon as I get a little spare time. I believe in my case I'll be redefining the draw() method as opposed to overloading since I'm not sure python supports overloading.

Thanks again for the direction.
Reply 0 0
oneFoolsArmy
Works perfectly! Thanks for the help.

I was wondering what I could do about changing how the node looks in the attribute editor now? It displays a lot of attributes inherited from the MPxLocatorNode class that I don't need. Is there an easy way to turn off or hide inherited attributes?

And how do I control the name of the transform node created? Is there a method I can override, or do I have to do so in my custom creation command?


Reply 0 0
Reply