Extracting data into C++?

I want to write a C++ assett manipulation thing, but how do I access, say, the location of an obect in a .blend file, from an external C++ program?? Both loading and saving, btw

  • Can you setup a C++ project with libraries and build it? One way is to use assimp library. You can leave the .blend file as it is. http://assimp.sourceforge.net/main_features_formats.html

  • Another easy way is to actually loop all objects in the scene and export their name and position in text format (for o in context.scene.objects: print(o.name + str(o.location)). You can write a script export_objects.py and then create a .bat file that calls blender from command line like this, in GUI-less terminal form: blender.exe myscene.blend --python export_objects.py -background.
    Sorry if all these sound difficult, if you want more info let me know.

  • Something related to the subject:
    Save bone position and rotation in a .txt file - #2 by const

1 Like

Looking at the web info, does assimp (love the name, is it an imp WITH or IN the rectum?) have good options outside format to format translation? I need to do my own format.

My first impulse, but there is a lot of info in a standard scene. Hoping there was a shortcut, or someone had done and shared the work…?

Anything! Don’t hold back on me!

Same as above, my go-to idea, but it looks daunting and I am checking for shortcuts or alternates before I venture down that arduous road!

1 Like

Yeah sounds funny. Though I am not a native English speaker to feel the real power of the pun, but it still hits me. :slight_smile:

Now I remember that there is this blend2json script, though I have not used it yet I see lots of potential in it.

https://github.com/blender/blender-dev-tools/blob/master/utils/blend2json.py

At least is very concise and saves you a ton of boring boilerplate code.

If I understood the question correctly, I think that that this library is more like a “game-engine-ready” type of import and not one with full features. As for example you have no bezier curves, that would be very useful for path animations, also you have no empty objects. So how handle object animations? A workaround is to use a blank mesh object that is supported and use it to transfer animations in it.

https://github.com/assimp/assimp/blob/884bb39391da04d2b5e03672c2029b2ff0191a28/code/AssetLib/Blender/BlenderLoader.cpp#L1252

With python2json you have the extra step of generating the data files but at least you can get everything. If you have no problem with the serialization part then this is the real deal. With assimp you can retain the .blend file intact which is huge productivity boost, but making some compromizes with the supported features.

And THAT is definitely a nice thing, if I can make it work!

I think I’m going at this from the wrong angle. I basically want to convert a full .blend file, animations and all, to individual text files detailing each part (a material, an NLA action, etc.), to create an externalized file format. Translate a full file into text, and translate that text back into the full file. Not something people do often, it seems…

Since I am interested in this as well, I had a little test going on with the blend2json.

I have a cube.blend which is a test file, with two animation keyframes going up. The generated json file is about 88.000 lines of json (about 5000 closing brace lines - blank), about 2.6MB.

The cube description is about 200 lines long and it looks like this:

{ "code": "OB",
      "size": 1384,
      "addr_old": 2025423586,
      "dna_type_id": "Object", ...
      { ...
        ""id", "name"": "OBCube", ...
        "mat": 20, ...
        "loc":
        [0.0, 0.0, 0.0], ...
        "size":
        [1.0, 1.0, 1.0],
        "rot":
        [0.0, 0.0, 0.0], ...

The keyframes in the same style, they are written as bezier curve values (as expected). It seems that there is no way to get the position of the object, unless you try to recreate the entire animation code.

The typical way of exporting animations is to change the scene current frame and get pos-rot of objects.

Bottom line is that the blend2json is very useful only for working on the data side of things. As for example if you convert the blend file to godot file makes a good starting point.

However if you want to get calculated behavior at runtime (baked things) having an exporter is the right way.

blendster.zip (110.1 KB)

1 Like

distant screams of agony

There HAS to be an easier way. I think maybe just straight up python scripting it into a text file would be a fraction of that work…

Yeah the most boring way is the most practical. :slight_smile:

2 Likes

You can use PyBind11 to interface with Blender’s Python API.