0
$\begingroup$

Is there a way to access the grid of the current 3D View and snap the mouse cursor to the grid. Or if snaping is not supported, get the grid interesections so that I can draw points just to these locations and not in between?

$\endgroup$
4
  • 1
    $\begingroup$ 3D view > properties (N) > display panel has the grid settings. $\endgroup$
    – batFINGER
    Commented Dec 18, 2018 at 12:22
  • $\begingroup$ No I meant - sorry only mentionioned in the title - with Python:-) $\endgroup$
    – Jayanam
    Commented Dec 18, 2018 at 12:56
  • 1
    $\begingroup$ Yes with python, as in answer below. $\endgroup$
    – batFINGER
    Commented Dec 18, 2018 at 12:57
  • $\begingroup$ Ah ok, now I saw it:-) $\endgroup$
    – Jayanam
    Commented Dec 18, 2018 at 12:59

2 Answers 2

1
$\begingroup$

PY console code

Each 3d view space has a setting for grid lines and grid scale (as well as grid subdivisions) See 3D View > Properties (N) > Display

For example sake I have set the lines to 4 and the scale to 0.5.

Py console test code, first find the 3d view area.

>>> for i, a in enumerate(C.screen.areas):
...     i, a.type
...     
(0, 'INFO')
(1, 'PROPERTIES')
(2, 'CONSOLE')
(3, 'VIEW_3D')
(4, 'TEXT_EDITOR')

>>> space = C.screen.areas[3].spaces.active

Get the settings

>>> space.grid_lines
4

>>> space.grid_scale
0.5

Set up our range (for example -2 to 2) Notice I use integer modulus j // 2 (since odd numbers give the result of prior even number)

>>> g = space.grid_lines // 2
>>> rge = range(-g, g + 1)

Iterate over x and y to give the 2D vec of XY grid. The scaling of 0.5 makes the point (2, 2) => (1, 1) (_ ... are results edited out for brevity)

>>> for x in rge:
...     for y in rge:
...         space.grid_scale * Vector((x, y))
...         
Vector((-1.0, -1.0))
Vector((-1.0, -0.5))
Vector((-1.0, 0.0))
Vector((-1.0, 0.5))
    ...
Vector((0.0, -1.0))
Vector((0.0, -0.5))
Vector((0.0, 0.0)) # origin
Vector((0.0, 0.5))
    ...
Vector((1.0, 1.0))

>>> 
$\endgroup$
1
  • $\begingroup$ Thx, that helped a lot. so basically when I have mouse coords I just need the scaling, have to do it just when in Ortho mode and convert 3d to 2d, then integer modulo and take scaling into account to get the point. Great! $\endgroup$
    – Jayanam
    Commented Dec 18, 2018 at 13:02
1
$\begingroup$

ok, first of all, the grid setting can be found in the right panel (hotkey N), under the display section.

grid setting can be found here

I did not find the path to those properties in python, but if you look deeper i'm sure you will find them. Anyway, i placed in the script the default values of the default grid, and in the script i basically created a copy of that grid (if you run it you will see that the cubes move to the intersections in the grid)

import bpy

ops = bpy.ops
lines = 16
Scale = 1,00
subdiv = 10

for row in range(lines+1):
    for col in range(lines+1):
        ops.mesh.primitive_cube_add()
        ops.transform.resize(value = (0.2, 0.2, 0.2))
        bpy.ops.transform.translate(value=(row-lines/2, col-lines/2, 0))

To move the cursor to the closer intersection, just round the cursor location to the closest integer, then find the "row" and "col" values for that point and move the cursor to that location.

I'm not sure i solved your problem, tell me how it goes

T

$\endgroup$
2
  • 1
    $\begingroup$ Thx, this works, but already solved by @badFINGER - but upvoted anyway $\endgroup$
    – Jayanam
    Commented Dec 18, 2018 at 13:23
  • $\begingroup$ yeah, I saw that too late ahah $\endgroup$
    – Tareyes
    Commented Dec 18, 2018 at 14:33

You must log in to answer this question.

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