Writing Extensions

By
-
Login to Follow
-
Industry
  • Film & VFX
  • Games
  • Design Visualization
Subject
  • Rendering
  • 1.0
  • Shading
Products
  • Maya
  • Arnold
Skill Level
  • Beginner
Duration
30 min

Creating a basic extension

We can create a very simple extension that translates the native Phong Maya shader to a standard Arnold shader, since it is not currently supported by MtoA and has no any translator associated to it.
First we need to create a new class that inherits from the MtoA translator we need. As this is a simple example, we begin inheriting from the most simple CNodeTranslator:


And we override the methods in a very simple way:

creator

Just returns an instance of the dened class.

CreateArnoldNodes

Creates the Arnold node that will be exported. We create an Arnold standard here as we will translate the Maya Phong into an Arnold standard.

Export

We export the attributes that will not change during an IPR session. We will export here the diffuse Phong attribute to the standard Kd attribute here for the sake of experimentation.

Update

We export the attributes that could be updated in an IPR session. We will export here the color Phong attribute to the standard Kd color attribute.


Note that this is only an example and does not correctly export the Phong shader to a similar standard shader.
Lastly, we need the code that will register the translator for the Maya Phong shader:

To compile this, set these enviroment variables: ARNOLD_PATH, MAYA_PATH and MTOA_PATH:

set ARNOLD_PATH="C:\solidangle\releases\Arnold-X.X.X.X-platform"
set MTOA_PATH="C:\solidangle\mtoadeploy\20XX"
set MAYA_PATH="C:\Program Files\Autodesk\Maya20XX"

To compile the extension, do the following depending on your OS:

Windows

Open a Visual Studio command prompt and execute the following commands:

cl /c translator1.cpp /EHsc /MD /DNT_PLUGIN /DREQUIRE_IOSTREAM /I%MAYA_PATH%\include /I%MTOA_PATH%\include /I%ARNOLD_PATH%\include

cl /c extension1.cpp /EHsc /MD /DNT_PLUGIN /DREQUIRE_IOSTREAM /I%MAYA_PATH%\include /I%MTOA_PATH%\include /I%ARNOLD_PATH%\include

link /dll extension1.obj translator1.obj /LIBPATH:%ARNOLD_PATH%\lib /LIBPATH:%MAYA_PATH%\lib /LIBPATH:%MTOA_PATH%\lib ai.lib OpenGl32.lib glu32.lib Foundation.lib OpenMaya.lib OpenMayaRender.lib OpenMayaUI.lib OpenMayaAnim.lib OpenMayaFX.lib mtoa_api.lib


After this, we should copy the generated extension1.dll le into this path:
MTOA_PATH\extensions
Now, if we open Maya and assign any object a Phong shader, it will be rendered in an IPR session similar to a lambert. If we change the color Phong attribute, it will updated in the IPR. But if we change the diffuse Phong attribute, it will not updated until we stop and restart the IPR again. This is because we exported the diffuse Phong attribute in the Export method instead of doing it in theUpdate method.
If we export the scene, we will see that the Phong Maya shader has been exported like this:

...
standard
{
name phong1
Kd 0.8
Kd_color 0.5 0.5 0.5
}
...

From here, you can make the Export and Update method as complex as you need it.


Adding specific Arnold attributes to a node

Now, we may need a specic attribute for the translator of the Phong that is not present in the Maya node. We can create it and add a section in the Attribute Editor of the node for the Arnold attributes.
We will follow with the Phong translator, and we will add a specular weight attribute to it that will be used in the Arnold translator.
First thing, we will need a NodeInitializer method.


Where we will create the specic attribute with the required properties. Here we can define a soft and hard range to limit the attribute value.



Finally, when we register the extension, we need to provide the NodeInitializer method:


One last nice addition is to create an Arnold section in the Attribute Editor that will show this attribute. We can do this by placing a python file with the same name as the compiled extension in the same folder MTOA_PATH\nextensions


This will create a section in the Attribute Editor of the Phong shader like this:

Multiple translators for the same node

It is possible to associate more than one translator to the same Maya node. We will need to do the following steps:

First, when we register the translator, we should provide a name for it. We can rewrite the first extension like this:


And the second like this:
After this, and to have different Attribute Editor templates for each one, we can rewrite our previous template like this:

The resulting Attribute Editor will look like this one:

Translator of a custom node

First, we will create a very basic Maya Plugin that defines a custom node:


And compile it. Assuming windows, open a Visual Studio command prompt and execute the following commands:

cl /c myPlugin.cpp /EHsc /MD /DNT_PLUGIN /DREQUIRE_IOSTREAM /I%MAYA_PATH%\include

link /dll myPlugin.obj /LIBPATH:%MAYA_PATH%\lib OpenGl32.lib glu32.lib Foundation.lib OpenMaya.lib OpenMayaRender.lib OpenMayaUI.lib OpenMayaAnim.lib OpenMayaFX.lib /export:initializePlugin /export:uninitializePlugin /OUT:myPlugin.mll

Load the generated myPlugin.mll in Maya, and you will be ready to create mySimpleNode node instances. Type the following in the Script Editor, in MEL:
createNode mySimpleNode

A translator for this node will be very easy, but this time, we will inherit the translator from aCDagTranslator instead from a CNodeTranslator as it will allow us to use methods specic for DAG nodes, like exporting the transformation matrix.

And we will define the methods to export an Arnold sphere, get the matrix from the node, and export the node attribute to the sphere radius.



And we load the extension with:

A last thing we may want to do is to have that custom Maya node in the extension, so the plugin is not required to be loaded.
First, we simplify our custom Maya node plugin removing the initializePluginand uninitializePlugin methods:


We do not need to modify the translator, but in the initializeExtension method we make these changes:


Now when the extension is loaded with MtoA, our custom node and its translator will be correctly registered.



Posted By
Tags
  • Maya
  • Arnold
  • Rendering
  • 1.0
  • Shading
0 Comments
To post a comment please login or register
*Save $66 per month on Autodesk's Suggested Retail Price (SRP) when purchasing 1 year term 3ds Max or Maya subscription.