OSL RIB Box Hack

return to main index


Although RfM19 introduced the possibility of creating custom RIS pattern nodes using the Open Shading Language (OSL) it did not provide a fully functional PxrOSL node. The PxrOSL node can reference an OSL shader but it does not have the functionality to dynamically add UI parameter widgets that "connect" to a shaders inputs. For example, figure 1 shows the attribute editor for a PxrOSL node that references a shader named colorByObjOrigin.oso. Note there are no UI widgets.

Figure 1 - No UI

Figure 2 - With UI
(refer to OSL: Color by Height)

Figure 2 shows what a UI-savvy PxrOSL node will look like when version 21 of RfM is released. In the meantime the work-around introduced in this tutorial and a more complicated approach explained in the tutorial "RfM: OSL RIB Box + TCL Hack" will allow the reader to explore the use of OSL with Maya.

Readers who wish to get started with OSL should refer to the tutorial "Cutter: Open Shading Language"


The code for a very simple OSL shader is given in listing 1.

Listing 1 (RedLeft.osl)

shader RedLeft(color patcolor = color(1, 0, 0),
               color bakcolor = color(1, 1, 1),
               float cutoff = 0.5,
               output color resultRGB = 0) 
if(v <= cutoff)
    resultRGB = patcolor;
    resultRGB = bakcolor;

Step 1 - Compile the Shader

Use Cutter to compile the shader. When Cutter uses the "oslc" shader compiler to create the RedLeft.oso file it's location will be reported in the Process Monitor window.

Figure 3

Step 2 - Test the Shader & Develop a Shading Network

Right mouse click on the .osl document and generate a beauty pass Rib file that can be used to test the shader - for example figure 4.

Figure 4

Save the Rib file and render the scene.

Figure 5

Of course any of the RedLeft's parametes, such as "cutoff" can receive an input from an existing node such as PxrFractal and as a consequence somewhat more interesting shading results can be achieved - figure 6.

Pattern "PxrFractal" "PxrFractal1"
        "int layers" [6]
        "float frequency" [4.0]
Pattern "PxrOSL" "RedLeft1" "string shader" "RedLeft"
        "color patcolor" [1 0 0]
        "color bakcolor" [1 1 1]
        "reference float cutoff" ["PxrFractal1:resultF"]
Bxdf "PxrDisney" "PxrDisney1"
        "reference color baseColor" ["RedLeft1:resultRGB"]

Figure 6

Step 3 - Create a Duplicate of the Shader for Use with Maya

When using a OSL shader with Maya it is convenient to have the shader located in Maya's project directory. Rather than changing the pref that controls where the OSLC compiler saves a shader a duplicate of the shader can be automatically created by Cutter.

Go to the Rman Tool->Options->Shading tab and click the "dest" checkbox. Browse to the Maya project directory that you will use for testing your shader - figure 7.

Figure 7

Compile the shader again but notice the Process Monitor will report where a duplicate of the shader has been placed - figure 8.

Figure 8

Step 4 - Prepare a Rib Archive for Use with Maya

Copy and paste the code of the shading network into an archive rib file. Save the file in the Maya project directory that will use the OSL shader. For example, the following rib was saved as RedLeftNetwork.rib.

Listing 2 (RedLeftNetwork.rib)

Pattern "PxrFractal" "PxrFractal1"
    "int layers" [6]
    "float frequency" [4.0]
Pattern "PxrOSL" "RedLeft1" "string shader" "RedLeft"
        "color patcolor" [1 0 0]
        "color bakcolor" [1 1 1]
        "reference float cutoff" ["PxrFractal1:resultF"]
Bxdf "PxrDisney" "PxrDisney1"
        "reference color baseColor" ["RedLeft1:resultRGB"]

Step 5 - Prepare a Rib Box in Maya for Use with Maya

Make sure that both the RedLeft.oso shader and the RedLeftNetwork.rib have been saved in the Maya project directory. Assign any material (Reyes or RIS) to the geometry that will be shaded with the OSL shader. In the materials shading group node go to the menu Attributes->RenderMan and add a RIB Box.

Figure 9

Enter the following text to the RIB Box.

    ReadArchive "RedLeftNetwork.rib"

Don't worry about the text wrapping in the text field.

Figure 10

Finally, render the scene.

Additional look-dev is accomplished by editing the code in the "RedLeftNetwork.rib" archive and pressing the 'R' key with Pixar's "it" window at the front. If the effect of the OSL shader cannot be seen it is most likely because the Maya project directory has not been set correctly.

© 2002- Malcolm Kesson. All rights reserved.