Start a new topic
Answered

Python Script Distance from Raycast

I am using 2018b.


We want to pull the actual distance from the raycast sensor.

I have ways to do this in a custom extension for 2019a through cpp but not python.


Is there a simple enough way to report this


double* distances = reinterpret_cast<double*>(outputRayCastResults.distances.getRawMemory()); 


It's only for a single raycast line.


Best Answer

This was answered on another thread - I will repost the answer here:


You can access the output fields of the Raycast Sensor extension by using the "getOutput()" function with the name of the Output as an argument. Then, query the value of that field.

For example this will retrieve the value of the Output "Intersection Point": 
raycast_extension.getOutput('Intersection Point').value


To access the raycast extension via Python, if you're working with a Script extension in the Editor, you can give it a Parameter of type "Extension Pointer". (There's more information about that process here)

To populate that new Parameter, you can either drag the Raycast extension onto it from your Explorer list, or click the "..." buttons that appear when you move the mouse over it, and follow the instructions to select your extension.


Then, in your script, you will be able to refer to the extension you added as a parameter using this syntax (in this example, I named the field "raycast_extension"):
self.parameters.raycast_extension.value


So, combining that code with the code from above, the following will print the value of the raycast's Intersection Point output: 

raycast_extension = self.parameters.raycast_extension.value
print(raycast_extension.getOutput('Intersection Point').value)



Distance isn't discretely given as an output by this extension, but the origin and intersection point are given and can be used to calculate the distance. The distance would be equal to the square root of ((x2−x1)²+(y2−y1)²+(z2−z1)²)
Note that since, the "Origin" output is in a container called "Ray", the code to access it looks like this: 
raycast_extension.getOutput('Ray')['Origin'].value

1 Comment

Answer

This was answered on another thread - I will repost the answer here:


You can access the output fields of the Raycast Sensor extension by using the "getOutput()" function with the name of the Output as an argument. Then, query the value of that field.

For example this will retrieve the value of the Output "Intersection Point": 
raycast_extension.getOutput('Intersection Point').value


To access the raycast extension via Python, if you're working with a Script extension in the Editor, you can give it a Parameter of type "Extension Pointer". (There's more information about that process here)

To populate that new Parameter, you can either drag the Raycast extension onto it from your Explorer list, or click the "..." buttons that appear when you move the mouse over it, and follow the instructions to select your extension.


Then, in your script, you will be able to refer to the extension you added as a parameter using this syntax (in this example, I named the field "raycast_extension"):
self.parameters.raycast_extension.value


So, combining that code with the code from above, the following will print the value of the raycast's Intersection Point output: 

raycast_extension = self.parameters.raycast_extension.value
print(raycast_extension.getOutput('Intersection Point').value)



Distance isn't discretely given as an output by this extension, but the origin and intersection point are given and can be used to calculate the distance. The distance would be equal to the square root of ((x2−x1)²+(y2−y1)²+(z2−z1)²)
Note that since, the "Origin" output is in a container called "Ray", the code to access it looks like this: 
raycast_extension.getOutput('Ray')['Origin'].value

Login to post a comment