mikedh / trimesh

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

fix_inversion for none-watertight meshes #1419

Open FreakTheMighty opened 2 years ago

FreakTheMighty commented 2 years ago

I've attached a glb with a some normals that are inverted. I'd like to use fix_inversion but the docs note this only works with water tight meshes. image In blender, however, there is a function "Recalculate Outside" that sounds very similar to fix_inversion, but works as expected. image I'm curious if there is someway to add such support to Trimesh?

example.zip

FreakTheMighty commented 2 years ago

Based on the docs, it sounds like the normals are calculated relative to some bounding volume...

These tools will recalculate the normals of selected faces so that they point outside (respectively inside) the volume that the face belongs to. The volume does not need to be closed; inside and outside are determined by the angles with adjacent faces. This means that the face of interest must be adjacent to at least one non-coplanar other face. For example, with a Grid primitive, recalculating normals does not have a meaningful result.

mikedh commented 2 years ago

Hey, m.fix_normals might be what you're looking for:

In [10]: m.fix_normals?
Signature: m.fix_normals(*args, **kwargs)
Docstring:
Find and fix problems with self.face_normals and self.faces
winding direction.

For face normals ensure that vectors are consistently pointed
outwards, and that self.faces is wound in the correct direction
for all connected components.

Parameters
-------------
multibody : None or bool
  Fix normals across multiple bodies
  if None automatically pick from body_count
File:      ~/trimesh/trimesh/constants.py
Type:      method
FreakTheMighty commented 2 years ago

Hi @mikedh thanks for the comment, unfortunately that is what I was using, I guess I called out fix_inversion cause of the two "fixes" in fix_normals that seemed to be the one I was interested in.

https://github.com/mikedh/trimesh/blob/5c8891defc1967cfcb9fd7c0c908b4f666364091/trimesh/repair.py#L164-L167

mikedh commented 2 years ago

Gotcha fix_winding should make the normals consistently pointed though (i.e. not multicolored like your picture)?