blender's file format

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

Post Reply
Sutabi
Posts: 53
Joined: Fri Oct 18, 2002 1:35 am
Location: Oceanside, California
Contact:

blender's file format

Post by Sutabi »

I am looking in readfile.c and writefile.c and I am just lost...

I am currently using python and struct lib to read the binary data, but I am having troble connect data types.

Is all the data 'signed char' ? I went ahead and split the file up from the text DATA and unpacked as 'signed char' to get a list of numbers. I have no problem reading the header ^_-, but to find where the mesh data is defined seems impossible even though Mesh tools and other mesh text is listed in there...

z3r0_d
Posts: 289
Joined: Wed Oct 16, 2002 2:38 am
Contact:

Post by z3r0_d »


Sutabi
Posts: 53
Joined: Fri Oct 18, 2002 1:35 am
Location: Oceanside, California
Contact:

Post by Sutabi »

From what I've been reading the in read/write files is that All the UI structures are @ top, then it lists all the structures after UI so then after the list the binary is sorted in the same order as the list.

Write(file,encode type,"Header Name", Size?, data?)

read:
If each header is a multiple of 8 then a header of "face[4]" is 8*4 which is 32 bytes, but what I dont know is where face[4]'s structure beings/ends and the type of it.

Code: Select all

class bHead:
    def __init__(self):
        code       = None
        sizeOf     = None
        cType      = None

joeedh
Posts: 31
Joined: Wed Oct 16, 2002 10:30 pm
Contact:

Re: blender's file format

Post by joeedh »

Sutabi wrote:I am looking in readfile.c and writefile.c and I am just lost...

I am currently using python and struct lib to read the binary data, but I am having troble connect data types.

Is all the data 'signed char' ? I went ahead and split the file up from the text DATA and unpacked as 'signed char' to get a list of numbers. I have no problem reading the header ^_-, but to find where the mesh data is defined seems impossible even though Mesh tools and other mesh text is listed in there...
Blender has compiler flags set to force everything to be unsigned char, this of course doesn't affect storage sizes.

besides that, I really don't know much about the file reading code, other then when you load meshes (or hemeshs) you use a function of the sort libreadaddr_new (there are several variants), that takes an old pointer as an argument and returns the new pointer to the newly loaded data.

hope that helps.

joeedh

eeshlo
Posts: 73
Joined: Wed Nov 06, 2002 10:02 pm

Post by eeshlo »

While old, this might be useful to you maybe. Three years ago I created a python blendfile reader for the lightflow export script to get materials/textures/nurbs/metaballs info which was not possible using the python api at the time. You can find the script distribution here (LFexportGUI):
http://www.zoo-logique.org/3D.Blender/i ... ython.php3
the file you want to look at is BFREAD.py in the modules/script directory.
I also still have a distribution with some documentation and examples on how to use it, but don't have it uploaded anywhere, so mail me or something if you want it.

I made several attempts at improving it, coding it in C++ as well as another version in python (one pass). The idea being that yafray could use blendfiles directly, but that is of no use anymore, so I never quite finished them.

Sutabi
Posts: 53
Joined: Fri Oct 18, 2002 1:35 am
Location: Oceanside, California
Contact:

Post by Sutabi »

um... I dont know your email, but that would be great! email:

owner @ sutabi.tk

Sutabi
Posts: 53
Joined: Fri Oct 18, 2002 1:35 am
Location: Oceanside, California
Contact:

Post by Sutabi »

I really dont know where to ask this, but the the Face's vertex index in .blends are not be the same as if you where making an export in Blender Python API. I.E the first face may be the 3rd face in the list.

Since that is true and the order is switched, then that also should mean the vertices are switched as well, which is why I can getting faces drawn incorrectly in opengl, when reading from .blends.

Does anyone know how blender interperetes this? Also it you try to export blender's face index though the API in opengl it will draw wrong because the drawing order is not correct, so some faces will draw forward and some backward. even is you change GL_CCW to CL_CW or bother with GL_FRONT or GL_BACK.

eeshlo
Posts: 73
Joined: Wed Nov 06, 2002 10:02 pm

Post by eeshlo »

If you are still using my blendfile reader, this has to do with almost all lists being out of order, you can't use the indices directly. You have to sort the lists by it's 'address' parameter first (not a blendfile parameter, added by the script).
I thought I had an example .py of that in the .zip I sent you, but apparently not. You can reference the LFexport script for that (see EXPORT_LF_NURB) if you want.

eeshlo
Posts: 73
Joined: Wed Nov 06, 2002 10:02 pm

Post by eeshlo »

Never mind, to save you the trouble, here is an example of how to import a mesh back into blender:

Code: Select all

from BFREAD import *
from Blender import Object, NMesh, Redraw

filename = BLENDFILE_NAME_HERE
meshname = MESHNAME_FROM_FILE_HERE

def adr_cmp(a, b):
	x = '0x'+a.address[2:]
	y = '0x'+b.address[2:]
	exec('t = %s-%s' % (x,y))
	return t

bf = BFReader(filename)
bf.getData(['Mesh', 'MVert', 'MFace'])

FBlenderM = bf.convert2Python()

mesh = FBlenderM.getMesh(meshname)

# Sort the face and vertex lists by address.
# This should really be done by the blendfile reader itself.
# Another weakness is that if the mesh only has a single face,
# it is just that face, not in a list, so have to check for that here too
if type(mesh.mface).__name__=='list': mesh.mface.sort(adr_cmp)
mesh.mvert.sort(adr_cmp)

# as an example import back in Blender again
newmesh = NMesh.GetRaw()

# vertices
for v in mesh.mvert:
	newmesh.verts.append(NMesh.Vert(v.co[0], v.co[1], v.co[2]))

# faces
for f in mesh.mface:
	# face is at least an edge (read 0 index for f3/f4 as NULL)
	vidx_list = [f.v1, f.v2]
	# only add the other vertex indices if not 0
	if f.v3: vidx_list.append(f.v3)
	if f.v4: vidx_list.append(f.v4)
	newface = NMesh.Face()
	for vidx in vidx_list:
		newface.v.append(newmesh.verts[vidx])
	newmesh.faces.append(newface)

# back to Blender, using the actual file mesh name (skipping the first two id chars 'ME')
NMesh.PutRaw(newmesh, mesh.id.name[2:])
Redraw()

fill in the required 'filename' and 'meshname' (from the file) yourself.

Post Reply