Diary - B3D Exporter for Blender 2.6.x

Monkey Archive Forums/Monkey Projects/Diary - B3D Exporter for Blender 2.6.x

bruZard(Posted 2012) [#1]
I've learned something about Python and the Blender 2.6 API ... the B3D Specs are not complicated ... read the data from a Blender scene is complicated ;)
At now i've created some procedures to write b3d specific data in a hardcoded file and i can write the header and some other chunks ... i'am really far away from exporting a complete mesh ... i think we want a stable exporter which can all the funny things that B3D can do ;)

just for fun, my Blender Environment for scripting this Exporter:




AdamRedwoods(Posted 2012) [#2]
yes, good luck, this is very useful.

Another thought is a collada->b3d converter. I actually have a half-built collada importer for blitzmax minib3d, but collada is *TERRIBLE*. so many <buried><variables><everywhere>...

Let me know if you get stuck.


Shagwana(Posted 2012) [#3]
Maybe I can lend a bit of a hand :)


Thats a chunk of code from my exporter... shows how to get uv data (via channels), vertex color data (via channels). Also how to get polygon data and a bunch of other stuff.

The above code wont run (as is) but you should be able to take something away from it!

I will say as well that this is ment to work with 2.6 - BMesh


bruZard(Posted 2012) [#4]
thank you Shagwana, very interesting!


bruZard(Posted 2012) [#5]
*.b3d feels like 1990 ...


Shagwana(Posted 2012) [#6]
Through my poking about with blender I thought I best warn you my above code is NOT exactly spot on.


Some things to worry about...

Its possible for a blender mesh to exist where only some polygons have used a uv texture and others that have not.

It looks like to me that if vertex painting has taken place, then all the verts of the mesh have vertex colouring.

Its also possible that a single mesh has different textures for different polygons.

You can get most Blender meshes into this state by joining two meshes together.


bruZard(Posted 2012) [#7]
i give up. The B3D format is absolutely crap ...


AdamRedwoods(Posted 2012) [#8]
Is there anything specific you are stuck on?
I don't find B3D as bad as Collada.


bruZard(Posted 2012) [#9]
sorry, i was very busy the last days. The problem is the 90th century format. Why should i know the size of a chunk? It's very complicated to calculate this in Blender-Python. Why the possibility to define that a vertex has normals or not? A good format takes a 0 for empty values and has end tags for chunks.
In Blender i must iterate the complete scene a dozend of times only for calculate the sizes of the chunks. More than 1000 lines of code and i've nothing written in a file, only parsed the scene, again and again and again ....
A job for someone who has killed his grandmother.


AdamRedwoods(Posted 2012) [#10]
I wish the 3D community would create a better format than collada and FBX for animation. Surprised no one has grabbed the reins.

I would recommend writing a pseudo-JSON/B3D format and export blender to that.

So each chunk would be similar to
"bb3d" {
  "version" : 1,
  "node" : {
    "name": "root_node",
    "position": [0,0,0],
    "scale": [0,0,0],
    "quaternion": [1,0,0,0],
    "mesh" : {
      "brush": -1,
      "vrts": {  
        [x,y,z...]
      },
      "tris" : {
        "brush" : -1, 
        [v0,v1,v2...]
      }
    }
  }
}


should make things easier. then a simple importer could be made for minib3d, since it mimics the original B3D chunk file.


bruZard(Posted 2012) [#11]
Very good idea ... currently i get that from Blender:
b3d{
	'version': 1, 
	'brus': [
	{'name': 'Material', 
	'fx': 0, 
	'n_texs': 1, 
	'shininess': 0.5, 
	'diffuse': [0.800000011920929, 0.800000011920929, 0.800000011920929, 1.0], 
	'tex_id': [1], 
	'specular': [1.0, 1.0, 1.0, 1.0], 
	'blend': 1, 
	'id': 1}
	], 
	'texs': [{'scale': [1.0, 1.0], 
	'name': 'woodfine.jpg', 
	'file': 'woodfine.jpg', 
	'position': [0.0, 0.0], 
	'rotation': [0.0, 0.0], 
	'id': 1}
	]
}

the final exporter will export something like that:
b3d{
	"version"	: 	1,
	"brus"		: 	[
						{
							// Brush Data
						},
						{
							// Brush Data
						}
					],
	"texs"		:	[
						{
							// Texture Data
						},
						{
							// Texture Data
						}
					]
	"nodes"		:	[
						{
							// a node
							"name"		: "Cube",
							"position"	: [0.0, 0.0, 0.0],
							"scale"		: [1.0, 1.0, 1.0],
							"quaternion": [1.0, 0.0, 0.0, 0.0],
							"mesh"		: {
											"brush"	: 1,
											"vrts"	: [...]
											// etc...
										  }
							// etc...
						},
						{
							// another node
						}
					]
}



bruZard(Posted 2012) [#12]
BlendFX() Modes:
FX_NOTHING	= 0
FX_FULLBRIGHT	= 1    # implemented
FX_VERTEXCOLORS	= 2    # implemented
FX_FLATSHADED	= 4    # implemented
FX_DISABLEFOG	= 8    # impossible
FX_DISABLEBC	= 16   # impossible



bruZard(Posted 2012) [#13]
JSON for *.b3d at now:


at this time the exporter produce the following JSON with the standard Blender cube:



bruZard(Posted 2012) [#14]
Question: can a B3D file have more than one root node? Or: how can i save more than one mesh into a B3D file?


AdamRedwoods(Posted 2012) [#15]

Question: can a B3D file have more than one root node? Or: how can i save more than one mesh into a B3D file?

I think it only has one root node, but you can have many nodes. You can have other meshes attached to the root node.

The other way to do it is to export all vertices and then define different surfaces with tris and brush ids.


bruZard(Posted 2012) [#16]

{'NODE': {'type': 'MESH', 'scale': {'y': 1.0, 'x': 1.0, 'z': 1.0}, 'position': {'y': -0.29439577460289, 'x': 0.4664255380630493, 'z': 0.3739100992679596}, 'rotation': {'y': 0.0, 'x': 0.0, 'z': 0.0}, 'MESH': {'VRTS': [{'tex_coord_sets': 0, 'flags': 3, 'position': {'y': 1.0, 'x': -0.9999999403953552, 'z': 1.0}, 'tex_coords': [], 'normals': {'y': 0.5773491859436035, 'x': -0.5773491859436035, 'z': 0.5773491859436035}, 'color': {'red': 0.0, 'green': 0.0, 'alpha': 1.0, 'blue': 0.0}, 'size': 52, 'tex_coord_setsize': 0}, {'tex_coord_sets': 0, 'flags': 3, 'position': {'y': 1.0, 'x': -0.9999999403953552, 'z': 1.0}, 'tex_coords': [], 'normals': {'y': 0.5773491859436035, 'x': -0.5773491859436035, 'z': 0.5773491859436035}, 'color': {'red': 0.0, 'green': 0.0, 'alpha': 1.0, 'blue': 0.0}, 'size': 52, 'tex_coord_setsize': 0}, {'tex_coord_sets': 0, 'flags': 3, 'position': {'y': 1.0, 'x': -0.9999999403953552, 'z': 1.0}, 'tex_coords': [], 'normals': {'y': 0.5773491859436035, 'x': -0.5773491859436035, 'z': 0.5773491859436035}, 'color': {'red': 0.0, 'green': 0.0, 'alpha': 1.0, 'blue': 0.0}, 'size': 52, 'tex_coord_setsize': 0}, {'tex_coord_sets': 0, 'flags': 3, 'position': {'y': 1.0, 'x': -0.9999999403953552, 'z': 1.0}, 'tex_coords': [], 'normals': {'y': 0.5773491859436035, 'x': -0.5773491859436035, 'z': 0.5773491859436035}, 'color': {'red': 0.0, 'green': 0.0, 'alpha': 1.0, 'blue': 0.0}, 'size': 52, 'tex_coord_setsize': 0}, {'tex_coord_sets': 0, 'flags': 3, 'position': {'y': 1.0, 'x': -0.9999999403953552, 'z': 1.0}, 'tex_coords': [], 'normals': {'y': 0.5773491859436035, 'x': -0.5773491859436035, 'z': 0.5773491859436035}, 'color': {'red': 0.0, 'green': 0.0, 'alpha': 1.0, 'blue': 0.0}, 'size': 52, 'tex_coord_setsize': 0}, {'tex_coord_sets': 0, 'flags': 3, 'position': {'y': 1.0, 'x': -0.9999999403953552, 'z': 1.0}, 'tex_coords': [], 'normals': {'y': 0.5773491859436035, 'x': -0.5773491859436035, 'z': 0.5773491859436035}, 'color': {'red': 0.0, 'green': 0.0, 'alpha': 1.0, 'blue': 0.0}, 'size': 52, 'tex_coord_setsize': 0}, {'tex_coord_sets': 0, 'flags': 3, 'position': {'y': 1.0, 'x': -0.9999999403953552, 'z': 1.0}, 'tex_coords': [], 'normals': {'y': 0.5773491859436035, 'x': -0.5773491859436035, 'z': 0.5773491859436035}, 'color': {'red': 0.0, 'green': 0.0, 'alpha': 1.0, 'blue': 0.0}, 'size': 52, 'tex_coord_setsize': 0}, {'tex_coord_sets': 0, 'flags': 3, 'position': {'y': 1.0, 'x': -0.9999999403953552, 'z': 1.0}, 'tex_coords': [], 'normals': {'y': 0.5773491859436035, 'x': -0.5773491859436035, 'z': 0.5773491859436035}, 'color': {'red': 0.0, 'green': 0.0, 'alpha': 1.0, 'blue': 0.0}, 'size': 52, 'tex_coord_setsize': 0}], 'brush_id': -1, 'TRIS': [{'brush_id': -1, 'vertex_id': [0, 1, 2]}, {'brush_id': -1, 'vertex_id': [4, 7, 6]}, {'brush_id': -1, 'vertex_id': [0, 4, 5]}, {'brush_id': -1, 'vertex_id': [1, 5, 6]}, {'brush_id': -1, 'vertex_id': [2, 6, 7]}, {'brush_id': -1, 'vertex_id': [4, 0, 3]}], 'size': 0}, 'size': 0, 'name': 'Cube'}, 'version': 1, 'size': 0, 'header': 'BB3D'}



This is the current export for a cube (no materials). Next step: Vertexcolors, Vertexgroups, UV Layer, textures etc.


AdamRedwoods(Posted 2013) [#17]
my suggestion is to perhaps keep vertex coordinates, tri indexes, normals, etc, in bunches, otherwise you'll get massive files.


dragon(Posted 2013) [#18]
check this:

http://supertuxkart.svn.sourceforge.net/viewvc/supertuxkart/media/trunk/blender_25/

i do not tested it


bruZard(Posted 2013) [#19]
OMG ... it works ... thx dragon!


dragon(Posted 2013) [#20]
this is the latest version of b3d exporter
used and updated by supertuxkart
i searched half internet for it...


but i would love to have b3d binary import for minib3d
i hope you can help here?


bruZard(Posted 2013) [#21]
yes, with this base i can add base64 support for miniB3D.


dragon(Posted 2013) [#22]
miniB3d have already base64 support...
but i mean binary without base64