Using OpenCV in Blender Python script


I’m trying to use the opencv python bindings within blender. When I try to import opencv (from import *) I get this error: ImportError: No module named
But when I try to run the same script out of Eclipse (with pyDev Plugin) I have no Problem at all.

It seams that the Blender Python interpreter uses not the same include paths as the system python install.

Another problem is that when I’m trying to include a class in an other file that is in the same dir like the Blender Python script, I get the same error. I could fix this by just adding the following lines:

import sys

But I think thats not the best solution. Is there any way how I can tell Blender to use the same include paths than the system python install?

Thank you very much for any help!

What OS, blender version and python version are you using?

Are there any errors before the one you posted? Is it finding your installed python?


thanks for your reply. I’m using Mac OS X 10.5.5. Blender (2.48a) seams to use python 2.3 but when i run programs out of pydev (eclipse) its python 2.5.

There is no other error.


nice to see anybody else using blender + openCV.

I had the same error on Ubuntu linux, but realized blender want’s you to import directly sub-libraries, like this:

from opencv import cv 
from opencv import highgui 
from import * 
from opencv.highgui import *

remember, this is without writing import opencv before that… weird.
I did this in the game engine, and I’m not sure if it works the same in the blender interface, but maybe…
I also noticed this doesn’t relate to the game player, where it just works the to-be-expected way.
good luck with this problem, it made me a little headache too :slight_smile:

I finished my first try using OpenVC and doing camera movement detection.
If you wanna look at the python-script code, it is in the blend-file to generate the chessboard-pattern pictures in this posting:

The python-OpenCV-script is not for running inside blender. It has to saved locally and run. I think as soon you use OpenCV-lib you can get in trouble when using the highgui-part. That is the part for GUI in/output.

Besides this a simple script can run inside blender, for example something like this:

# test for opencv
from import *
distortion = cvCreateMat(5,1,CV_32FC1)
i = 0
for l in (-0.1, 0.19, 0.0, -0.004, -0.7):
	cvmSet(dist, 0, i, l)
	i += 1
print distortion

creates a simple matrix, like used for the distortion camera params in OpenCV camera calibration functions.
But take care if using more of the memory special OpenCV routines. OpenCV comes with a whole bunch of own memory, math, GUI routines.


thank you very much for that advice. Now I can Import OpenCV without a problem :slight_smile:

But how can I run a Blender Python Script outside of Blender?

What I’m trying to do, is to move a cube with my head along the x and y axis. So I made a python class extending Thread that detects my head from a webcam. That thread is called in the main script, where the main-loop gets the x and y position an puts the box to that position. Now, basically it works but its very very slow. will it perform better if I use the build in game engine?

Thanks again for your help.

Great thread, it was such a pleasure to finally solve this problem, but I’m interested in this question too!

But how can I run a Blender Python Script outside of Blender?

There is no way to run Python scripts using the Blender API without an instance of Blender running. This is however not exactly what you want. What you want is to run a script in/with Blender without the GUI popping up, or even on a headless system, like a web server.

There are two ways to achieve this:

  • Recompile Blender as a Python module [difficult]
  • Run Blender with the --background option and running an external Python script: “blender -b -P”

The second option works with all modern Blender versions out of the box, so this is preferred.

Blender binaries are usually distributed with their own Python environment, which is unfortunately very restricted. This environment lacks any of the modern package management features and of course no VirtualEnv support. It can be very difficult to get compiled extensions working with Blender.

Especially on Windows, Blender supports using the “System Python”, again without recognizing VirtualEnvs. It’s not trivial to set up, but if it works, at least you get to have some package management.

Due to these limitations, Blender scripts are notoriously difficult to distribute if they have any dependencies at all. Pure Python, no deps? No problem, just drop the script in the right directory and everything is fine. Any dependencies? Good luck finding the right directories on every system/version/installation.

This situation is highly frustrating, especially because the Python community outside Blender has solved the “binary packages for multiple platforms” very well by now. Even Microsoft released a free Version of MSVC just to compile Python packages.