[Closed] padding unused bytes in maxscript
I’m actually trying to write a b3d exporter in maxscript, as their is no more support for this format in 3dsmax from max 2008. And when writing my binary file, I got a problem when comparing an old file and a file written with my own exporter.
My basic binary file exportation code.
fname = getSaveFileName caption:"Export to file:" filename:"test.b3d"--Dialog to chose where we want to save the file
deletefile fname --Making sure we don't write in the same file.
f = fopen fname "wb"--Identify the file to open to write in
WriteString f "BB3D" --Writing the title BB3D
Writelong f 1 --Writing the Version number
WriteString f "NODE" --Writing the title NODE
fclose f
My File Exporter result
BB3D.....NODE
Old python and onigirl B3D pipeline Exporter file result
BB3DÈ.......NODE
This is the code from blender 2.45 gandalf exporter to write string. I looked at the python doc to see what the struct.pack was doing and saw that it was padding the written binary. and also in this case writing in little-endian wich seem to be the way how max write to… Link to python doc
def write_string(value):
binary_format = "<%ds"%(len(value)+1)
return struct.pack(binary_format,value)
Is their some way to pad the missing null value into my binary file? I tried to add value by hand by adding some value in my string so instead of writing "BB3D", I was writing "BB3D000" or using this right after my string to write 3 extras null.
fseek f 3 #seek_cur
But it wasn’t working.
From the python docs you point to it looks like the “%d” is actually a double precision value (8 bytes).
If you really do need to write doubles out you need to use WriteFloatAsDouble (not just WriteFloat)
…but don’t think that’s what you are after.
If this is the format you are trying to follow:
http://www.blitzbasic.com/sdkspecs/sdkspecs/b3dfile_specs.txt
then might want to start with a simple chunk writer and build up from there:
struct simplechunker
(
f,
ChunkMarkers = #(),
fn WriteFourCC s =
(
-- Write a 4 character string as a fourcc code (4 bytes, no terminating null)
WriteByte f (bit.charAsInt s[1])
WriteByte f (bit.charAsInt s[2])
WriteByte f (bit.charAsInt s[3])
WriteByte f (bit.charAsInt s[4])
),
fn StartChunk chunkname = (
WriteFourCC chunkname
append ChunkMarkers #(chunkname, ftell f)
WriteLong f 0 -- leave space for the size, endChunk will fill it in later
),
fn EndChunk chunkname = (
openChunk = ChunkMarkers[ChunkMarkers.count]
if openChunk[1] != chunkname do
(
throw ("Endchunk called with " +chunkname+ "! Expected name is " + openChunk[1])
)
-- Calculate the size of the chunk we are closing
currPos = ftell f
markPos = openChunk[2]
chunkSize = currPos - (markPos+4)
--format "Set size of chunk % to 0x% @ offset 0x%
" openChunk[1] (bit.intAsHex chunkSize) (bit.intAsHex markPos)
fseek f markPos #seek_set -- Update the chunksize
WriteLong f chunkSize
fseek f currPos #seek_set -- Seek back to where we left off writing
deleteItem ChunkMarkers ChunkMarkers.count
)
)
fn WriteTestFile fname =
(
deletefile fname
f = fopen fname "wb"
chunker = SimpleChunker f
chunker.StartChunk "BB3D"
WriteLong f 1 -- The file version
chunker.StartChunk "NODE"
-- Fill in something
WriteString f "All your data are belong to here"
chunker.EndChunk "NODE"
chunker.StartChunk "NODE"
-- Fill in another thing
WriteString f "Another node goes here"
chunker.EndChunk "NODE"
chunker.EndChunk "BB3D"
if (chunker.ChunkMarkers.count != 0) do throw "You still have open chunks!"
fclose f
)
.biddle