One possible coding workflow (On Linux)

Some people wanted to know how I manage my own code, find things in Blender’s code etc. when I code, so I’m going to write about it now.

I use Ubuntu and this should work on Fedora/OpenSUSE/whatever (Of course using their respective package managers). Most things here are also applicable to OS X.

Building Blender quickly and easily

I use CMake, because it seems to be the faster than SCons on Linux.

First, follow this guide:
http://wiki.blender.org/index.php/Dev:2.5/Doc/Building_Blender/Linux/Ubuntu/CMake
Personally, I use the folder names “.bsvn” instead of “blender-svn” and “cmake” instead of “build” because they’re easier to type when using the command line. I’m lazy like that.

Once you have followed that guide and built Blender once, create a script like this:

#!/bin/sh
cd ~/.bsvn/blender
svn up
cd ~/.bsvn/cmake
cmake ../blender
make -j3
make install

(I’m not sure if “cmake …/blender” is necessary)
Personally I use a much longer and more complex script that basically does this, but is more careful about it. This works for simple building though.

Save this as build_blender.sh, save it somewhere out of the way, and make it executable:

$ chmod u+x build_blender.sh

Then create a link on your desktop that runs that script.
I also create a link on my desktop to my build of Blender, you can find it here (If you used my folder names): ~/.bsvn/cmake/bin/blender

Finding things in the source code

Blender has a lot of code. Around a million lines. Although this might be a lot/little, it doesn’t matter; All it means is that if you are looking for something, it can take you a long time unless you know what you’re doing.

This is why grep is your friend. Go to your Blender source directory, in my case ~/.bsvn/blender and use this command to search:

grep -irn --exclude="*\.svn*" "your search string here" *

The --exclude argument makes sure that you don’t waste time searching through SVN’s cache. The -irn arguments mean:

  • Case insensitive (It treats a search for “TEST” and “test” the same)
  • Recursive (It looks through all the directories under the working directory)
  • Line numbers (It tells you which line number each search result appears on)

If you know that you’re looking for something that is not written in Python (Like the Python files where the buttons, sliders, and other UI elements are actually declared), you can go to blender/source/blender and search from there. It will eliminate any Python-related search results and take less time.

There are a few things I could say about the actual source directories and what they contain, but I don’t feel qualified to say more than that the interesting editor code is in blender/source/blender/editors/. Each editor (view3d, properties editor, UV/Image editor etc.) has its own folder. Within each folder, there are files related to drawing the editor/space, the operators that the space uses, the inner workings of the space, and a lot of other things.

Have a look at this image if you want to know more.

Managing your own code

Keeping track of what code you wrote, making sure your code works with the latest revision of Blender, etc.

I use this method of keeping track of my own code:

I was going to write about this myself, but take the time to watch the video instead. There you’ll see what I was going to explain, and you’ll see it happen too. All I’ll say is that it allows you to easily make local branches that you can quickly sync with trunk and create patches from.

Writing code

I use gedit. :slight_smile: It has nice code highlighting and works for me.

Reading code

Reading code can be more challenging than writing code. The only advice I can really give you is to read slowly and carefully, and Don’t Panic. :smiley:

Pssst… you typed ‘git’ instead of ‘grep’. You may wanna fix that.

+1 @Fweeb :slight_smile:

For finding one’s way around in the code I recommend using an IDE such as QTCreator under Linux. Currently I’m trying to understand Blender’s internal renderer in order to accelerate it using OpenCL, so I have to figure out which parts of the renderer code are the most CPU intensive and when those parts of the code are used in the rendering process. With an IDE such as QTCreator one can right-click on a label (a function name or a variable) and select “Find All Usages” from the context menu. In the window below the editor a list of locations where the label is used will appear. You can click on those locations and jump to the lines in the source where the label is used. It’s much quicker than using grep since you can jump back and forth among the references.

Lies and slander!

You can also type ‘make’ in blender’s svn directory and all the cmake stuff is magically taken care of.

After the first time you can edit …/build/linux/CMakeCache.txt to do non-default builds.

There’s also:
make package_debian – name says it all
make package_pacman – for Arch Linux or something?
make package_archive – .tar.gz I’m guessing

And my super-secret, not hooked into the make file so it doesn’t get messed up by committee, ‘make package’ from the cmake build directory that builds a Fedora .rpm.

Upadated it.

I’m not sure why there hasn’t been an update to the instructions at : http://wiki.blender.org/index.php/Dev:2.5/Doc/Building_Blender/Linux/Ubuntu/CMake , but it seems the devs switched to using Python 3.2 for building 2.57 onwards . The only problem with this is that Ubuntu (10.04 to the current 11.04) still uses Python 2.6 as the default python install and the instructions given will not work (you will get an error message about the python3.2-dev install) and you cannot build Blender . Even the Ubuntu software repositories only go up to Python 3.1 and that doesn’t work either .

Apparently Ubuntu requires Python 2.6 and forcing 3.2 as the default will likely screw up Ubuntu so you need to do an alt install of Python 3.2 along with essential dependencies : http://wiki.blender.org/index.php/Dev:2.5/Doc/Building_Blender/Linux/Troubleshooting .

You can skip most of the page and just copy/paste the following in your terminal (unless you are having trouble getting Collada and ffmpeg to work) :

# get required packages for build
sudo apt-get install build-essential libncursesw5-dev libreadline5-dev libssl-dev libgdbm-dev libc6-dev libsqlite3-dev tk-dev
 
# get source
wget http://www.python.org/ftp/python/3.2/Python-3.2.tgz && tar -xvf Python-3.2.tgz
 
# make install
./configure
make
sudo make altinstall
 
# make 3.2 the default python system wide (number 1 at the end stays there)
sudo update-alternatives --install /usr/bin/python python /your/path/to/python3.2 1
 
# ensure various versions of python play nice with each other
sudo update-alternatives --config python

then follow the rest of the instructions at http://wiki.blender.org/index.php/Dev:2.5/Doc/Building_Blender/Linux/Ubuntu/CMake to build blender . I guess you could skip the python3.2-dev install part when installing dependencies, though likely that won’t cause any issues if left in .

You need to do this when building using Scons as well .