Emulate hotkey (G) Grab/Move by Python script


(Andreax81) #1

After a few hours of searching, I have not found documentation on how to emulate the pressure of the “G” key that allows you to move an object freely with the mouse. I have a modal function and I would like to insert this command to select an object and keep it on the mouse.

the command on blender suggests bpy.ops.transform.translate () but I can not find options for this.

(Nikola Yordanov) #2

hmm…maybe a workaround is to get the mouse position on screen,convert it to 3d vector and feed the xy to the object xy position …and since it’s modal…it will feed the mouse xy to the 3d object xy until released.
I managed to do that with 2d objects in OpenGL. I guess the trick is to turn the mouse position from screenspace to your 3d space.

(Andreax81) #3

you’re right, I’m looking at something like that, but I was thinking about taking advantage of the default snapping of blender, and this solution I think does not detect the snap (mesh, vertex, edge, volume)
I also thought I could simulate the “G” key but I think it is not possible, and that it creates some problems

(jandress) #4

bpy.ops.transform.translate('INVOKE_DEFAULT') will run the operator as modal. I don’t know how it plays with an already running modal operator.

Other parameters are here:

(Andreax81) #5

Yes that is the way! but is not simple to do , looking, I found exactly that. but with active snapping is very hard to do :s

(Andreax81) #6

This is addon, inside have what I’m looking for, have a script
that can place an object in grab mode
(Empty object) this example places a blank that will then be deleted, I would like that empty to be a cube (for example), and that does nothing but place it

(Jeacom) #7

This should do the trick.