Introduction
The mental ray for Maya 2011 integration is based on mental ray version 3.8. This version allows to use MetaSL shaders for software rendering. Maya 2011 provides the LLVM back-end required for automatic deployment only for the Windows platform, where LLVM is enabled by default. Linux and MacOSX may also use LLVM as well if the back-end plug-in is available. By default, those platforms will use the C/C++ back-end, which depends on external compiler tools and, therefore, is not recommended for use inside applications like Maya.
Prerequisites
The MetaSL compiler component is integral part of mental ray, while the LLVM back-end is implemented in shared library extension, called gen_llvm. This extension is only loaded on demand, like the first time a MetaSL shader is actually used. No other external tools or extensions are required to implement and render the following example, since we are going to write the MetaSL shader code ourselves. Especially, it is not necessary to have mental mill installed, the standalone shader authoring application for interactive shader development. Although, this program comes with a lot of example MetaSL nodes in source code that can help shader developers to get started much more quickly.
Load MetaSL shader in Maya 2011
The following steps need to be performed to use a MetaSL shader in Maya. The example was implemented in Maya on the Windows platform.
The idea is to load MetaSL shader code into mental ray for Maya 2011 using the Shader Manager. This requires to write a .mi file wrapper for the MetaSL shader, which is driving the creation of a Maya node.
Write a MetaSL shader
In order to demonstrate what MetaSL can do right now, we are going to write a useful node that helps to resolve a common problem in Maya: displacement workflow. We like to keep displacement related settings in a separate node, so that we don't need to touch properties of attached texture nodes like Color Effects settings Alpha Gain and Alpha Offset. That would allow us to share that same texture node with different objects or effects, assuming that Color Effects settings have been finalized for that texture and can not be touched.
Here is the example shader. It is supposed to return a scalar value to be attached to the displacement slot of a mental ray material. It expects a color type input for connection of any other shader, and converts this color into the scalar output according to a mode and a scale parameter.
--- displace_adapter.msl ---
Code:
// MetaSL 1.0
// steve, 19 Aug 2010
// Example displacement adapter shader to
// convert color input to scalar displacement values
shader displace_adapter
{
/*
enum Mode {
alpha = 0,
average = 1,
red = 2
};
*/
input:
Color color = Color(0);
Scalar scale = 1.0;
int mode = 0;
output:
Scalar result;
void main()
{
result = 0.0;
switch(mode) {
case 0:
result = color.a;
break;
case 1:
result = (color.r + color.g + color.b) / 3.0;
break;
case 2:
result = color.r;
break;
default:
result = (color.r + color.g + color.b) / 3.0;
break;
}
result *= scale;
}
};
Write a Phenomenon wrapper
The MetaSL shader can not be loaded directly into Maya 2011. But, using the standard .mi parsing capabilities for traditional shaders, it is possible to force to load it indirectly. That requires to write a simple wrapper which exposes the interface of the original shader and connects the interface parameters to the MetaSL shader inputs.
--- msl_displace_adapter.mi ---
Code:
$include "displace_adapter.msl"
declare phenomenon scalar
"displace_adapter_msl" (
color "color",
integer "mode",
scalar "scale" default 1.0
)
shader "shd" "displace_adapter" (
"color" = interface "color",
"mode" = interface "mode",
"scale" = interface "scale"
)
root = "shd"
end declare
It is important to mention, that this step is only needed for Maya to generate a new node type for the MetaSL shader. It is not required for mental ray, which automatically generates a node description, including input and output parameters, just from the MetaSL code. Future Maya versions may be able to create their node types from reading the mental ray node repository directly.
Announce MetaSL directory to Maya
The MetaSL source code and the .mi declaration files may reside in the same directory, or in separate folders. Both directories need to be announced to Maya. In our example, I create a single new directory
C:/Users/steve/Documents/metasl
and put the files
displace_adapter.msl
msl_displace_adapter.mi
there.
The .msl files should be located in the mental ray path MI_RAY_INCPATH,
the .mi files in the Maya path MI_CUSTOM_SHADER_PATH.
To achieve this, create/edit file Maya.env in the sub-directory 2011 (or 2011-x64) of the Maya user preferences location, and add statements similar to the following:
Code:
MI_RAY_INCPATH = C:/Users/steve/Documents/metasl
MI_CUSTOM_SHADER_PATH = C:/Users/steve/Documents/metasl
This will define those environment variables for that specific version of Maya. It is also possible to set those variables in the user environment of the operating system, which would then apply to all Maya versions that may be running on this machine. I guess that is OK for most users. Personally, I prefer to keep settings local to a certain Maya version.
The Maya Script Editor should report if the new .mi declarations were found and parsed. For our example, there may be some warnings printed because Maya needs to assign a node ID automatically from its pool of free node IDs. This means, that the Maya scene containing MetaSL wrapper nodes should be saved in ASCII format rather than BINARY, to store the node information in a form that does not depend on assigned node IDs.
Load the .mi Declaration in Maya
The mental ray for Maya plug-in will be searching for all .mi files in the mentioned directory, and generate Maya nodes out of them. And because the .mi files reference MetaSL code using the $include statement, it forces mental ray to initialize MetaSL and to load the LLVM back-end plug-in. If the user setup was successful, the following lines should be printed in the Maya Output Window as a confirmation:
Code:
mental ray for Maya 2011
mental ray: version 3.8.1.26, Feb 9 2010, revision 110437
mental ray: attempting to load library "C:/Program Files/Autodesk/Maya2011/bin\gen_llvm.dll".
mental ray: library "C:/Program Files/Autodesk/Maya2011/bin\gen_llvm.dll": loaded Code_generator12_plugin plugin "gen_llvm"
Once this is all done it is possible to use MetaSL nodes in Maya for rendering with mental ray like any other node. See next post for details.
Have fun, Steffen.