mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
3.02k stars 583 forks source link

Make boolean engine selection more flexible #2309

Closed onitake closed 3 weeks ago

onitake commented 3 weeks ago

Closes: #2306

As announced, here's a patch that refactors the boolean engine autodetection.

I had to refactor a few things to make the two engines behave the same way and avoid circular imports.

reduce_cascade is now in util.py. I assumed that this is needed in other places, so I didn't move it into the new interfaces.manifold module.

interfaces.blender and interfaces.manifold are now dynamic imports. This avoids a circular import loop and allows autodetection at load time.

mikedh commented 3 weeks ago

Awesome thanks for the PR!! Few quick thoughts:

Is there a side effect of try: import manifold3d? Because we could also pick the default by using the first one that isn't an ExceptionWrapper:

# which backend boolean engines                                                            
_engines = {
    "manifold": boolean_manifold,
    "blender": interfaces.blender.boolean,
    "junk": ExceptionWrapper(ValueError("nah")),
}
_engines[None] = next(
    (v for v in _engines.values() if not isinstance(v, ExceptionWrapper)), None
)
onitake commented 3 weeks ago

Looks like it didn't quite work as well as I had hoped.

Thanks for the pointers, I'll take a look at has_module.

mikedh commented 3 weeks ago

No worries! I'll play around with it in my branch a bit. Thanks for the PR!!