Before I show you how to add the keymap entry, here's a changed script that installs the keymap automatically.
Paste and run this version so you can use Shift–Q to run it. However, I've also added step by step instructions for adding the shortcut by hand, if you want to stick to the original code.
EDIT: I've modified the script to be closer to how the original script performs but I didn't have time to figure out what broke between releases so it's not exactly the same.
Modified Script with keymap entry
import bpy
from mathutils import Matrix, Vector
mode_keymap = None
class RollOperator(bpy.types.Operator):
bl_idname = "transform.roll"
bl_label = "Roll active object"
def execute(self, context):
dx = self.dx / 100
dy = self.dy / 100
#matrix = Matrix.Rotation((dx**2 + dy**2)**.5, 4, Vector((-dy, dx, 0)))
#matrix *= context.object.matrix_world
context.object.rotation_euler.x += dx*pi/4
context.object.rotation_euler.y += dy*pi/4
context.object.location.x += dx
context.object.location.y += dy
return {'FINISHED'}
def modal(self, context, event):
if event.type == 'MOUSEMOVE': # Apply
self.dx = event.mouse_x - event.mouse_prev_x
self.dy = event.mouse_y - event.mouse_prev_y
self.execute(context)
elif event.type == 'LEFTMOUSE': # Confirm
return {'FINISHED'}
elif event.type in ('RIGHTMOUSE', 'ESC'): # Cancel
context.object.location = self.location
context.object.rotation_euler = self.rotation_euler
return {'CANCELLED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
rotation = context.object.rotation_euler
location = context.object.location
self.location = location[0], location[1], location[2]
self.rotation_euler = rotation[0], rotation[1], rotation[2]
self.dx = 0
self.dy = 0
self.execute(context)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
bpy.utils.register_class(RollOperator)
key_config = bpy.context.window_manager.keyconfigs.addon
if key_config:
key_map = key_config.keymaps.new(name='3D View', space_type='VIEW_3D')
key_entry = key_map.keymap_items.new("transform.roll",
type='Q',
value='PRESS',
shift=True,
)
mode_keymap = (key_map, key_entry)
How to add the entry by hand
Edit Preferences, and go to the Keymap tab
Scroll down until you see the 3D View tab. Click the arrow to open it.
Scroll down until you see the Object Mode tab. Click the arrow to open it.
Scroll down until you see the Object Mode (Global) tab. Click the arrow to open it. You'll now be nested 3 deep.
Scroll down until you see the Add New button. Click it. That will add an entry labeled "none" just above the button. Click the arrow to open it.
type transform.roll
into the box that has "none" in it. Change the A
to a Q
(don't shift, just click the A
and type q
) and click the shift
button. The edit box should now look like this:

Now you can run the script with Shift–Q