Skip to content

Instantly share code, notes, and snippets.

@esnosy
Created June 3, 2021 16:43
Show Gist options
  • Save esnosy/21616f2100ccdea736904d5e5e2a919e to your computer and use it in GitHub Desktop.
Save esnosy/21616f2100ccdea736904d5e5e2a919e to your computer and use it in GitHub Desktop.
How to efficiently ray cast static meshes Blender Python
import bpy
from mathutils.bvhtree import BVHTree
C = bpy.context
# Build BVH once
bvh = BVHTree.FromObject(C.object, C.evaluated_depsgraph_get())
for i in range(999999):
# Slower, this possibly builds BVH everytime
# C.object.ray_cast((0, 0, 0), (0, 0, -1))
# Faster
bvh.ray_cast((0, 0, 0), (0, 0, -1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment