Working with MDGContext causes maya to evaluate constantly

195 views
Skip to first unread message

fruity

unread,
Oct 23, 2016, 1:48:16 PM10/23/16
to Python Programming for Autodesk Maya
Hi there, 

I'm working on an MPxLocatorNode, and i have to get the value of a plug at a given time. I can do it, using an MContext, and it works perfectly :
MTime t( 10. );
MDGContext ctx( t );
plug.getValue( oInput, ctx );

However, when i'm doing it in a loop (i.e. with a dynamic time), Maya will run indefinitely, even after the draw() has been done : 
for (double i=0.; i < 2.; i += 1.)
{
MTime t( i );
MDGContext ctx( t );
plug.getValue( oInput, ctx );
}
// this will never stop being evaluated.

I'm assuming it's because even though i'm using a MContext, a redraw of my locator is triggered, somehow. Then, because this redraw is triggered, my draw method gets run again, causing another redraw, and so on ? 
But then, why it's not happening if i'm not using a loop ? And then, how can i query a plug at a given time, in a loop ? Is there some hack, based on a setDirty or setClean call ? Is it reliable ?
Any help would be much appreciated, as i couldn't find lots of information about it online =[
Many thanks !

fruity

unread,
Nov 3, 2016, 7:42:51 PM11/3/16
to Python Programming for Autodesk Maya
So still stuck with that annoying problem =[
I found this interesting page (http://theorangeduck.com/page/maya-velocity-node), talking about the same issue, more or less. But firstly, it's not exactly the same, and secondly it uses a callback to fix it, while I'm not sure this is the only solution in my case.
I tried tons of different workarounds, but nothing worked at this point.

I progressed a bit, though ! I know that it's not because of the loop itself, just because i query the mplug value twice.
In other word, to stick to my previous example :

MDGContext ctx( MTime(10.) );
plug.getValue(oInput, ctx );
will work

MDGContext ctx( MTime(11.) );
plug.getValue(oInput, ctx );
will work

but 
MDGContext ctx1( MTime(10.) );
plug.getValue(oInput, ctx1 );
MDGContext ctx2( MTime(11.) );
plug.getValue(oInput, ctx2 );
will put me in an infinite loop.

Any help, suggestion or anything would be more than welcome, i'm really stuck with that !
Many thanks !

Michael Boon

unread,
Feb 16, 2017, 10:10:54 PM2/16/17
to Python Programming for Autodesk Maya
I don't know where you're calling this code from, but I agree with your guess: calling plug.getValue causes your code to be run again.

The reason that would cause an infinite loop when you call it twice and not when you call it once, is that Maya short-circuits if you call it twice with the same frame. So if you call it with MTime(10.), that in turn calls it again with MTime(10.) and Maya is smart enough to stop. But if you call it with MTime(10.) and then with MTime(11.), that in turn goes back to the start, which Maya can't short circuit, and thus you're in an infinite loop.

Seems like you either need to avoid doing this calculation where you're doing it, or set up a system that caches the results.

(Or more likely, you figured this out yourself months ago.)

fruity

unread,
Feb 18, 2017, 8:02:54 PM2/18/17
to Python Programming for Autodesk Maya
hehe thanks, still cool to have new feedbacks anyway =] I actually gave up on this one. So the final solution was kind of a 'hack', as described in the link I posted above. I'm still a bit frustrated that I didn't find a 'maya friendly' solution ^^
Reply all
Reply to author
Forward
0 new messages