# Paint circle with Python in 3D view direction

I want to draw a circle into the 3d view in the direction of the current view rotation.

I have 2 vectors (center, mouse_pos), so I have two points in 3d space, so I have the radius - fine.

This is my code so far and the painted result:

segments = 32
mul = (1.0 / (segments - 1)) * (pi * 2)
self._vertices = [(sin(i * mul) + self._center[0],
cos(i * mul) + self._center[1], 0) for i in range(segments)]


With this the circle is painted like I have a "top perspective". I also have a method to get the current view direction:

def get_view_direction(context):
rv3d      = context.space_data.region_3d
view_rot  = rv3d.view_rotation

dir = view_rot @ mathutils.Vector((0,0,-1))
return dir.normalized()


My question is now how to let the segments of the circle take the rotation of the view into account? Any ideas?

EDIT: First part solved:

rv3d      = context.space_data.region_3d
view_rot  = rv3d.view_rotation

segments = 32
mul = (1.0 / (segments - 1)) * (pi * 2)
points = [(sin(i * mul), cos(i * mul), 0) for i in range(segments)]

for point in points:
vertex = view_rot @ mathutils.Vector(point)
self._vertices.append(vertex)


What's left now is to take the radius and center into acount, and I want to move it along the view direction, but I already have a method for this:

def get_view_direction(context):
rv3d      = context.space_data.region_3d
view_rot  = rv3d.view_rotation

dir = view_rot @ mathutils.Vector((0,0,-1))
return dir.normalized()


So I guess I can add the direction multiplied by a scalar to each vector?

Okay guys got it, sorry to again answer my own question:-)

rv3d      = context.space_data.region_3d
view_rot  = rv3d.view_rotation

segments = 32
mul = (1.0 / (segments - 1)) * (pi * 2)
points = [(sin(i * mul) * self._radius, cos(i * mul) * self._radius, 0)
for i in range(segments)]

self._vertices = [view_rot @ mathutils.Vector(point) +
self._center for point in points]
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.3430509865283966, "perplexity": 3872.8099459475843}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-29/segments/1593655879738.16/warc/CC-MAIN-20200702174127-20200702204127-00515.warc.gz"}