The OSL shaders used in the above examples can be found here.


It is possible to create your own shaders using Open Shading Language. OSL shaders can be used to implement anything from simple texture patterns to full materials using closures. They can be placed in the plugin search path, and will then be automatically loaded like other types of shaders. Once loaded, they can be inspected, instantiated, and linked in the same way as C++ shaders. The osl node provides an alternate way to load OSL shaders, which can be used to write shader code for a specific material. When setting the shader name or code, the parameters from the OSL shader automatically appear on the node to be set or linked.

More information about using OSL shaders can be found here

 A video that shows how to use the OSL shader can be found here.

An example .osl shader and .mtd file can be found here.

Limitations

  • The shaders must have unique names. If the name conflicts with an existing shader, the OSL shader won't load. You should see a warning in the log in this case.
  • The name of the output attribute is not the same as the shader code.
  • Unable to do #includes in OSL shader code.
  • No support for exposing input attributes of type Array and Matrix.


Multiple Outputs

Shader outputs can be linked to inputs of other shader nodes in an .ass file or programmatically via the AiNodeLinkOutput() function by optionally specifying which output is desired when linking. When no output is specified, the node's default output parameter will be chosen, preserving Arnold's existing behavior. Standard OSL language rules apply when specifying multiple OSL shader outputs. An example OSL shader with three color outputs could look like the following snippet and each output could be connected separately :

shader test_shader
 (
    output color result = color(1, 1, 1),
    output color good = color(0, 0, 1),
    output color bad = color(1, 0, 0)
 )
 {
 }
An example .ass file using multiple outputs can be downloaded here.

  • Only intfloatpointvectornormalcolor and string parameters are supported.
  • Default values are always zero for parameters.
  • Widgets are not set for color parameters.
  • Only one output parameter is supported.
  • No array or structured input or output parameters.
  • Closures are not supported.
  • Only a single shader can be defined on each node.
  • Including other header files is not supported.
  • Global variables (u, v, etc.) are not supported as parameter defaults.



OSL Inline Shader Workflow

After editing the code, either externally or in the code editor, the Compile button needs to be pressed to generate the parameters. In case of a compilation error, no parameters will be added to the node, and a compilation_errors text field will contain the errors from the OSL compiler.

The code and the parameters can be compiled using python, for example:

osl = NodegraphAPI.GetNode('myNode')
from ktoa import recompileOSL
recompileOSL(osl)

errors = osl.getParameter('parameters.compilation_errors')
if errors is None:
    # successful compilation
    pass
else:
    print('Errors: \n%s' % errors.getValue(0.0))



  • No labels
© 2020-2021 Autodesk. All Rights Reserved / website terms / privacy/cookies / ccpa settings