This example C++ code uses the Arnold API to create a basic scene containing a sphere, a textured quad mesh, a point light and a camera. It will then render the scene to a user-specified JPEG file on disk.

Source Code

example.cpp
#include <ai.h>

int main()
{
   // start an Arnold session, log to both a file and the console
   AiBegin();
   AiMsgSetLogFileName("scene1.log");
   AiMsgSetConsoleFlags(AI_LOG_ALL);
 
   // create a sphere geometric primitive
   AtNode *sph = AiNode("sphere");
   AiNodeSetStr(sph, "name", "mysphere");
   AiNodeSetVec(sph, "center", 0.0f, 4.0f, 0.0f);
   AiNodeSetFlt(sph, "radius", 4.0f);
 
   // create a polymesh, with UV coordinates
   AtNode *mesh = AiNode("polymesh");
   AiNodeSetStr(mesh, "name", "mymesh");
   AtArray* nsides_array = AiArray(1, 1, AI_TYPE_UINT, 4);
   AiNodeSetArray(mesh, "nsides", nsides_array);
   AtArray* vlist_array = AiArray(12, 1, AI_TYPE_FLOAT, -10.f, 0.f, 10.f, 10.f, 0.f, 10.f, -10.f, 0.f, -10.f, 10.f, 0.f, -10.f);
   AiNodeSetArray(mesh, "vlist", vlist_array);
   AtArray* vidxs_array = AiArray(4, 1, AI_TYPE_UINT, 0, 1, 3, 2);
   AiNodeSetArray(mesh, "vidxs", vidxs_array);
   AtArray* uvlist_array = AiArray(8, 1, AI_TYPE_FLOAT, 0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f);
   AiNodeSetArray(mesh, "uvlist", uvlist_array);
   AtArray* uvidxs_array = AiArray(4, 1, AI_TYPE_UINT, 0, 1, 2, 3);
   AiNodeSetArray(mesh, "uvidxs", uvidxs_array);
 
   // create a red standard surface shader
   AtNode *shader1 = AiNode("standard_surface");
   AiNodeSetStr(shader1, "name", "myshader1");
   AiNodeSetRGB(shader1, "base_color", 1.0f, 0.02f, 0.02f);
   AiNodeSetFlt(shader1, "specular", 0.05f);
 
   // create a textured standard surface shader
   AtNode *shader2 = AiNode("standard_surface");
   AiNodeSetStr(shader2, "name", "myshader2");
   AiNodeSetRGB(shader2, "base_color", 1.0f, 0.0f, 0.0f);
 
   // create an image shader for texture mapping
   AtNode *image = AiNode("image");
   AiNodeSetStr(image, "name", "myimage");
   AiNodeSetStr(image, "filename", "solidangle_icon.png");
   AiNodeSetFlt(image, "sscale", 4.f);
   AiNodeSetFlt(image, "tscale", 4.f);
   // link the output of the image shader to the color input of the surface shader
   AiNodeLink(image, "base_color", shader2);
 
   // assign the shaders to the geometric objects
   AiNodeSetPtr(sph, "shader", shader1);
   AiNodeSetPtr(mesh, "shader", shader2);
 
   // create a perspective camera
   AtNode *camera = AiNode("persp_camera");
   AiNodeSetStr(camera, "name", "mycamera");
   // position the camera (alternatively you can set 'matrix')
   AiNodeSetVec(camera, "position", 0.f, 10.f, 35.f);
   AiNodeSetVec(camera, "look_at", 0.f, 3.f, 0.f);
   AiNodeSetFlt(camera, "fov", 45.f);
 
   // create a point light source
   AtNode *light = AiNode("point_light");
   AiNodeSetStr(light, "name", "mylight");
   // position the light (alternatively use 'matrix')
   AiNodeSetVec(light, "position", 15.f, 30.f, 15.f);
   AiNodeSetFlt(light, "intensity", 4500.f); // alternatively, use 'exposure'
   AiNodeSetFlt(light, "radius", 4.f); // for soft shadows
 
   // get the global options node and set some options
   AtNode *options = AiUniverseGetOptions();
   AiNodeSetInt(options, "AA_samples", 8);
   AiNodeSetInt(options, "xres", 480);
   AiNodeSetInt(options, "yres", 360);
   AiNodeSetInt(options, "GI_diffuse_depth", 4);
   // set the active camera (optional, since there is only one camera)
   AiNodeSetPtr(options, "camera", camera);
 
   // create an output driver node 
   AtNode *driver = AiNode("driver_jpeg");
   AiNodeSetStr(driver, "name", "mydriver");
   AiNodeSetStr(driver, "filename", "scene1.jpg");
 
   // create a gaussian filter node
   AtNode *filter = AiNode("gaussian_filter");
   AiNodeSetStr(filter, "name", "myfilter");
 
   // assign the driver and filter to the main (beauty) AOV,
   // which is called "RGBA" and is of type RGBA
   AtArray *outputs_array = AiArrayAllocate(1, 1, AI_TYPE_STRING);
   AiArraySetStr(outputs_array, 0, "RGBA RGBA myfilter mydriver");
   AiNodeSetArray(options, "outputs", outputs_array);
 
   // finally, render the image!
   AiRender(AI_RENDER_MODE_CAMERA);
 
   // ... or you can write out an .ass file instead
   //AiASSWrite("scene1.ass", AI_NODE_ALL, FALSE);
 
   // Arnold session shutdown
   AiEnd();
 
   return 0;
}

Compiling

The following commands compile and run the example on various platforms:

Linux
export ARNOLD_PATH=/path/to/arnold
c++ example.cpp -o example -Wall -O2 -I$ARNOLD_PATH/include -L$ARNOLD_PATH/bin -lai
./example
macOS
export ARNOLD_PATH=/path/to/arnold
c++ example.cpp -o example -Wall -O2 -I$ARNOLD_PATH/include -L$ARNOLD_PATH/bin -lai
./example
Windows Visual Studio command prompt
set ARNOLD_PATH=c:/path/to/arnold
cl example.cpp /I %ARNOLD_PATH%/include %ARNOLD_PATH%/lib/ai.lib /link /out:example.exe
example

Output

Once rendered, the output should look like this:

  • No labels