Snap to grid while MouseDrag()

I have a ToolData plugin where the user can draw Planes.
I use MouseInput() to get the inital point and then MouseDragStart() and MouseDrag() to get the other point of the Plane.

Is there a way to snap to the grid using MouseDragStart() and MouseDrag() or is that something I have to do myself?
Something like snapPoint = grid_size * round(point / grid_size)

            mousex = msg[c4d.BFM_INPUT_X]
            mousey = msg[c4d.BFM_INPUT_Y]
            leftTop = c4d.Vector(mousex, mousey, 0)
            win.MouseDragStart(c4d.KEY_MLEFT, mousex, mousey, c4d.MOUSEDRAGFLAGS_DONTHIDEMOUSE|c4d.MOUSEDRAGFLAGS_NOMOVE)

            color = c4d.Vector(256,256,256)
            bd.SetMatrix_Screen()   #Use the screen's matrix to draw on
            mx = mousex
            my = mousey
            while True:
                result, dx, dy, channels = win.MouseDrag()
                if result!=c4d.MOUSEDRAGRESULT_CONTINUE: break
                mx += dx
                my += dy
                rightBottom = c4d.Vector(mx, my,0)
                self.v1 = leftTop
                self.v2 = rightBottom


Hello @pim,

Thank you for reaching out to us. It depends a bit on what you expect to happen here.

There is the snapping module of Cinema 4D but you cannot actually get values out of it, i.e., you cannot compute the snapped value of x with it, you can only define the snap settings with it.

But when I understand your correctly, you just want to quantize some mouse inputs for a plugin and for that you must indeed compute the values yourself. It is best to also draw a snapping location onto the screen, so that user can see where the actual input is in relation to the mouse.

How the snapping works in detail depends on what you want to do exactly when quantizing your plane drawing. The snapping module might become relevant here, because with it you can retrieve the working planes, which might be useful when placing planes.


MAXON SDK Specialist