10

I know Python is the standard scripting language for use inside Blender, but I didn't find a way to create a .blend file with python.

What I want to do is not to use python inside blender, but rather "use blender (libs?) inside python".

My planned workflow would be the following:

  1. Define some parameters for my model;
  2. Define a "generative recipe" to create appropriate Blender objects that will be saved to file;
  3. Create a python script to store the parameters and procedures. When the script runs, some .blend file is created in the same folder;
  4. Use Blender to visualize the model. If model needs to be changed, make changes to the script, run it again, and open it again.
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • Yes, it's possible. What does a blend file look like? Regardless if it's text or binary, you can create any kind of output file using Python. The question becomes: are their any appropriate libraries (e.g XML) to help you? – Jonathon Reinhart Apr 27 '14 at 17:07
  • @JonathonReinhart should I have mentioned that my "generative recipe" involves high-level object-creation blender commands? – heltonbiker Apr 27 '14 at 17:11
  • @heltonbiker how about something like this: a (generic) python script that copies an empty .blend file, run the Blender Python script (open a python subprocess calling blender from command line(no GUI)) to use the "generetive recipe" and save the file ? – George Profenza Apr 27 '14 at 18:16
  • @heltonbiker Also, what is you main goal ? (For example, if you simple want to generate some geometry and render it nicely, you can probably export a mesh without blender and use an external renderer (like POVRay, yafaray, mitsuba, luxrender,etc.)) – George Profenza Apr 27 '14 at 18:22
  • My main goal is to create the model programmatically/procedurally/numerically/non-interactively instead of by using the mouse. Also, my model needs to be dimensionally accurate, so I would most probably be using trigonometric calculations in order to properly position vertices, edges and such. – heltonbiker Apr 28 '14 at 13:59

2 Answers2

12

There is an experimental option that allows you to build your own copy of blender as a python module to use like a standard python module. This page is about all the help you will get for it.

I would recommend you write your python script as a blender script to be run inside blender. You can automate saving the file using bpy.ops.wm.save_as_mainfile(filepath="path/to/myfilename")

You can run a script inside blender that generates a blender text block that you can then run as a python script, select all, delete and re-run. You can also easily reload an external file as a text block in blender if you prefer an external text editor for it's features.

You can also use your plan to generate the script yourself which you can then run using blender with blender -b -P myscript.py the -b option tells blender to run in the background without a GUI.

sambler
  • 6,917
  • 1
  • 16
  • 23
9

You can start a new Blender process from any application (a C++, Python app or even command line) and tell the new process to run a script file (written in Python). This script will generate your geometry and then can save the new scene to a blend file.

To start a new Blender process and force it to execute a script use:
blender.exe --background --python "c:\path to\script.py"

Val
  • 1,548
  • 1
  • 20
  • 36