Tip: Figuring Out How Blender Works

So you’ve been looking around in the Blender source code and you’ve found a function that you are interested in understanding better. If you’d like to see when that function is run during execution of Blender, one way to do it is to insert “printf” statements in the code and recompile. printf will print it’s arguments to your system console (note that to see these you must run Blender from a command line, not launched from your desktop, etc.).

Let’s say that you’ve found a function called bar() and you want to see when bar() executes as Blender is running. Here is bar():


void bar(char *some_arg)
{
    int x, y, z;

....
}

Insert a printf within bar() up near the top but after any local variable declarations, like so:


void bar(char *some_arg)
{
    int x, y, z;

    printf("We're in bar()!
");

....
}

After you have re-compiled Blender and started the new copy, each time the bar() function is run the printf will print We’re in bar()! on the terminal screen.

In the example above we simply used printf to flag when the bar() function was executed, but you can also use printf to examine the value of variables as Blender is running.

The way printf works is to use a ‘format string’ and then an arbitrary number of variables separated by commas. The format string, which we used above as "We’re in bar()!
" can have special characters in it which get replaced by the variables you supply. For example, to view the value of the integer variable ‘x’ in bar() you could change the printf to be:


void bar(char *some_arg)
{
    int x, y, z;

....

    printf("bar(): int x = %d
", x);
}

Notice the ‘%d’ in printf’s format string. This tells printf that you want the variable x to be printed as a decimal number, and that you want it printed between the equals sign and the newline character (’
') within the string.

printf is a very flexible statement. You can get quite sophisticated output using it. Let’s say that you wanted to monitor the value of the argument passed to bar(), which is a char pointer (i.e. usually a string):


void bar(char *some_arg)
{
    int x, y, z;

   printf("bar(): some_arg = %s
", some_arg);

  ....
}

Here we are using ‘%s’ to tell printf that some_arg should be printed as a character string. Each time bar() is called, the printf will print the string that was passed to it as an argument.

Carefully placed printf statements within Blender’s code can tell you a lot about how Blender works. The hard part is deciding where to best put them (and remembering where you put them so you can remove them later!).

For more info on the printf statement consult your system’s programming manual “man 3 printf” or online via google.

PS: If you get compiler errors when using printf it may be that you need to place “#include <stdio.h>” near the top of the file you’re working on. I haven’t encountered that problem yet but it may come up. #include <stdio.h> is a compiler directive that helps the compiler understand what printf is. If you do need to put that directive in, make sure it’s up near the top of the file along with the other #include directives.