XML preprocessor¶
Before parsing the XMl content, a preprocessing pass is done.
Tags of the form <% ... %> are preprocessor directives, and will be removed or replaced during the preprocessor pass, before the XML parsing.
Variables¶
Preprocessor variables are defined using the set directive, and their value is pasted using the syntax $variable.
Redefining a variable overrides their previous value.
Variables’ values can be overridden via user-supplied defines, for example via the Command line interface.
Variables example
1<%set spe = 1024 %>
2<%set direction = forward %>
3
4<integrator type="plt_path" >
5 <string name="direction" value="$direction"/>
6</integrator>
7
8<sensor type="prespective">
9 <integer name="samples" value="$spe" />
10</sensor>
Flow control¶
A few flow control directives are supported.
Conditionals¶
Conditional if-else statements are supported, using the <% if (expr) %>, <% else if (expr) %>, <% else %> and <% endif %> directives.
Conditionals example
Conditional preprocessor directives may be used as follows:
1<%set optical_preview = true %>
2<%set pdepth = 16 %>
3
4<integrator type="plt_path">
5 <% if ($optical_preview==true) %>
6 <string name="direction" value="backward"/>
7 <% else %>
8 <string name="direction" value="forward"/>
9 <boolean name="russian_roulette" value="<% if ($pdepth>8) %> false <% else %> true <% endif %>"/>
10 <% endif %>
11 <integer name="max_depth" value="$pdepth" />
12</integrator>
Loops¶
Simple for loops can be implemented using the <% for var=(start expr) to (end expression) %> directive.
The start index and end index are both inclusive.
Math expressions and variables can be used for the expressions for the start and end indices.
For loop example
The following example instantiates N (but not less than 2) screens spaced apart a distance of D metres, each W wide and H tall.
1<% for x=1 to (MAX(2,$N)) %>
2<shape type="rectangle">
3 <point name="p" x="($Xstart + $x*$D) m" y="0 m" z="0 m" />
4 <point name="x" x="$W m" y="0 m" z="0 m" />
5 <point name="y" x="0 m" y="1 m" z="0 m" />
6 <ref id="screen_bsdf" />
7</shape>
8<% endfor %>