On Mon, 2 Mar 2015 4:58 AM Ben Hearn <[email protected]> wrote:

Hello all,

I am currently in the process of writing a tool for work that requires the
need to read our in house.model format which is a binary file.

Here's the thing, the guy who wrote the tools that I inherited never
finished them and never commented them so I have been hacking at the old
code for a few months now which has been great fun and a brilliant learning
process. He left an old file that seems like it reads a binary file which
is exactly what I need.

I am however confused over something involved reading the data in a binary
file, so here goes.

To get the information from the binary file it would appear that certain
functions need to be called in a certain order. The only thing is all they
essentially do is read the file in byte chunks. If I comment any of the
functions out and run the code the data is all wrong.

So my question is, when you are reading binary files (or any files) every
time you perform a read function or a struct.unpack does the position in
the file that you read from change? Or is it something simpler like if you
have written your binary file in a certain way, you HAVE to read it in
exactly the same way?

 The file handle would advance with each read, regardless if it is a binary
or a plaint text file. It is just how the nature of the file operations
work. The difference with a binary file is that of you seek to a completely
random position, you would most likely not know how much to read to get
anything useful. A binary file follows a protocol defined by its format.
You start at a known point and read an expected amount. That may return a
known value that indicates what to read next, or the protocol may require
that you always just read N different amounts, in order, in a loop, until
the file is done.
Something like an int32 is going to be a specific number of bytes in the
file. So if you know you should be reading an int32 at this point, you have
to both be at the starting position of that data, and read the 4 bytes.
Then you have your valid data and have advanced to the start of something
else.
Some binary formats could use a protocol where you first read some kind of
descriptor string which might say:

   100,s,4,i,4,i,50,s,30

and you would then know that you next should expect to read a 100 byte
string, 2 4 byte ints, and a 50 byte string. And that the next descriptor
will be a 30 byte string describing possible a different amount of data.

Or maybe you have a header at the beginning that indexes a bunch of byte
offsets to various components of the file format.

There are a number of different ways it could work, but in your example it
does seem like it is just a pattern of assumed reads that have to be
consistently read over and over.

I might be missing some crucial theory so any help would be much
appreciated :)

Here are the function calls and the loop that contains the reading of info:

def _read_uint32(file):

data = file.read(4)

data = struct.unpack('I', data)[0]

return data



def _read_string(file):

size = _read_uint32(file)

data = file.read(size)

return data



def _read_int32(file):

data = file.read(4)

data = struct.unpack('i', data)[0]

return data



def _read_matrix4(file):

data = file.read(8 * 16)

return data

And here is the function that I am using to loop through the file and
gather the data:

def unserialize(file):



materials = []



print "OPENING THIS FILE PATH: ", file



file = QtCore.QFile(file)



file.open(QtCore.QIODevice.ReadOnly)



buf = file.read(4)



total_size = _read_uint32(file)



start_offset = file.pos()



passes = 1



for i in range(passes):

file.seek(start_offset)

 while( (file.pos() - start_offset) < total_size):

type_id = _read_uint32(file)

chunk_size = _read_uint32(file)

 if type_id == 0 and i == 0:

print "ID: ", _read_int32(file)

print "NAME: ", _read_string(file)

print "TRANSFORM ", _read_matrix4(file)

print "PIVOT TRANSFORM ", _read_matrix4(file)

print "PARENT ID ", _read_int32(file)

print

elif type_id == MATERIAL and i == 0:

print _read_int32(file)

print _read_string(file)

print _read_int32(file)

else:

file.seek(file.pos() + chunk_size)

Cheers!

Ben

-- 
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/45b8fdf5-c7e4-4f86-8dbc-f575659cf585%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/45b8fdf5-c7e4-4f86-8dbc-f575659cf585%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2oJjaQois0swfV59E8K9es6mAYNqeeoDdzr4qaM3m2Sw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to