Python - how to avoid delay opening Blender?

Hi friends,

I am writing an app that makes calls to blender to modify an object.

Say the user wants to change the material of an object. The app opens blender in background mode and passes in a python script. The script changes the object and saves the file.

Then the user wants to change the location of the object - same thing, blender is opened in the background and told to execute a script.

My problem is that it takes 3 or 4 seconds to open blender each time the user wants to do something. My app has a super slow response time.

Is there a way to avoid this delay from opening blender every time? (Perhaps one instance of blender open at a time, receiving notifications from the app?)

Are you talking about it opening up the blender window? Is that what’s taking so long?

Use the -b argument to make it not open the user interface.

He mentions in his post that he is opening in background mode.

He is totally correct, blender takes 3-4 seconds to open in background mode before processing hte pyhton script.

Best bet is to try to manipulate the blend file without blender… something like this https://code.google.com/p/pyblenderfile/ (inactive but should give you a start)

That could be. Thanks for the link!

Here’s another idea. Can python scripts inside blender use external modules? If I could make a script running in blender listen on a certain port, then it could receive messages telling it to modify the object, render a snapshot, etc. That way I would only have to load the file once, then the script would wait until it received a message on the port to tell it what to do.

Thoughts? Could it work?

I haven’t used Python inside Blender, but it is a full-featured language, but with a limited number of available python modules. A post I found on StackExchange suggested:

Python3.4 in Blender is bundled within Blender. It doesn’t share libraries with system’s python (see this related post).

So, to use extra libraries you need to install it into /blender/2.72/python/lib/python3.4/.

OR you can remove /blender/2.72/python and Blender will fallback to using the Python installed on the system (however the versions must be compatible).

So you should be able to get a full distribution of Python modules including network access, by one of those methods, just replacing the version numbers with the appropriate versions of Blender and Python.

Please note that allowing an app to call Blender and pass it some arbitrary code to run has security risks, so you would only run this in a safe environment.

For any interested parties, I found a better way to do this:

Compile Blender as a python module. (http://wiki.blender.org/index.php/User:Ideasman42/BlenderAsPyModule) Let the app talk to a python script and let the python script talk to Blender. That way, the python script can open a file once and keep it open, listen for input from the app and change the model until the app says it’s done, then close the file.

I’ve tested this and it works great.