Why and When to use MDGContext

Why and When to use MDGContext

jonathan_ouellet
Contributor Contributor
444 Views
1 Reply
Message 1 of 2

Why and When to use MDGContext

jonathan_ouellet
Contributor
Contributor

Hello,

     I've been using MDGContext to evaluate some transforms at a given time thinking that it would be faster than updating the whole scene with "oma.MAnimControl.setCurrentTime(time)".  Unfortunately, it seems that updating the whole scene is faster.  I'm I using the MDGContext wrong ?  When should it be used ?  Should I avoid using custom MDGContext?

 

 

 

 

 

from maya.api import OpenMaya as om

sel = om.MSelectionList()
sel.add(node + '.worldMatrix[0]')
worldMatplug = sel.getPlug(0)
time = oma.MAnimControl.currentTime()
for f in range(100):
    time.value = float(f)
    context = om.MDGContext(time)
    matObj = worldMatplug.asMObject(context)
    wm = om.MFnMatrixData(matObj).matrix()

 

 

 

 

 

Looks like I should use MContextGuard instead, but it isn't available in python.  I suppose it just set the context as current than switch back to the previous context as the guard goes out of scope?

 

Edit:

 

I did some more test and figured out that in most of the times its faster or approximately the same.   It is just slower in a particular scene in which there is animations layers.  Once I bake the layers, using MDGContext is faster.  Any idea why ?

 

Thanks for your help.

 

Jonathan

0 Likes
445 Views
1 Reply
Reply (1)
Message 2 of 2

jocatapo
Contributor
Contributor

Hello,
I'm struggling with another issue from MDGContextbut I think I can answer your question.

 

you are using the wrong time,

time = oma.MAnimControl.currentTime()

That one is doing the same as cmds.currentTime()

Use that instead:

mTime = om2.MTime(float(f), om2.MTime.uiUnit())
mdg_context = om2.MDGContext(mTime)

 

0 Likes