trimesh
trimesh copied to clipboard
Bug with Shapely 2.0 - utils.is_sequence fails for multipart geometry
I don't have a simple reproducible case for this, but when using Shapely 2.0 the trimesh/util.py is_sequence function fails. It looks for iterator attributes, which multipart geometries no longer have as part of Shapely 2.0.
In m case I tried to use the trimesh.path.polygons.projected() function with inputs known to work with Shapely 1.8. With Shapely 2.0, the call raised a AttributeError with the message "'MultiPolygon' object has no attribute 'exterior'"
The workaround I used is to patch that particular function in my top level init.py:
# Trimesh uses is_sequence() to check if a geometry is a multipart geometry in
# trimesh.path.polygons.edges_to_polygons(), but it doesn't actually do the check
# properly, so we are patching it
original_is_sequence = trimesh.util.is_sequence
def is_sequence(obj) -> bool:
if isinstance(obj, geometry.base.BaseMultipartGeometry):
return True
return original_is_sequence(obj)
trimesh.util.is_sequence = is_sequence
Ah yeah I also see a ton of depreciation warnings with my applications for Shapely 2.0 from trimesh. Any idea when Shapely 2.0 releases?
I honestly don't know, but the new features in Shapely 2.0 like parallelizing and geometry precision control are important to my project so I'm using the pre-release.
Nice! I just briefly tried and failed to install Shapely2 to play with this haha, in the mean time I'll try to clean up the DepreciationWarnings when I see them.
PR's welcome for any S2 fixes! I think given their new API it's possibly to support both pre-1.8 and 2+?
I'm working on deprecation warnings, and am getting this one:
ShapelyDeprecationWarning: The array interface is deprecated and will no longer work in Shapely 2.0. Convert the '.coords' to a numpy array instead.
pts, tris = trimesh.creation.triangulate_polygon(p, engine='earcut')
IIUC, triangulate_polygon isusing the numpy array interface to Shapely POlygons -- so there's nothing I can do in my code.
But I suspect cleaning up these deprecation warning with code that still works in shapely <2 will get us much of the way there.
-CHB
I think I got all of these, and trimesh tests are passing with pip install shapely==2.0b2