Hurray , Hurray I didn’t express myself correctly , 
what I wanted is not to compute at intervals of time rather, what I wanted 
Really is to defer 
the compute() to virtually execute only when Maya hits the Idle event, that is 
when the user is not actively changing a thing, 
fortunately there’s MEventMessage:: addEventCallback (“idle” , …) . 

Here’ s how made it work : 

//override void MPxNode::postConstructor() to create the callback for this 
instance
void myclass::postConstructor( )
{
    MStatus st ;

    MCallbackId dummy ;
    dummy  = MEventMessage::addEventCallback
    (  
     "idle",
     deferredCompute, 
     this,
     &st
     );

    MCHECKSTATUS(st);

    mCallBacks.append(dummy);

//when the node is deleted , delete this instance ‘s callbacks

    dummy  = MNodeMessage::addNodePreRemovalCallback
    (  
     thisMObject(),
     _removeCallBacks, 
     this,
     &st
     );

    MCHECKSTATUS(st);

    mCallBacks.append(dummy);

}

//now in compute()

MStatus myclass::compute(const MPlug& plug , MDataBlock& datablock )
{  
    if(mSloppy != ACTION )
    {
        mSloppy = BYPASS//compute gets delayed
        return MS::kSuccess;
    }

    //natural compute begins
    if (plug != aOut)
    {
        return MS::kUnknownParameter;
    }

    MGlobal::displayInfo("compute()");//just to debug
    MStatus st;

    auto imesh_h = datablock.inputValue(aOperands, &st);
    MCHECKSTATUS(st);
    
    auto trig_h = datablock.inputValue(aTrigger, &st);
    MCHECKSTATUS(st);

    auto out_h = datablock.outputValue(aOut, &st);
    MCHECKSTATUS(st);
    out_h.set(imesh_h.asMesh());

    st = datablock.setClean(plug);
    MCHECKSTATUS(st);

    //reset on sleep 
    mSloppy = SLEEP;

    return MS::kSuccess;
}

Now Here’s the juiciest part , it’s sneaky ,  vicious for real

void myclass::deferredCompute(void *clientData)
{
    auto that = static_cast<myclass*>(clientData);
   
    if( that->mSloppy == BYPASS )
    {

        that->mSloppy = ACTION; 
        //trigger compute()
        MPlug trig_p  = MPlug( that->thisMObject() , aTrigger );
        bool lastValue;
        trig_p.getValue(lastValue);
        trig_p.setValue(!lastValue);
    }

}

Basically I trigger compute on Idle with a special & simple attribute 
‘aTrigger’ by using MPlug::setValue. There’s more details to it  but I think 
you understand the idea already..

Sent from Mail for Windows 10

From: Justin Israel
Sent: Sunday, March 4, 2018 6:46 PM
To: [email protected]
Subject: Re: [Maya-Python] Re: Evaluation of Node only once ( compute() )


On Mon, Mar 5, 2018, 7:07 AM Marcus Ottosson <[email protected]> wrote:
This is the timer I had in mind.

http://doc.qt.io/qt-5/qtimer.html
It'd be able to delay a callback till later, and run it in the main thread 
without special treatment. Ultimately, it'd mean updating the plug outside of 
your `compute()` function, where your `compute()` is merely calling this timer, 
which is connected to something capable of updating your plug.
At the end of the day, what you're asking is to aggregate multiple calls that 
Maya makes to `compute()` into a single call by an arbitrary amount of time. It 
isn't going to be pretty. The more robust option is probably the boolean 
toggle. If you have a drag context involved, you could automatically set the 
boolean toggle to `False` when entering drag, and reset it to `True` once done. 
That way, you'd probably get the effect you're looking for.

Could it also work to check if the mouse button is down? 
http://doc.qt.io/archives/qt-4.8/qapplication.html#mouseButtons
That alone would probably miss a compute. But maybe it could be combined with a 
QTimer. The timeout could be connected to a method that checks if the mouse is 
still pressed. If it is, trigger another timer to itself for 100ms. Otherwise, 
compute. 




On 4 March 2018 at 14:55, justin hidair <[email protected]> wrote:


Le dimanche 4 mars 2018 00:02:17 UTC, justin hidair a écrit :

Let's say I have a Node which has an input mesh and an output mesh , 
everytime I move the input mesh , while I move it, compute() is called probably 
15 times and the output is updated also 15 times,
That's great and all but what If I don't want continuous update ? 

what if I want to move the input mesh from A to B , and only when it's at B, 
and I stop moving it, output will update ( so 1 call to compute() )
is there a way to do that ? 
is the time between the total change of the input and the call to compute() as 
a name or a concept in the API ?
See this is important , because it allows to reduce overhead significantly , 
increase performance on complex calculations ...Etc.
-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/34ed49b6-2f80-41c3-8a32-e907d7509d40%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAawKVa8E%3DOWntim%3Dro5ddFZBWdZOjh6PCtp7UXfYSjJw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3dsY5szz5VkjaNUkVQvB4sQHyY0pAvVXOiM7VA%2BMKTgw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/5a9d736a.a58adf0a.edfd2.b920%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to