Share

Handling Deprecated API Calls

When you use a deprecated API call, a warning such as this one will be generated by your compiler:

warning C4996: 'MPxTransform::validateAndSetValue': Override validateAndSetValue(const MPlug&, const MDataHandle&) instead.  If needed, useMDGContext::current() to get the context.

If you enabled treat warning as error on your compiler and experienced a related build error during your migration, you can disable the deprecation warnings.

To silence the deprecation warnings, you can define the _OPENMAYA_DEPRECATION_DISABLE_WARNING preprocessor macro, or use one of the following compiler switches:

MSVC /Wd"4996"
ICC -diag-disable 1786
Clang -Wno-error=deprecated-declarations
GCC -Wno-error=deprecated-declarations

To treat deprecation as an error, your can define the _OPENMAYA_DEPRECATION_ERROR_ALL preprocessor macro, or use one of the following compiler switches:

MSVC /We"4996" (/D_CRT_NONSTDC_NO_DEPRECATE)
ICC -diag-error 1786
Clang -Werror=deprecated-declarations
GCC Werror=deprecated-declarations

Backwards compatibility for deprecated methods

Deprecated methods can be removed from the API as soon as within three major releases. As such you should remove deprecated methods from your plug-ins as soon as possible.

However, if the scope of the change is too great to be done all at once, or if you are using third-party software, you may have deprecated API calls remaining in your code.

You can override deprecated API calls as long as you do not override both the deprecated and the new API in the same hierarchy or in the same class.

The following examples show how to override a new and deprecated API. The examples use the getMatrix() method. MMatrix::getMatrix(const MDGContext&) was deprecated in Maya 2018 and replaced with MMatrix::getMatrix().

To override the new API:

class UserTransform : public MPxTransform {
    MMatrix getMatrix() override;
};

To override the deprecated API:

class UserTransform: public MPxTransform {
    MMatrix getMatrix(const MDGContext&) override; // deprecated
};

Common mistakes to avoid

When using both a new and deprecated API, do not override both the new and deprecated APIs within the same class or inheritance hierarchy, and do not call the deprecated API from the new API's override or vice versa.

The following examples illustrate these common mistakes. The examples use the getMatrix() method. MMatrix::getMatrix(const MDGContext&) was deprecated in Maya 2018 and replaced with MMatrix::getMatrix().

Do not override the new and deprecated APIs within the same class. For example DO NOT do this:

class UserTransform: public MPxTransform { 
    MMatrix getMatrix() override; // new 
    MMatrixgetMatrix(const MDGContext&) override; // deprecated 
}; // Error, override both API in one class 

Do not override both the new and deprecated APIs in the same inheritance hierarchy.

For example, DO NOT do this:

class UserTransformBase: public MPxTransform { 
   MMatrixgetMatrix(const MDGContext&) override; // deprecated 
}; // Ok 

class UserTransformDerived: public UserTransformBase {  
    MMatrix getMatrix() override; // new 
}; // Error, override both API in one inheritance hierarchy 

Do not call the deprecated API from new API’s override or call the new API from the deprecated API override:

For example, DO NOT do this:

class UserTransform: public MPxTransform { 
    MMatrix getMatrix() override { 
        return MPxTransform::getMatrix(MDGContext::current()); // Error, calling deprecated API from new API overridden 
    } 
}; 

Was this information helpful?