Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create_bvh_tree_multi_objects creates orphan meshes #1097

Closed
rasmushaugaard opened this issue May 22, 2024 · 0 comments · Fixed by #1098
Closed

create_bvh_tree_multi_objects creates orphan meshes #1097

rasmushaugaard opened this issue May 22, 2024 · 0 comments · Fixed by #1098
Labels
question Question, not yet a bug ;)

Comments

@rasmushaugaard
Copy link
Contributor

Describe the issue

Observing increasing memory usage when calling create_bvh_tree_multi_objects multiple times.

The below code sample has a copy of the implementation .
The below code also has a fix (one line), removing the mesh copies after use.

Minimal code example

import blenderproc as bproc  # noqa
import mathutils  # type: ignore

from typing import List

from blenderproc.python.types.MeshObjectUtility import MeshObject, bmesh, Matrix


def create_bvh_tree_multi_objects(
    mesh_objects: List[MeshObject],
) -> mathutils.bvhtree.BVHTree:
    """Creates a bvh tree which contains multiple mesh objects.

    Such a tree is later used for fast raycasting.

    :param mesh_objects: The list of mesh objects that should be put into the BVH tree.
    :return: The built BVH tree.
    """
    # Create bmesh which will contain the meshes of all objects
    bm = bmesh.new()
    # Go through all mesh objects
    for obj in mesh_objects:
        # Get a copy of the mesh
        mesh = obj.get_mesh().copy()
        # Apply world matrix
        mesh.transform(Matrix(obj.get_local2world_mat()))
        # Add object mesh to bmesh
        bm.from_mesh(mesh)

        # Uncomment below line to avoid leaving orphan meshes
        # bpy.data.meshes.remove(mesh)

    # Create tree from bmesh
    bvh_tree = mathutils.bvhtree.BVHTree.FromBMesh(bm)
    bm.free()
    return bvh_tree


bproc.init()
mesh = bproc.object.create_primitive("SPHERE")
for i in range(500):
    create_bvh_tree_multi_objects([mesh] * 100)

Files required to run the code

No response

Expected behavior

Would expect that create_bvh_tree_multi_objects frees any temporary objects that it might create, but memory usage increases when called multiple times.

BlenderProc version

v2.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question, not yet a bug ;)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant