OBJ importer error (python 3.7)

A forum dedicated to the Draft, Arch and BIM workbenches development.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
olof
Posts: 11
Joined: Thu Sep 04, 2014 7:40 am

OBJ importer error (python 3.7)

Post by olof »

Hi,

When I try to export an OBJ file (such as when uploading to Sketchfab) I get an error like:

Code: Select all

Traceback (most recent call last):
  File "~/.FreeCAD/Mod/WebTools/Sketchfab.py", line 220, in upload
    pack = self.saveFile()
  File "~/.FreeCAD/Mod/WebTools/Sketchfab.py", line 138, in saveFile
    importOBJ.export(objects,filename+".obj")
  File "~/miniconda3/envs/freecad_py37/Mod/Arch/importOBJ.py", line 199, in export
    outfile.write("# FreeCAD v" + ver[0] + "." + ver[1] + " build" + ver[2] + " Arch module\n")
TypeError: a bytes-like object is required, not 'str'
At the moment the file is being opened for writing in binary mode. Given OBJ files (as I understand it) are text files is this needed? If I change it to open as a text file it works as expected.

I seem to have an old account for the issue tracker associated with my email address but can't find/remember my username to allow me to reset my password...

OS: elementary OS 5.0 Juno
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.14555 (Git shallow)
Build type: Release
Branch: master
Hash: 51b953e2c6edd7f76b1d8faa584424cfb6367d45
Python version: 3.7.1
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedKingdom (en_GB)
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: OBJ importer error (python 3.7)

Post by vocx »

olof wrote: Tue Apr 02, 2019 12:21 pm Hi,

When I try to export an OBJ file (such as when uploading to Sketchfab) I get an error like:

Code: Select all

TypeError: a bytes-like object is required, not 'str'
At the moment the file is being opened for writing in binary mode. Given OBJ files (as I understand it) are text files is this needed? If I change it to open as a text file it works as expected.
The error you get sounds like it could be an incompatibility between Python 2 and Python 3 (http://python3porting.com/problems.html). I think Python 3 handles all strings as Unicode, but Python 2 used to consider them byte arrays (each character a byte). Since FreeCAD was developed with Python 2, there may still be some issues if you use Python 3. As far as I know, the developers are still working on ironing out all issues with Python 3, so that FreeCAD 0.19 (next release cycle) be fully compatible with Python 3. Porting to python3
Version: 0.18.14555 (Git shallow)
Did you compile your own FreeCAD? I'm using a pre-packaged FreeCAD for Ubuntu, compiled against Python 2. Did you try with the latest, stable 0.18? I'm using Version: 0.18.16093 (Git), and it seems to work. Your version is several thousands commits behind.

I also suggest you to try a simple export, without using the Sketchfab stuff.

Code: Select all

import Arch
import importOBJ

FreeCAD.ActiveDocument.addObject("Part::Box", "Box")
b = FreeCAD.ActiveDocument.ActiveObject
b.Label = "Cube"
b.Length = 1000
b.Width = 300
b.Height = 200
eq = Arch.makeEquipment(b)

importOBJ.export([eq], "/home/olof/model.obj")
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Post Reply