Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binvox write does not work for python3 #3

pclausen opened this issue Feb 4, 2018 · 1 comment

binvox write does not work for python3 #3

pclausen opened this issue Feb 4, 2018 · 1 comment


Copy link

@pclausen pclausen commented Feb 4, 2018

Nice little tool; saved me some work but this bug took some time to figure out...

The write-method does not work for python3. The problem is handling of writing bytes in Python3 has changed.

I am not sure if I was doing something wrong but I was trying to push a fix but it did not work. I am very new to git so if you can help me push the change I would appreciate it. It is probably something simple I have forgotten. What i did:

git clone
# did my changes
git add
git commit -m "Issues with writting with Python3. Fixed by using import struct; struct.pack('B',byte_value)"
git push origin HEAD:master
remote: Permission to dimatura/binvox-rw-py.git denied to pclausen.
fatal: unable to access '': The requested URL returned error: 403

Thanks. Below the patch.

diff --git a/ b/
index 704fc1f..6151a1e 100644
--- a/
+++ b/
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 #  Copyright (C) 2012 Daniel Maturana
 #  This file is part of binvox-rw-py.
@@ -62,6 +63,7 @@ True
 import numpy as np
+import struct
 class Voxels(object):
     """ Holds a binvox model.
@@ -228,6 +230,16 @@ def sparse_to_dense(voxel_data, dims, dtype=np.bool):
     #TODO ensure this is right when dims are not all same
     #return x*(dims[1]*dims[2]) + z*dims[1] + y
+def bwrite(fp,s):
+    fp.write(s.encode())
+def write_pair(fp,state, ctr):
+    fp.write(struct.pack('B',state))
+    fp.write(struct.pack('B',ctr))
 def write(voxel_model, fp):
     """ Write binary binvox format.
@@ -244,11 +256,11 @@ def write(voxel_model, fp):
         dense_voxel_data =
-    fp.write('#binvox 1\n')
-    fp.write('dim '+' '.join(map(str, voxel_model.dims))+'\n')
-    fp.write('translate '+' '.join(map(str, voxel_model.translate))+'\n')
-    fp.write('scale '+str(voxel_model.scale)+'\n')
-    fp.write('data\n')
+    bwrite(fp,'#binvox 1\n')
+    bwrite(fp,'dim '+' '.join(map(str, voxel_model.dims))+'\n')
+    bwrite(fp,'translate '+' '.join(map(str, voxel_model.translate))+'\n')
+    bwrite(fp,'scale '+str(voxel_model.scale)+'\n')
+    bwrite(fp,'data\n')
     if not voxel_model.axis_order in ('xzy', 'xyz'):
         raise ValueError('Unsupported voxel model axis order')
@@ -265,19 +277,16 @@ def write(voxel_model, fp):
             ctr += 1
             # if ctr hits max, dump
             if ctr==255:
-                fp.write(chr(state))
-                fp.write(chr(ctr))
+                write_pair(fp, state, ctr)
                 ctr = 0
             # if switch state, dump
-            fp.write(chr(state))
-            fp.write(chr(ctr))
+            write_pair(fp, state, ctr)
             state = c
             ctr = 1
     # flush out remainders
     if ctr > 0:
-        fp.write(chr(state))
-        fp.write(chr(ctr))
+        write_pair(fp, state, ctr)
 if __name__ == '__main__':
     import doctest

Copy link

@pclausen pclausen commented Feb 4, 2018

It was probably something with rights. I was able to fork this code and push the change, see

If you import the change, should I then delete my fork ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

1 participant