Read and/or write binary files in a script.

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

  • Richard HaseltineRichard Haseltine Posts: 100,961

    Do you mean a compressed file? If so you want to use DzGZFile, not DzFile.

  • contessgcontessg Posts: 55

    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

  • contessgcontessg Posts: 55

    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

  • Richard HaseltineRichard Haseltine Posts: 100,961

    You'd need to know the number format used, then get bytes and combine them as dictated by the format

  • contessgcontessg Posts: 55

    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.

  • Richard HaseltineRichard Haseltine Posts: 100,961

    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.

  • CGI3DMCGI3DM Posts: 279
    edited May 2018
    bytesRead = ByteArray("EFGTAZDRAAAA")print(toInt32(bytesRead,0))print(toInt32(bytesRead,4))print(toInt32(bytesRead,8))function toInt32(bytesRead,offset){	BYTE1 = bytesRead.byteAt(0+offset)	BYTE1 <<= 0;// 00000000 00000000 00000000 00110100	BYTE2 = bytesRead.byteAt(1+offset)	BYTE2 <<= 8;// 00000000 00000000 00110100 00000000 	BYTE3 = bytesRead.byteAt(2+offset)	BYTE3 <<= 16;// 00000000 00110100 00000000 00000000 	BYTE4 = bytesRead.byteAt(3+offset)	BYTE4 <<= 24;// 00110100 00000000 00000000 00000000 		return BYTE4+BYTE3+BYTE2+BYTE1}

    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)..

    Post edited by CGI3DM on
  • Cris PalominoCris Palomino Posts: 11,381

    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.

  • contessgcontessg Posts: 55

    @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?

  • jag11jag11 Posts: 885
    contessg said:

    ... 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.

     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.

  • Richard HaseltineRichard Haseltine Posts: 100,961

    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.

  • contessgcontessg Posts: 55

    Thank you jag11 and Richard Haseltine.

    I never used JSON. Visibly, I have a lot to learn. I hope this will resolve my issue.

  • contessgcontessg Posts: 55

    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

Sign In or Register to comment.