Read my book

I wrote books about Webpack and React. Check them out!

Saturday, September 26, 2009

Handling branching imports in Python

The premise of this problem is quite simple. I am currently developing a user interface library, Scocca. The basic idea is that it provides a generic programming interface that works in multiple user interface frontends. So in principle the same code should work without any changes in Blender, PyGTK, PyOpenGL, PyQT etc. environment as long as the needed frontend code has been implemented.

Most of my effort has gone into making the backend work and to provide simple frontend for Blender. Blender and PyOpenGL frontends share drawing their OpenGL drawing code. Clearly it does not make sense to have some sort of conditional check in each module of my drawing package. So how to handle it instead? The solution is quite simple and can be seen in my root package __init__.py:

The interesting part is the way __init__.py exposes GL. In my drawing modules I can access GL just by using "from scocca import GL" without having to care which module it actually happens to import.