View Full Version : Raycast from MEL Script

07-09-2012, 10:02 AM
I'm writing a procedural content script based on something I'm working on in Unity (C#). However it requires the ability to do a simple raycast hit test against geometry in the scene. I need to be able to pass a start point, a direction, and have a boolean return true if geometry was hit by the ray.

Is there a built-in Raycast function? I couldn't find anything in the command reference, and I'm not really sure where to start looking into rolling my own.

Some tips on where to start would be greatly appreciated!

07-09-2012, 10:30 AM
See if this helps:

and if you reached the same conclusion then consider this:

Although in your case I think it might be an overkill, and you might be satisfied with intersection of the objects bounding boxes.
Also for your case I would look into the selection interface, which tells if a click selected an object. You might be able to employ this mechanism to shoot a ray from an arbitrary point.

07-09-2012, 11:43 AM
you could also start by doing a search on this forum. search for rayIntersect.

07-09-2012, 11:47 AM
Brilliant, thank you very much!

EDIT: Both of you! Thanks :) I found a couple of threads mentioning "raycast" but not enough to kickstart from.

07-10-2012, 05:08 PM
I've played around with the python code from the first link, and it works perfectly but only if I have a positive ray direction, and in many cases a positive ray origin.

If I create a unit sphere (radius=0.5), set the ray origin at [0,0,0] and the ray direction to [0,1,0], the script returns 1 indicating a ray hit.

If I set any negative values in the ray origin or the ray direction, it gets a bit flakey and doesn't produce expected results (assuming I'm expecting the right results...)

For example, setting the ray origin at [0,1,0] and the ray direction to [0,-1,0], the script returns 0.

07-10-2012, 10:42 PM
First I would appreciate if you don't invent new definitions. Unit sphere means radius=1 (wiki).

Second, you have this line:

result = int(fmod(len(hitFaces), 2))

which I assume means return the modulus of the number of faces that were hit and 2. Meaning odd return true, even return false. Sounds familiar? Right, this script checks if a point is inside a mesh. I think that something such as

result = hitFaces

would get you what you want. But I'm only guessing, so let me know if you want me to actually check this.

07-11-2012, 12:26 AM
Thanks for pointing out the modulus. That was the problem.

CGTalk Moderation
07-11-2012, 12:26 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.