Today I'm investigating how difficult it is to write a mesh export plug-in for Blender3D in Python. A previous observation is that Blender3D plug-ins are very unlikely to actually work with slightly newer releases/versions of the Blender software. I'm keeping this in mind as I read instructions, as I expect a lot of interface stuff to vary a bit from the instructions when I actually go to write it. Opening a few extant plug-in scripts makes them look fairly simple, and oddly enough the Blender data structures look quite simple and sensible. It would be great to be able to go directly from Blender to my new custom format (previous post) without relying on a file conversion stage. The down-side to this route is that I'll probably need to maintain a Blender script in different versions, as well as potentially a Max or Maya export script. Anyway, I'll jot stuff down here as I give it a go.
I found a nice step-by-step guide to building an .obj exporter at http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Advanced_Tutorials/Python_Scripting/Export_scripts. I'll try to walk through this first.
Blender has a built-in Python console for interactively querying stuff that's in your current scene in Blender, in the same way as the final export script will do. That's pretty cool. Now, I'm using Blender 2.65 at work (and I think an even newer version at home). I notice straight away that if I query the contents of my mesh that there is no faces. There is a polygons though, and that looks about right - there's 6 of them, and my test cube should have 6 square faces.
Okay, next difference to documentation (which I think was for version 2.5 or so) - the path where scripts should go. The tutorial said ~/.blender/scripts which seems sane, but that didn't seem to work. Other scripts are reported as being in ~/.config/blender/2.65/scripts/addons/. I got lost in the menu system, and I just couldn't find the button to "update menus", so I gave up and just closed and re-opened the programme. Maybe it's done dynamically now? Anyway, it didn't show up when I put it in the new folder either, so I guess some script spec has changed. Let's open up another one...
It looks like the entire descriptor format has changed. It needs a bl_info block now instead, according to http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Guidelines/Addons it should look like this:
...and it appears in the User Preferences→Addons menu! I can't enable it though - the BASH console says:
So I went into the interactive console and fooled around. There was a bpy.data.scenes, but not a bpy.data.scenes.active. There didn't appear to be any objects in the scene - perhaps just iterating over bpy.data.objects is fine. This seems to work, taking care to mind Python's meaningful whitespace:
Okay, it looks like the Blender interface in that tutorial is different to the 2.65 version (COMPLTELY different); http://www.blender.org/documentation/blender_python_api_2_64_release/info_quickstart.html#integration. I'll need some sort of class interface with register and unregister thingies.
I followed that tutorial instead - it works! Not only did i find the secret script interface, but I notice that there's like 3 different terminals; the "what is the script runner doing" terminal up the top of the scripts view, the interactive console down the bottom, and the actual print out stream, which goes to the BASH console window from which you ran Blender. Yup.
I opened up the STL format exporter script, and copied the layout. I notice that the main "init" Python script pulls in 2 additional utils scripts which are in the same folder. I would like to put everything into one file. I copied the STL plugin layout, deleted all the "import" functionality (I just want to export to start off with), removed hooks to external files, and renamed all the STL bits to APG. It now looks like this:
And it works! I can now click the tick-box to add the add-on to Blender, and it appears in the export scripts menu! I can even choose a filename to export. It won't work yet because my write_apg() function isn't defined yet. If I try to export a pop-up tells me this too, which is nice. I think I can write this function inside the export class there.
For a cube I managed to get a file written out. I swiped the STL exporter's function that gets a list of faces for an object. It also triangulates a mesh which is nice. For a cube I got 36 3d vertex points written, which is exactly what I want; duplicated points and all.
Now, presumably I can look at formatting the static mesh part according to my custom layout, and adding normals and texture coordinates too. The animation part can come later, but I guess I can more or less copy how the simple BVH exporter works.
To be continued...