Arnold Plugin Development – Adding Custom Parameters (Patreon)
Downloads
Content
We can upgrade the Arnold plugin we created in the last article with a few custom parameters. Let’s look at how!
Our camera plugin has two main features: it creates distortion and it adds a vignette. Initially, the magnitude of both is hardcoded, but that’s not too useful. A flexible plugin should have all relevant parameters exposed. In this case, it would be nice for the user to be able to fully control the amount of noise and the size of the vignette.
Let’s look at two lines of code in particular:
output.origin = noise_vector * 0.04f;
and
output.weight = 1 - dist2;
These set the output values of the noise and vignetting, respectively. To dial them in we can attach a simple multiplier to each. Multiplying by zero would mean no effect, multiplying by 1 would be the default effect, and any other value would set a custom amount.
Go to the parameter declaration section of the code, node_parameters, and add two variables:
node_parameters{ AiParameterFlt("fov", 60.0f); AiParameterFlt("vig_mult", 1.0f); AiParameterFlt("noise_mult", 1.0f);}
The fov parameter already exists, but we added two new float parameters: vig_mult and noise_mult. How do we access these values?
Looking at the existing code, we can see the syntax Arnold needs to retrieve variable values:
data->tan_fov = tanf(AiNodeGetFlt(node, AtString("fov")) * AI_DTOR / 2);
In this case, AtString("fov") fetches the value of fov. We can use similar syntax to retrieve the values of vig_mult and noise_mult and apply it to our outputs:
output.origin = noise_vector (0.04f * AiNodeGetFlt(node, AtString("noise_mult")));
and
output.weight = 1 - (dist2 * AiNodeGetFlt(node, AtString("vig_mult"));
Build the plugin again with these changes. (The full code is available for download below.) Now, when we render from kick we can control our two new variables from the command line, using, for example:
kick -set mycamera.vig_mult 1.2 -set mycamera.noise_mult 2
Easy and powerful!