6
$\begingroup$

I have a python application which creates 3d models. It is already able to export these models as FBX, DAE, OBJ etc. In the vast majority of cases, the next step in the pipeline is touching up the model inside blender, which means I then have to import the models in blender from the said export formats.

However, some blender-specific features aren't possible to express in these file formats. The obvious idea to solve this would be exporting directly to a blend file instead of to a third-part unrelated file format.

Researching ways to do this, I've found http://wiki.blender.org/index.php/User%3aIdeasman42/BlenderAsPyModule which seems like one approach. However, this seems to require a custom-built blender.

I have also found the binary specifications for the file format, notably http://wiki.blender.org/index.php/Dev:Source/Data_Structures/DNAStructs and (some other links which I'm not allowed to include in the post atm).

... so obviously it'd be possible (albeit cumbersome) to write an export function with a bit of trial an error.

However, before I do this: Has this already been done? Is there an existing library/API out there for producing blend files outside blender?

$\endgroup$
2
  • $\begingroup$ Question, how fixed is the specifications? $\endgroup$
    – user877329
    May 19, 2015 at 16:50
  • $\begingroup$ If we're talking about the DNAStructs etc, it's my impression that this is not a formal file format specification, just an indication of how it usually looks. Blend files are more akin to memory dumps than structured data organizing. But I'm by no means an expert on it. $\endgroup$ May 20, 2015 at 20:18

3 Answers 3

3
$\begingroup$

I don't know of how to export directly to Blender (seems overly complicated), but you could import PLY/STL/etc files, and then script all the additional features you want.

In my case, I was free to choose the original CAD program, so I chose to build directly in Blender via the PyModule. I work with Blender completely separate from the GUI, and rely only on an external python3 and the bpy module. The bpy module is easy to make.. and you can keep it largely separate from your existing blender install (I run from source folder).

Just run this command (make sure you don't have a $HOME/src):

git clone http://git.blender.org/blender.git && cd blender && git submodule update --init --recursive && git submodule foreach git checkout master && git submodule foreach git pull --rebase origin master && build_files/build_environment/install_deps.sh --with-all --skip-osl && sudo make bpy && cd .. && rm -rf blender && sudo rm -rf build_linux_bpy && rm -rf $HOME/src # this gets bpy python module

I also found this: https://code.google.com/p/pyblenderfile/ but, honestly, I don't see much benefit. Its not developed enough to allow importing everything, so I found a better way of doing the same thing using the bpy module.

What I do is this:

-- build stuff in blender (I do it via python, without ever starting the official application)

import bpy
#build stuff
bpy.ops.wm.save_as_mainfile(filepath="./objects8.blend")

-- import the model

import bpy
bpy.ops.wm.open_mainfile(filepath="./objects8.blend")

Works great for me. I have some issues recreating stuff you can do in the GUI, but its mostly because Blender has terrible documentation for code (imo). Still, you can do sweet stuff since you call all the equivalent commands directly in python and it executes using the bpy module.

$\endgroup$
1
  • $\begingroup$ Using blender api was an approach we used before but largely abandoned due to frequent API changes in blender, and the load of support requests from users who couldn't figure out how to install a plugin in blender. But the pyblender file looks like a readable start for figuring out the basic structures of the blend file. Thanks! $\endgroup$ Jun 30, 2015 at 7:33
1
$\begingroup$

There is no official API for writing blender files outside of blender. a few people have written ways of reading blender files though. the only one i have seen in python that is open is Blender Aid https://code.google.com/p/blender-aid/

it reads the blender file and enters information into a database about links from one file to another.

It could be a good starting point for you to write your own script to create blend files if you choose to go this way.

$\endgroup$
1
$\begingroup$

BAM (Bam Asset Manager) is able to manipulate .blend files (it can extract/pack everything needed by a given .blend (libraries, images...) into a single file), you may want to have a look at its code.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .