Jump to content
  • Advertisement
Sign in to follow this  
V-man

Python, write binary

This topic is 3847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

According to the bottom part of http://www.ibm.com/developerworks/library/os-python8/index.html to write binary files
>>> myfile = open("testit.txt", "wb")
>>> for c in range(50, 70):
...     myfile.write(chr(c))
... 
>>> myfile.close()
>>> myfile = open("testit.txt")
>>> myfile.read()
'23456789:;<=>?@ABCDE'
>>> myfile.close()

So c is a character (8 bit). The code works when I tested. Does anyone know the trick for writing 16 bit integers, 32 bit integer, 32 bit float, 64 bit integer?

Share this post


Link to post
Share on other sites
Advertisement
Then there is hope!
I am trying to write an export script for Blender for my own file format.
I wish they had chosen C or C++ for scripting. It would make my life easier.

Share this post


Link to post
Share on other sites
Quote:
Original post by V-man
I wish they had chosen C or C++ for scripting. It would make my life easier.

At the expense of making life much harder for all those non-programmer artist types who are the majority userbase for Blender. Brilliant!

Mild sarcasm aside, writing specific C-style data types in Python is simply a matter of using the struct module:


>>> import struct
>>> struct.pack("hhifq", 4, 4196, 1073741824, 4169.248, 2**50)
'\x04\x00d\x10\x00\x00\x00@\xfcI\x82E\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> s = struct.pack("hhifq", 4, 4196, 1073741824, 4169.248, 2**50)
>>> struct.unpack("hhifq", s)
(4, 4196, 1073741824, 4169.248046875, 1125899906842624L)

Share this post


Link to post
Share on other sites
Quote:
At the expense of making life much harder for all those non-programmer artist types who are the majority userbase for Blender. Brilliant!


I don't know. To me, each language is as complex as the rest. It is always the same concept but the syntax changes.

Thanks to all the replies. I am starting to get off the ground. Now if I can figure out what is the most straigtforward way to get index list + vertices + textures from Blender.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!