RfM
Primvar Face Color


return to main index


Related Tutorial:

    Primvars

Some useful mel procs for adding primvars can be found in RfM: Customizing.


Introduction

This tutorial provides an example of how the faces of one or more polymesh surfaces can be colored using a primvar and a single instance of PxrSurface.


Using a Face Color Primvar

Setting Up the Material

1     Assign an instance of PxrSurface to one or more polymesh objects.
2     Click the connection icon of the "Diffuse Color".
3     From the list of RIS patterns connect PxrPrimvar.
4     Enter a name of the primvar, say, greens and change "Variable Type" to color.



Figure 1


Setting Up the Surface(s)

1     Copy and paste the MEL proc, shown in listing 1, into Maya's script window.
Execute the script in order to load it into memory.

Listing 1


// Assigns a color to each face of a mesh.
// 1  Select the objects.
// 2  Execute this command, addFaceColorPrimVar("mycolor", <<0,0,0>>,<<1,1,1>>);
  
global proc addFaceColorPrimVar(string $shortname, vector $minval, vector $maxval) {
    string $sel[] = `ls -sl`;
    string $fullname = "rmanuC" + $shortname;
    for($current in $sel) {
        string $shapes[] = `listRelatives -s $current`;
        string $shape = $shapes[0];
        if(attributeExists($fullname, $shape) == 0) {
            addAttr -ln $fullname -sn $fullname -dt vectorArray -k 1 $shape;
            }
        int     $numFaces[] = `polyEvaluate -f $shape`;
        string     $dataStr = $numFaces[0] + " ";
        float    $r, $g, $b;
        for($n = 0; $n < $numFaces[0]; $n++) {
            vector $rgb = rand($minval, $maxval);
            float $r = $rgb.x;
            float $g = $rgb.y;
            float $b = $rgb.z;
            $dataStr = $dataStr + $r + " " + $g + " " + $b + " ";
            }
        eval("setAttr " + $shape + "." + $fullname + " -type vectorArray " + $dataStr);
        }
    }

Alternatively, copy and paste the code of the MEL proc into a script named addFaceColorPrimVar.mel and save the file into your maya/scripts directory. If the reader has previously followed in the instructions in the tutorial "RfM: Customizing" the proc should automatically be available for use without it first being sourced.


2     Select the surfaces to which PxrSurface was previously assigned.
Execute the following command,

addFaceColorPrimVar("greens", <<0.125, 0.59, 0.24>>, <<0.055, 0.25, 0.101>>);


3     Finally, render the scene.



Figure 2


The start and end colors that are randomized by the Mel proc ie.
<<0.125, 0.59, 0.24>>, <<0.055, 0.25, 0.101>>
were derived from the RGB values of a PhotoShop color picker.



Figure 3

Figure 4







© 2002- Malcolm Kesson. All rights reserved.