Read and/or write binary files in a script.
contessg
Posts: 55
Hello,
sorry to ask you another question because I don't find any solution to resolve my problem.
I would like to read and/or write binary files.
I thought I will find a way to create an array containing Float values like vertices (float3 values) and then write a binary file but I only found solutions to write text files.
This format takes a lot of space and generates huge files if I want to save an animation as a cache file.
My question is: Is there a way to export an array as a binary file.
I thank you for your replies.
Comments
Do you mean a compressed file? If so you want to use DzGZFile, not DzFile.
Hello Richard,
You are right, this method should allow me to reduce a lot the size of the file.
But I have a binary file created in another application and I want to read it without any transformations.
When I say Binary file, I really want to mean file containing data in Binary mode.
A vertex is defined by three single values. It is what I call a float3.
This element takes 12 bytes in memory.
I just want to read these vertices in binary mode.
Thank you for your help.
Gérald
Ah, in that case you'd use file.read( 12 ) to get a 12 member byte array, http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/object_index/bytearray .
Yes, I saw this feature but I don't know how to convert this array containing 12 bytes in a array containing 3 float values of 4 bytes.
Is there the ability to read specific values from a file: an integer32 (4 bytes), an integer64 (8 bytes), a single (4 bytes), a double (8 bytes)?
A DzVec3 is an object in DazScript. Is it possible to read a DzVec3 or better an array of DzVec3 from a file?
Thank you for your time.
Gérald
You'd need to know the number format used, then get bytes and combine them as dictated by the format
Excuse me, I am not sure to understand correctly.
I tried to read 12 bytes in a ByteArray and to use a transtypage using : DzVec3(ByteArray), but the result is not fine.
Do I use the correct method? I don't find another way to change the format.
You need to know how the output from the other application, generating the bytes, is formatted - how does it go from float to four bytes? You need to perform the reverse operation on the bytes from the file.
As far as I know, it's complicated, but something like that is what you should do.
You should also know if it is little endian or Big Endian, it is "int signed or unsigned or floating ", it is of 8,16 or 32 bits.
This is an sample of what has to be done, you should review, this sample may contain errors, (the order may be incorrect)..
First obvious error in the code is a complete lack of "var" keywords. As written, all variables (including those defined within the function) are polluting the global scope; without "var" there is no declaration, thus the variables are implicitly declared in the global scope. Second obvious error in the code is the lack of "new" operator to instantiate the ByteArray object.
@Richard Haseltine: I perfectly know the structure of the float value I want to read because it is an application I wrote. I am just surprised it is so complicated to read a single value, directly readable by any CPU, or GPU.
@CGI3DM: Thanks a lot for your example. It clearly shows it will be difficult to read or write a cache file directly in DazScript.
I admit that this is the first time I have to use a shift function to read an integer or a single value.
Thank you for your time.
@Cris Palomino: I agree with you concerning the explicit variables declaration, but I highly thanks CGI3DM for this very instructive example.
As you are in the Daz staff, do you think it will be possible to read basic data as integer or single values directly in the future versions of DazScript?
In that case, you have full control, just add another output format, like JSON or XML and you'll be on the other side. That's the beaty of open-standard file formats.
JSON would be the obvious choice, you can then load the whole file and parse it to a single data object in a couple of lines, from which you can extract your values as with a local object.
Thank you jag11 and Richard Haseltine.
I never used JSON. Visibly, I have a lot to learn. I hope this will resolve my issue.
Hello,
Richard, I followed your first method by using some GZFiles.
I am really surprised but the size of text files compressed in GZip and the size of the file containing the same values in binary mode are the same.
For me, it is an opportunity. Obviously I have to rewrite the cache file using another method, but the result works fine.
I use the "Timechanging" method to update the actor in the scene as described in another post. This method is not very fast, but my final goal is to render an animation and some seconds more is not really important.
Thank you for your help.
Gérald