2
$\begingroup$

I am trying to use python scripts on blender4.0 to do the following: detect whether the ray from the camera shooting point intersects with the obstacle, and find the global coordinates of the intersection point.

enter image description here

The key function ray_cast(), I can't find the official instructions, but it does work for now.

enter image description here

However, I encountered the following problems. When I changed the position of the obstacle (monkey head), I checked to see if the same ray was still blocked, only to find that the intersection position results were the same as the last time. Whether I use G to change the position or use a script to change the position, this problem occurs.

import bpy
import os

# Get the camera, target object, and obstacle object in the scene
camera = bpy.data.objects['Camera']
target_object = bpy.data.objects['Cube']
obstacle = bpy.data.objects['monkey']

# Define the ray origin and ray direction
ray_origin = camera.location
ray_direction = (target_object.location - camera.location).normalized()

output_directory = "F:/"
output_filename = "output.txt"
output_filepath = os.path.join(output_directory, output_filename)

for frame in range(5):
    obstacle.location.z += 0.5
    obstacle.update_from_editmode()
    
    obstacle.update_from_editmode()
    
    hit, location, normal, index = obstacle.ray_cast(ray_origin, ray_direction)
    
    if hit:
        output_file.write("Frame " + str(frame+1) + ": Ray intersects with the obstacle object at location: " + str(location) + "\n")
        output_file.write(output_file.write("Frame " + str(frame+1) + ": Normal at the intersection point: " + str(normal) + "\n"))
    else:
        output_file.write("Frame " + str(frame+1) + ": Ray does not intersect with the obstacle object\n")

output_file.close()

Running result enter image description here

I couldn't figure out what the problem was.

I also tried to keep the original relative position and press A to select all the objects and move them all together, which also resulted in an error (hit value changed to False).

$\endgroup$
4
  • $\begingroup$ this script doesn't work. -> hit is not defined. Pls post a script which runs and which you tried out. thank you. $\endgroup$ Commented Mar 4, 2024 at 8:56
  • $\begingroup$ and even better: provide blend file, so we don't have to guess where your objects are. thx. $\endgroup$ Commented Mar 4, 2024 at 9:04
  • $\begingroup$ and yes, the code as is (if you have the ray_cast command not in the loop) will have the result you have. Since you didn't show us your "real" code, we have no idea where you placed your ray_cast command $\endgroup$ Commented Mar 4, 2024 at 9:05
  • $\begingroup$ Sorry, I re-edited the complete code, please help me to see if there is any problem. $\endgroup$ Commented Mar 5, 2024 at 2:01

1 Answer 1

3
$\begingroup$

The docs for ray_cast are here.

Note that the origin and direction are given in object space. So you need to convert back and forth between world and object space.

# Location in world space
target_world = target_object.matrix_world.to_translation()
origin_world = camera_object.matrix_world.to_translation()

# Location in obstacle space
world2obj = obstacle.matrix_world.inverted()
target_obj = world2obj @ target_world
origin_obj = world2obj @ origin_world

# Direction in obstacle space
direction_obj = (target_obj - origin_obj).normalized()

# Ray cast
hit, loc_obj, normal, index = obstacle.ray_cast(origin_obj, direction_obj)

# Convert obstacle-space intersection to world space
loc_world = obstacle.matrix_world @ loc_obj
$\endgroup$
1
  • $\begingroup$ Thank you so much for your help, it really worked! $\endgroup$ Commented Mar 5, 2024 at 8:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.