We can't really be sure what exactly the header is used for in your case, but similar to my other examples, it can be used to store information about the rest of the file. Maybe it is a magic id that can be checked to ensure the file content is exactly what you expect it to be. You could design a file to have a specific amount of the start of the file be an ascii or binary header, and then the body picks up at a certain point.
On Mon, Mar 2, 2015 at 9:44 PM Benjam901 <[email protected]> wrote: > Hey guys, > > Thanks for the solid info! It has cleared up a lot for me and it all makes > a bit more sense now. I am still a little fuzzy about what the header does? > The header for our files is a 4 byte string, what does this indicate or is > it for the engine/computer so it is able to decode it properly? > > Cheers, > > Ben > > > On Sunday, 1 March 2015 16:58:50 UTC+1, Ben Hearn 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? >> >> 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/10fcc246-f70a-495b-9c1c-b837bd6ecbc6%40googlegroups.com > <https://groups.google.com/d/msgid/python_inside_maya/10fcc246-f70a-495b-9c1c-b837bd6ecbc6%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/CAPGFgA1x99HgzJ7QuShaf4iWnik9bHYN-mfE78xQv7raDYnZdw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
