How do you go from K&R to Blender source code?

Hi there,

I appreciate that this question has sort of been asked before, in that a lot of people have talked about how difficult programming is, how they’re not getting it, how steep the learning curve is, etc. But I have a slightly different question and I’m also looking for advice, so here goes…
Twenty years ago I studied IT in a UK university masters program, a real sink-or-swim fast track one year conversion course for people who had no experience with comp sci (my first degree was in philosophy!). We studied Java, and I did okay - not great, but okay - and I went on to work in the industry for two years, but I didn’t really enjoy it so I left and transitioned into the video industry where I now work as an editor.

About five or six years ago I got into programming mainly as a hobby, but at the back of my mind was the notion that I might someday go back to programming as a job, but maybe more like using Python scripting inside of Blender or Javascript in After Effects, or maybe indie game dev or something like that. I’ve tried various languages in my self study, including C (using K&R), scheme (using the wizard book), Processing, Python, some hobby game development using C# and Monogame, some C++. All of which makes it look like I’ve been flitting around not sticking to anything, which is kind of true, but I’ve intentionally done so. Since it’s mostly been a hobby activity I haven’t felt inclined to learn anything to a high degree of polish. I’ve been doing whatever interests me until it no longer does, or gets so difficult that I feel I’m making no progress whatsoever.

For about the last six months I’ve been using Processing and a couple of weeks ago I thought I’d put some of my learning to the test and see what I could do with Python in Blender. I managed a fairly reasonable music visualisation. It doesn’t look great but I was pleased that the whole thing was scripted, and it showed me that the stuff I’ve been learning hasn’t been a waste, that I understand the vector maths and matrices that I’ve been learning about recently, and that I’m progressing reasonably well. But I took a look at the Blender source code and I just don’t see how my abilities scale up to that. Here’s a random example:

static BMLoop *bm_loop_create(
        BMesh *bm, BMVert *v, BMEdge *e, BMFace *f,
        const BMLoop *l_example, const eBMCreateFlag create_flag)
  BMLoop *l = NULL;

  l = BLI_mempool_alloc(bm->lpool);

  BLI_assert((l_example == NULL) || (l_example->head.htype == BM_LOOP));
  BLI_assert(!(create_flag & 1));

I know some C so I recognise that they’re using pointers, logical operators, etc, but even so, this is almost incomprehensibly difficult to understand. And this is one tiny sample of one file which is thousands of lines long, and there are hundreds of files. How do you go from K&R to this?

I must stress, I’m not beating myself up about my own abilities here - I think my progress since getting back into programming has been a little slow, but I don’t mind that. My question is more general, and I’m also asking for advice - how does anyone get to this level of complexity? What are the next steps from doing the sort of stuff in Zed’s C book or the K&R book to be able to contribute to a project like Blender?

Thanks in advance to anyone who replies, any advice gratefully received.

Yes, so the thing about C is it’s a language that helps you create gigantic systems that are almost whole other languages on their own. It can take you months and sometimes even years to understand a code base the size of Blender, even if you are a total pro at C. If you were a C programmer who did a lot of graphics code then it’d be easier, and if you worked on a similar system it would easier than that, but even then you would need months to learn this code. The reason is most of these systems end up being their own little world and language.

But, there’s nothing in this that’s unusual. The trick is you need to be able to go see what each of those BM* things are. The best tool to do that is cscope, ctags, and if you can find a good IDE that can do even better. I’m a big fan of IDEs when you’re desperately trying to figure out code written by tons of people (even if they get in the way of almost everything else).

Ultimately though, if you just want to use Blender and you have some success with Python then I wouldn’t even bother with the C code.