meshcat-dev / meshcat-python

WebGL-based 3D visualizer for Python
MIT License
259 stars 62 forks source link

Added `from_stream` method to MeshGeometry subclasses. #44

Closed danieljfarrell closed 5 years ago

danieljfarrell commented 5 years ago

Changes

If mesh already exists in memory it is inefficient to save the mesh to a file and reload it. This can be avoided using streams.

f = io.StringIO('mesh ascii')  # or io.BytesIO('mesh binary')

The from_file methods open the file and then load,

@staticmethod
def from_file(fname):
    with open(fname, "r") as f:
        return MeshGeometry(f.read(), u"dae")

The from_steam methods skip the with open step and load the data in the stream directly.

@staticmethod
def from_stream(f):
    return MeshGeometry(data_from_stream(f), u"dae")

StringIO changes quite a bit between Python 2 and Python 3 and I noticed you are supporting Python 2 so I made it work in both cases. data_from_stream looks like this,

def data_from_stream(stream):
    if sys.version_info >= (3, 0):
        if type(stream) == BytesIO:
            data = stream.read().decode(encoding='utf-8')
        elif type(stream) == StringIO:
            data = stream.read()
        else:
            raise ValueError('Stream must be StringIO or BytesIO, not {}'.format(type(stream)))
    else:
        data = stream.read()
    return data

In Python 3 we first check if we are a string or a bytes stream and load accordingly. Python 2 only has str types so we can go ahead and just load that.

Testing

Tests have been added and pass in both Python 2 and Python 3. The .obj, .stl (ascii and binary) and the .dae files from tests/data are loaded into streams and visualised. Loading the text from disk into a stream kind of defeats the purpose of using streams but the round trip is a good test and shows that it works.

codecov-io commented 5 years ago

Codecov Report

Merging #44 into master will decrease coverage by 12.87%. The diff coverage is 94.54%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master      #44       +/-   ##
===========================================
- Coverage   87.35%   74.47%   -12.88%     
===========================================
  Files           8        8               
  Lines         609      670       +61     
===========================================
- Hits          532      499       -33     
- Misses         77      171       +94
Impacted Files Coverage Δ
src/meshcat/tests/test_drawing.py 72.51% <100%> (-23.6%) :arrow_down:
src/meshcat/geometry.py 62.91% <88.88%> (-24.04%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update d26423c...c99f362. Read the comment docs.

rdeits commented 5 years ago

This looks great, thanks! Just one minor question before I merge it.

danieljfarrell commented 5 years ago

Yes, is better practice. Will update the repo.

danieljfarrell commented 5 years ago

Happy with the new changes?

rdeits commented 5 years ago

Looks great, thank you!