From 0e863fc2473c75d01917ff2a61602b11db66aedd Mon Sep 17 00:00:00 2001 From: Nicolas Braem Date: Thu, 18 Oct 2018 12:00:58 +0200 Subject: [PATCH 1/2] fix for unicode filenames Fix from here: https://stackoverflow.com/questions/27366479/python-3-os-walk-file-paths-unicodeencodeerror-utf-8-codec-cant-encode-s --- openctm/io.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/openctm/io.py b/openctm/io.py index f693392..2c95b6a 100644 --- a/openctm/io.py +++ b/openctm/io.py @@ -19,7 +19,7 @@ def __eq__(self, other): def import_mesh(_filename): ctm_context = ctmNewContext(CTM_IMPORT) try: - ctmLoad(ctm_context, str(_filename).encode('utf-8')) + ctmLoad(ctm_context, _encode(_filename)) err = ctmGetError(ctm_context) if err != CTM_NONE: raise IOError("Error loading file: %s" % str(ctmErrorString(err))) @@ -55,7 +55,7 @@ def import_mesh(_filename): def export_mesh(_ctm, _filename): ctm_context = ctmNewContext(CTM_EXPORT) - if not _filename.lower().endswith('.ctm'): + if not str(_filename).lower().endswith('.ctm'): _filename += '.ctm' try: @@ -81,6 +81,21 @@ def export_mesh(_ctm, _filename): p_normals = None ctmDefineMesh(ctm_context, p_vertices, CTMuint(vertex_count), p_faces, CTMuint(face_count), p_normals) - ctmSave(ctm_context, ctypes.c_char_p(_filename.encode('utf-8'))) + ctmSave(ctm_context, ctypes.c_char_p(_encode(_filename))) finally: ctmFreeContext(ctm_context) + +def _encode(_filename): + try: + return str(_filename).encode("utf-8") + except UnicodeEncodeError: + pass + + try: + # works fine for pathlib.Path + return bytes(_filename) + except TypeError: + pass + + return str(_filename).encode("utf-8", "surrogateescape") + From fc5cbbd4207b51cc405667766577a50cb2e9f78c Mon Sep 17 00:00:00 2001 From: Rafael Hautekiet Date: Thu, 18 Oct 2018 14:54:37 +0200 Subject: [PATCH 2/2] version bump --- openctm/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openctm/version.py b/openctm/version.py index c1a7902..d521168 100644 --- a/openctm/version.py +++ b/openctm/version.py @@ -1 +1 @@ -__version__ = '1.0.9' \ No newline at end of file +__version__ = '1.0.10'