Blembic – Documentation for Developers
Link to Thread about Blembic on blenderartists.org:
What is it good for?
Blembic is a Mesh Cache system which makes it possible to exchange mesh geometry with varying vertex count from one 3D programm to another one. A Mesh Cache system creates one single mesh per frame, writes this to an individual file in one 3D program and streams these files into another 3D program. So whenever you change a frame the 3D program will load the relevant file and show it in the viewport. When you change the frame again it will remove this file and load the next file, so that you see a consistent stream of meshes.
This is usefull for the exchange of all kinds of animations, especially ones such as fluid or particle simulations where it is the norm that vertex count varies from frame to to frame. There are a lot of other mesh cache formats such as MDD or PC2 but they only work for meshes that have a consistent vertex count and order over the range of the whole animation.
Efficiency through binary files
Simulations such as fluids often involve very large meshes with very many vertices and polygons which means that the import and export has to be very efficient. As far as I can tell it is not possible to efficiently stream mesh caches with regular text based formats such as obj. The import simply takes too long. Therefore it is a lot better to use binary files.
Binary files might sound a bit daunting at first but actually they are very simple to understand. Creating a binary files actually only means that a script (in this case our exporter) translate numbers and letters which can be easily read by humans into letters and numbers which can be easily read by computers. Since the exporter does this translation for us the importer is able to read the incomming files a lot faster.
How it works
– The Blembic exporter moves to the first frame the user wants to export.
– It creates a file in a folder the user has declared.
– Blembic analyses the Mesh
– It reads parameters such as vertex count and material IDs and writes it in to the file
– Blembic closes the file and saves it.
– Blembic moves to the next frame and does the same until all frames are exported.
Since this whole format is still in development the structure might change in future releases breaking support for older exporters.
The structure is relatively simple. The exporter reads and writes in the following order:
1. Number of vertices (1 integer)
2. Vertex position (3 floats per vertex)
3. Number of vertices (1 integer)
4. Number of faces (1 integer)
5. Vertex index number of which each face is made up of (3 integers per face)
6. Number of (Split) Normals (1 integer)
7. Vector of each normal (3 floats per normal)
8. MatID per face (1 integer per face)
9. Number of MatIDs (1 integer)
10. Number of UV Maps (1 integer)
11. Number of Vertices in UV Map (1 integer)
12. UV vertex position (2 floats per vertex in UV Map)
13. Number of Faces in UV Map (1 integer)
14. UV vertex index number of which each UV face is made up of (3 integers per UV face)
Creating binary files with Python
Creating a binary file with Python is simple. You open a file (if it doesn´t exist Python will create it automatically)
If you want to be able to write in binary you have to add „wb“.
myFilePath = "C:\\myTestFolder\\myfile.BBCTEST"
myTestFile = open(myFilePath, "wb")
In order to write a number in binary you first have to pack it. This is done with the struct.pack function.
Inside the brackets you have to declare what kind of number you want to pack. In our case we will be packing an Integer so the first thing in the bracket is „i“.
The second thing in the brackets is „9“ which is the number that will be translated into binary:
myTestStruct = struct.pack("i",9)
myTestStruct = struct.pack("i",9)
And finally we close the file
For a more in depth explanation refer to struct page of the Python manual. It also explains how to open and unpack a binary file. Python Struct Reference
Writig an exporter in Python
So all we have to do now is collect the relevant data mentioned under Belmbic Structure and pack it into our binary file. How collecting this data works depends on the 3D program you are using and you have to look that up in the 3D programms reference manual. It is possible that the 3D program you want to write an exporter for doesn not support Python or has better access to other script languages. The exporter I wrote for 3ds Max for example uses MaxScript. The syntax is obviously different but the principle should be the same.
open file -> get data -> pack to binary -> write to file
If you have any questions feel free to contact me via email, the Blembic thread on blenderartists.org.
Titel: Blembic Documentation
Language: Exporter 3ds Max -> Maxscript, Exporter Houdini (in development) -> Python, Importer -> Python