2
$\begingroup$

I have an addon with that I draw in 3d space (lines). The vertices are where I click with the mouse cursor.

enter image description here

I use the region_2d_to_location to get a point from the mouse coordinates in 3d space. But I want to draw a bit more "in front" like 2 or 3 units in direction of the view camera (hope this is understandable:-))

I guess I have to manipulate the last parameter of the region_2d_to_location function, but how to calculate this location dependant on the current view?

I guess I can use the

bpy.context.space_data.region_3d.view_rotation

and then kind of multiply a vector like (2,2,2) with it to draw in a distance to the origin. Any ideas on how to do this with this Quaterion?

$\endgroup$
6
  • 1
    $\begingroup$ Related Add scalar * view_direction_vector to result of region2d to 3d location. $\endgroup$
    – batFINGER
    Commented Dec 18, 2018 at 9:50
  • $\begingroup$ I can get the view_direction like this: dir = bpy.context.space_data.region_3d.view_rotation * mathutils.Vector((0,0,-1)) vec = region_2d_to_location_3d(region, rv3d, (x, y), (0,0,0)) vec += 2.0 * dir // but a quaternion cant be added to a vector $\endgroup$
    – Jayanam
    Commented Dec 18, 2018 at 10:10
  • 2
    $\begingroup$ Oh really? Please re-check. Both dir and vec as stated above are vectors. r3d.view_rotation is a quaternion. A quaternion * vector => vector. Make dir a unit vector vec -= 2 * dir.normalized(). $\endgroup$
    – batFINGER
    Commented Dec 18, 2018 at 10:57
  • 2
    $\begingroup$ Sheet man... If you are in 2.8 use @ instead of * $\endgroup$
    – batFINGER
    Commented Dec 18, 2018 at 11:41
  • 1
    $\begingroup$ @batFINGER Oh man, that's it, you saved me! $\endgroup$
    – Jayanam
    Commented Dec 18, 2018 at 11:44

1 Answer 1

3
$\begingroup$

Thx to @batFINGER, here is the code that works (@ is important for multiplying Quaternion and Vector in Blender 2.8):

rot = bpy.context.space_data.region_3d.view_rotation

# Instead of adding it to the vector that is returned by region_2d_to_location_3d
# I pass it to the method as last argument
dir = rot @ mathutils.Vector((0,0,-1))
dir = dir.normalized() * -2.0

vec = region_2d_to_location_3d(region, rv3d, (x, y), dir)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .