Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a python script using an already running blender?

Normally, I would use "blender -P script.py" to run a python script. In this case, a new blender process is started to execute the script. What I am trying to do now is to run a script using a blender process that is already running, instead of starting a new one.

I have not seen any source on this issue so far, which makes me concern about the actual feasibility of this approach.

Any help would be appreciated.

like image 210
Igor Almeida Avatar asked Apr 01 '16 16:04

Igor Almeida


2 Answers

Blender isn't designed to be started from the cli and to then keep receiving more commands from the cli as it is running. It does however include a text editor that can open text files and run the text block as a python script, it also includes a python console that can be used to interactively type in commands while blender is running. You may also find this addon useful as it lets you to run a text block in the python console, this leaves you with an interactive session that contains the variables as they exist at the end of the scripts execution.

There is a cli option to run blender as a python console blender --python-console - the gui does not get updated while this console is running, so you could open and exec several scripts and then when you exit the console, blender will update it's gui and allow interactive use, or if you start in background mode -b then it will quit when you exit the console.

like image 80
sambler Avatar answered Oct 23 '22 05:10

sambler


My solution was to launch Blender via console with a python script (blender --python script.py) that contains a while loop and creates a server socket to receive requests to process some specific code. The loop will prevent blender from opening the GUI, and the socket will handle the multiple requests inside the same blender process.

like image 39
Igor Almeida Avatar answered Oct 23 '22 04:10

Igor Almeida