From a3d8d01b0e24fc8ba3d31498b16f581c9968b80a Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Mon, 28 Jul 2014 17:10:10 +0200 Subject: Fix T40949: Importing STL file fails and produces a python error - Mac. Replace usage of mmap by mere open(). It's not significatively slower (perhaps 2% or 3%), and seems more reliable. Based on patch by paddy10663 (Patrick Taylor), with own edits and optimizations. --- io_mesh_stl/stl_utils.py | 77 ++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 41 deletions(-) (limited to 'io_mesh_stl') diff --git a/io_mesh_stl/stl_utils.py b/io_mesh_stl/stl_utils.py index 67478ba8..d1dea5e9 100644 --- a/io_mesh_stl/stl_utils.py +++ b/io_mesh_stl/stl_utils.py @@ -26,35 +26,14 @@ Used as a blender script, it load all the stl files in the scene: blender --python stl_utils.py -- file1.stl file2.stl file3.stl ... """ +import os import struct -import mmap import contextlib import itertools from mathutils.geometry import normal # TODO: endien - -@contextlib.contextmanager -def mmap_file(filepath): - """ - Context manager over the data of an mmap'ed file (Read ONLY). - - - Example: - - with mmap_file(filepath) as m: - m.read() - print m[10:50] - """ - with open(filepath, 'rb') as file: - # check http://bugs.python.org/issue8046 to have mmap context - # manager fixed in python - mem_map = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) - yield mem_map - mem_map.close() - - class ListDict(dict): """ Set struct with order. @@ -100,9 +79,16 @@ def _is_ascii_file(data): represents a binary file. It can be a (very *RARE* in real life, but can easily be forged) ascii file. """ - size = struct.unpack_from('>> # print the coordinate of the triangle n >>> print(pts[i] for i in tris[n]) """ + import time + start_time = time.process_time() tris, pts = [], ListDict() - with mmap_file(filepath) as data: + with open(filepath, 'rb') as data: # check for ascii or binary gen = _ascii_read if _is_ascii_file(data) else _binary_read @@ -258,6 +251,8 @@ def read_stl(filepath): # first equal point inserted. tris.append([pts.add(p) for p in pt]) + print('Import finished in %.4f sec.' % (time.process_time() - start_time)) + return tris, pts.list -- cgit v1.2.3