Staring in Maya 2018, you can use the concept of current evaluation context as a more efficient way to code context evaluation.
In previous versions of Maya, if you want to evaluate something at a context other than the normal one, you pass in the context to the method. For example:
MPxTransform::getTranslation( MSpace::Space, const MDGContext&, MStatus* );
Doing this often creates long chains of methods that pass the context in for no other reason than to pass it on to the next method. With the concept of current evaluation context, along with new utilities, no longer makes this necessary. You can now :
You can continue using existing methods that have a context parameter, but a context-free variation is recommended. For example:
return myTransform.getTranslation(MSpace::kTransform, myNewContext, &status);
is now functionally equivalent to:
{ MDGContextGuard ctxGuard( myNewContext ); return myTransform.getTranslation( MSpace::kTransform, &status ); }
In most cases, myNewContext is unnecessary as most code should evaluate in the current context. Exceptions include things like time-based caching and ghosting, where you would deliberately want to evaluate at an alternate context.
If you have a long sequence of calls in a different context to make, then you can use a scoped change of the current context. For example:
{ MDGContextGuard tempContext( myDifferentContext ); firstPlug.getValue( value1 ); secondPlug.getValue( value2 ); thirdPlug.getValue( value3 ); … }
Avoid passing the context around through different methods. Always use the current context to keep your code safe for multi-threaded evaluation.