0
$\begingroup$

I am calculating a 3d point on a face:

enter image description here

How can I get the distance to the origin from V1 either along the Y axis or along the face normal that I hit?

let me explain once more, I hope a bit more clear this time. I am shooting a ray with python, hitting a face (assume the direction is the face normal).

enter image description here

I want to find out the distance (d) of this normal from the face to the origin of the coordinate system.

enter image description here

But the face / plane could be anywhere, for example at the top, the bottom… and I want to find out the distance of this normal-vector of the face to the origin (0,0,0).

Thx for your help.

$\endgroup$
  • $\begingroup$ What is V1 in this case? Just some point of your choice? To get the distance along a normalized vector, you use the dot product between the vector (the face normal, or the Y axis) and the vector between obj.location and your point. You can use the vector class in the mathutils package for this. $\endgroup$ – Kalle Halvarsson Jan 28 '19 at 15:50
  • $\begingroup$ @KalleHalvarsson V1 is the 3d vertex that hits the face of an object. I just want to know the distance from this vertex along an axis - let's say I snapped the view front ortho - along the Y axis. If I snapped the view to top or back ortho then along the z axis. Perhaps I can use the region3d then for this calculation? $\endgroup$ – Jayanam Jan 28 '19 at 18:00
  • $\begingroup$ Still find this confusing. Sounds to me like you want the global coordinate of V1, Where its distance along y axis direction to x axis is V1.y etc. (or as shown in answer below) V1.dot(y_axis) $\endgroup$ – batFINGER Jan 30 '19 at 13:24
1
$\begingroup$

I think i understand your question now. Timdar's solution will unfortunately not work, it only gives you the Euclidean distance between the origin and the point, not the distance along the normal. It is close though - like i said, you need to use the dot product to project the separating vector onto the normal. This should work:

import bpy
from mathutils import Vector

# Let's say we use raycasting the same way as in timdar's suggestion:

hitData = bpy.context.scene.ray_cast(rayOrigin, rayDir)
# hitData is structured like this: (result, location, normal, index, object, matrix)

if hitdata[0]: # Did we hit anything?
    normal = Vector(hitData[2])
    position = Vector(hitData[1])
    origin = Vector((0, 0, 0)) # We can use any value here.
    delta = position - origin # In this case, this is the same as position.
    distance = abs(delta.dot(normal)) # Project the length of delta along normal.

It is also possible to replace "normal" with some other normalized axis, for instance the look axis of your viewport. You can find more about how to use the viewport matrix here: Viewport position and direction

| improve this answer | |
$\endgroup$
1
$\begingroup$

You could use this:

bpy.context.scene.ray_cast(viewLayer, origin, direction)
viewLayer = the view layer you are operating on
origin = the Vector origin of the ray
direction = the normalised Vector direction

If you are just using the default view layer you can just grab it via:
viewL = bpy.data.scenes['Scene'].view_layers["View Layer"]
origin would be the Vector position of V1
direction would be a normalised Vector, so (0,-1,0) for a negative Y axis direction

result = bpy.context.scene.ray_cast(viewL, (V1.x, V1.y, V1.z), (0,-1,0))

result contains a tuple, with the second item containing the location of the hit point on the mesh

So then you just want to calculate the distance between the two vectors so it would be:

import math

viewL = bpy.data.scenes['Scene'].view_layers["View Layer"]
result = bpy.context.scene.ray_cast(viewL, (V1.x, V1.y, V1.z), (0,-1,0))
hit = result[1]
distance = math.sqrt((V1.x - hit[0])² + (V1.y - hit[1])² + (V1.z - hit[2])²)
| improve this answer | |
$\endgroup$
  • $\begingroup$ If you want the distance between the point on the face that is hit by your ray (hit) and the origin (0,0,0) just do: (hit.x² + hit.y² + hit.z²)² $\endgroup$ – timdar Jan 28 '19 at 21:37
  • $\begingroup$ Ok, but there is just one hit, this is V1. From V1 I want to calculate the distance along the face normal to the origin of the coordinate system (0,0,0) $\endgroup$ – Jayanam Jan 28 '19 at 21:38
  • $\begingroup$ I am utterly sorry if I am totally off, but wouldnt you want to use this: math.stackexchange.com/questions/42640/… So it would be the root of (x²,y²,z²)? $\endgroup$ – morph3us Jan 28 '19 at 23:30
  • $\begingroup$ @morph3us yes you are correct, my mistake, I'll update the formula, thanks $\endgroup$ – timdar Jan 29 '19 at 11:25
  • $\begingroup$ I am not a native English speaker, so please dont mind a dumb question: Is "sqrt" taking the root of something? Because in my mind its just an abbreviation of "square root", which would be closely related to "squared". So I always thought it would first square the value, then take the root (or vice versa?)? $\endgroup$ – morph3us Jan 29 '19 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.