family_ops.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING
  3. if TYPE_CHECKING:
  4. from typing import Iterable, List, Set, Tuple
  5. from manimlib.mobject.mobject import Mobject
  6. def extract_mobject_family_members(
  7. mobject_list: Iterable[Mobject],
  8. exclude_pointless: bool = False
  9. ) -> list[Mobject]:
  10. return [
  11. sm
  12. for mob in mobject_list
  13. for sm in mob.get_family()
  14. if (not exclude_pointless) or sm.has_points()
  15. ]
  16. def recursive_mobject_remove(mobjects: List[Mobject], to_remove: Set[Mobject]) -> Tuple[List[Mobject], bool]:
  17. """
  18. Takes in a list of mobjects, together with a set of mobjects to remove.
  19. The first component of what's removed is a new list such that any mobject
  20. with one of the elements from `to_remove` in its family is no longer in
  21. the list, and in its place are its family members which aren't in `to_remove`
  22. The second component is a boolean value indicating whether any removals were made
  23. """
  24. result = []
  25. found_in_list = False
  26. for mob in mobjects:
  27. if mob in to_remove:
  28. found_in_list = True
  29. continue
  30. # Recursive call
  31. sub_list, found_in_submobjects = recursive_mobject_remove(
  32. mob.submobjects, to_remove
  33. )
  34. if found_in_submobjects:
  35. result.extend(sub_list)
  36. found_in_list = True
  37. else:
  38. result.append(mob)
  39. return result, found_in_list