PixelScript Basics (Studio)

Top  Previous  Next



PixelScript is syntactically similar to the C programming language, but has differences to make it better suited to writing graphics filters.  Here's a summary of the basics of this language.



real myNumber = 3;


PixelScript is a strongly typed language, which means that variables must be explicitly declared with a given type before they can be used.  Here the variable "myNumber" is declared with a type of real (equivalent to float in other languages) and assigned a value of 3.  Available types are listed here.


myNumber /= 4;


The previously declared variable is divided by 4, giving it a new value of 0.75.  PixelScript supports standard C-language operators such as +, -, *, /, ++, --, =, +=, -=, *=, /=, %=, ==, <, >, <=, >=, !=, &&, and ||.




Outputs the variable to the debug pane, which in this case has a value of 0.75.  This won't affect the rendered result, but is useful for figuring out if a script is executing as planned.




debug(max(2, 3));


Outputs "3" in the debug pane.  PixelScript has a large number of built-in functions. In this case, the "max" function selects the larger of the two numbers.


result.R = myNumber;


"result" is a built-in variable that accepts the output color of the script.  This example assigns the value of the variable into the red channel of the resulting pixel.  Because "myVariable" has a value of 0.75, the resulting image will be red of 75% intensity.  You don't need to loop over the pixels yourself to copy the value into each pixel since Genetica will ensure that your script is run for each pixel in the resulting image.




result.RGB = myNumber;


Assigns the value to the variable to the red, green, and blue components of the resulting pixel.




debug("x has a value of " + x);

debug("y has a value of " + y);


Built-in variables "x" and "y" contain the coordinates of the pixel that is currently being rendered.  Although your script is run for every pixel in the image, debug output is only displayed for a single pixel of your choosing, as shown below, to prevent thousands of debug messages from being displayed.  Because PixelScripts are resolution-independent, pixel coordinates are scaled to a range from zero to one.




result.R = x;


Since x-coordinates increase rightwards, assigning "x" to the red channel of the resulting image will create a linear gradient.




result.R = x;

result.G = y;


In this example, "x" is assigned to the red channel and "y" is assigned to the green channel.  One of the key things to remember about his language is scripts are written as if they were intended to only operate on a single data point, and Genetica will automatically loop over all pixels in the image and run your script for each one.  Therefore the script to the left is all that is needed to produce the following image.  Paste it into the code pane of the Edit PixelScript dialog and try for yourself.




result = color(x, y, 0);


"result" is of type color, which means the previous example could also have been written like this.  Learn more about the color type here.




result = input(1, x, y);


Inputs of the PixelScript node can be accessed with the input function.




result = input(1, x * 2, y);




if (3 < 2)


  debug("This won't appear");




  debug("This will appear");



PixelScript supports C-style if-statements.  In this case "This will appear" will be printed to the debug pane.

for (int i = 0; i < 3; i++)


  debug("Current value of i is " + i);



C-Style loops are also supported this will add the following to the debug pane:


Current value of i is 0

Current value of i is 1

Current value of i is 2


int i = 0;

while (i < 3)


  debug("Current value of i is " + i);




This loop is an alternative form of the previous example, and will produce the same output.

real[] values = new real[5];


for (int i = 0; i < values.Length; i++)

  values[i] = pow(2, i);


for (int i = 0; i < values.Length; i++)



Arrays can be defined with the new keyword and square brackets indicating the number of elements in the array.  Outputs:








// This single-line comment does nothing


/* This multi-line comment

also does nothing */


Code comments can be defined with "//" or "/*" and "*/", as in other C-style languages.


Continue your exploration of PixelScript by reading about types.




Page URL: http://www.spiralgraphics.biz/genetica/help/index.htm?pixelscript_basics.htm