writing b3d file

Blitz3D Forums/Blitz3D Programming/writing b3d file

jhocking(Posted 2010) [#1]
I'm exploring writing b3d files in order to potentially write an exporter. I can't tell what the first number in a BRUS chunk means.

The first value listed under BRUS is n_texs
http://www.blitzmax.com/sdkspecs/sdkspecs/b3dfile_specs.txt

There's no explanation next to the value like there is for every other value. That name sounds like the number of textures, but in the experiments writing b3d files I'm doing it seems like I need to set that value to 2 even if there aren't any textures in the brush.


Leon Drake(Posted 2010) [#2]
number of textures i believe


jhocking(Posted 2010) [#3]
Ok, but then why do I need to set it to 2?

For reference, here's Python code I wrote that spits out a cube. I'm loading this in my b3d viewer (which uses minib3d's mesh load command.)




Warner(Posted 2010) [#4]
For each texture, a texture index should be added at the end of the brush.
In your line:
f.write(struct.pack("<iiii", 1, 0, -1, -1))

-if I read it well- the two "-1"s at the end of the struct are texture indexes. So the brush uses 2 textures, but they are both '-1'.
If you want to create an 'empty' brush, use something like:
f.write(struct.pack("<ii", 49, 0))
f.write(write_string("material"))
f.write(struct.pack("<fffff", 0.8, 0.5, 0.5, 1.0, 0.0))
f.write(struct.pack("<ii", 1, 0))



jhocking(Posted 2010) [#5]
Thanks, that worked! That makes sense, it just wasn't explained in the specs.



Incidentally, technically you're also supposed to reduce the listed bytes in the chunk, but in practice that didn't matter for a reduction of just 8 bytes.

Still, the line
f.write(struct.pack("<ii", 49, 2))

should become
f.write(struct.pack("<ii", 41, 0))


and the line
f.write(struct.pack("<ii", 460, 1))

should become
f.write(struct.pack("<ii", 452, 1))




EDIT: oops actually I added the total bytes wrong in the first place, which just goes to show how in practice it doesn't really matter if you're off by an integer's worth of bytes. It should have added up to 464, so now it's reduced to 456

Last edited 2010


Warner(Posted 2010) [#6]
In the archives, there are a few .b3d exporters. I use them as a reference beside the official description. For instance this one:
http://www.blitzbasic.com/codearcs/codearcs.php?code=866
This one is pretty complete, it is written by Ricky Smith:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1794
It loads and saves animated .b3d's, which is pretty neat.


jhocking(Posted 2010) [#7]
Thanks again for the help. Ever since I started exploring reading binary files in Python I've been making so much progress in understanding the b3d file format. After all the years of saying I'm gonna write a Maya exporter, now I may actually do it.

Here's the code I wrote today to generate a b3d with a texture on it and some skeletal animation:



I figure that code could be useful to other people trying to figure out how to write exporters. Because all the values are hard-coded and it isn't linked to any specific modeling tool this code is a lot easier to sift through to figure out what's going on. I've looked at a lot of b3d export code (not those you linked specifically, but some other ones) and I never get anywhere with them because they are too complicated for me to jump into.


6(Posted 2010) [#8]
This will come in handy for me and no doubt save me a few hours when I come to trying to learn add b3d file support for Medieval4D. Cheers for the code :)