Using OSL Shaders in Arnold for Maya

Software:
Maya 2020 | Arnold 6

Autodesk Maya 2020 & Arnold 6 offer a flexible OSL development and usage workflow.
You can both load or write OSL shaders on the fly, compile, test, and render them,
And also define a shader folder path for shaders to be available as part of your library for all projects.
Steps for using OSL shaders in Maya & Arnold:

Writing an OSL shader or loading it for single use (just the current project):

  1. Create a new aiOslShader node:
    Annotation 2020-05-19 220416
  2. Select the new aiOslShader node and in its attributes either write new OSL code in the code OSL Code section, or press Import to Load an OSL shader file (*.osl):
    Annotation 2020-05-19 220454
  3. When new shader code is imported, it’s automatically compiled:
    Annotation 2020-05-19 220649
  4. I f you’ve written new code, or changed the code it will have to be re-compiled.
    In that case press Compile OSL Code:
    Annotation 2020-05-19 223632
  5. The code may contain errors, in that case you will see a red Compile Failure message:
    Annotation 2020-05-19 224342
    You can read the error message in the Maya output window, or in the Maya Script Editor, Correct the code and press Compile OSL Code again.
    Annotation 2020-05-19 224410
  6. After the OSL code is compiled successfully, the shader’s input parameters can be accessed in the OSL Attributes section below the code:
    Annotation 2020-05-19 224431
  7. Depending on the type of output the OSL shader generates, the aiOSLShader node should to be connected to an input in the object’s shader graph or Shading Group.
    * OSL shaders can be surface shaders, volume shaders, procedural textures, texture processors and more..
    To Apply the OSL shader to an object as a surface shader, disconnect the object’s current surface shader if it has one,
    And then drag and drop the aiOSLShader node from the Hypershade window onto the object.
    In the Connection Editor select outValue on the left side (node outputs) and surfaceShader in the right side (object inputs):
    Annotation 2020-05-19 220810

Note:
When compiling OSL shaders “on the fly” using the above steps, the shader’s input parameters don’t necessarily appear at their intended order that is  defined in the shader code.

 

Installing OSL shaders so they will always be available as custom nodes in the Hypershade library

  1. Create a folder for storing your OSL shaders, and place you OSL shader files (*.osl) in this folder.
    Annotation 2020-05-20 225134
  2. Locate Maya’s Maya.env file.
    This is an ascii text file containing environment variables that Maya loads at startup.
    The Maya.env will usually be located at:
    C:\Users\<your user>\Documents\maya\<maya version>
    Annotation 2020-05-20 225221
  3. Open Maya.env in a text editor and add the following line to it:
    ARNOLD_PLUGIN_PATH=<path to your OSL shaders folder>
    for example:
    ARNOLD_PLUGIN_PATH=D:\3DAssets\OSL_Shaders
    Annotation 2020-05-20 225400
  4. Restart Maya.
    When Maya loads, the MtoA (Maya to Arnold) plugin will automatically compile the shaders that are found in the folder, report about the compilations or found errors in the Maya output window, and create compiled *.oso files for each shader:
    Annotation 2020-05-20 230917
  5. The compiled shaders will now be available as custom nodes in the Hypershade Arnold library with the typical “ai” (Arnold Interface) prefix added to their names:
    Annotation 2020-05-20 231138
  6. The OSL shaders will be created as nodes with their editable attributes, that can be connected to an object’s shading network graph:
    * Connecting the node to the graph is the same as described in the previous part (7)
    Annotation 2020-05-20 231232

 

Related:

  1. OSL read-list
  2. What are OSL Shaders?
  3. Using OSL shaders in V-Ray for 3ds max
  4. Using OSL shaders in Blender & Cycles
  5. Writing a basic OSL color shader

5 thoughts on “Using OSL Shaders in Arnold for Maya

Leave a Reply