2
$\begingroup$

I'm having trouble running this script I found on the exchange. how do I go about running it? My efforts are:

  • open blender,
  • delete default scene,
  • add a cube,
  • switch to scripting workspace,
  • create new text data block,
  • paste script, and
  • run.

Nothing happens when I move the cube only a bpy.ops.text.run_script() in the console. How do I add the shortcut key needed to run the script.

Here is the original post with script

Version 3.0 cycles windows

ZERO coding experience

$\endgroup$
1
  • $\begingroup$ @MartyFouts Shift-Q doesn't seem to work. How would I add the entry to the keymap? $\endgroup$
    – mehnoy
    Apr 14, 2022 at 14:48

1 Answer 1

1
$\begingroup$

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 ShiftQ 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:

Final result

Now you can run the script with ShiftQ

$\endgroup$
7
  • $\begingroup$ Thank you for the in-depth response I learned a lot. unfortunately, the object doesn't roll when I use the shortcut. when I press shift Q it just moves, as if I just grabbed it moving it along the axis. no rotation :(. I tried troubleshooting different blender versions and both scripts but to no avail. Am i missing an add-on in pref? $\endgroup$
    – mehnoy
    Apr 15, 2022 at 9:17
  • $\begingroup$ You're not missing anything. The script was written for an earlier version of Blender and it looks like it needs an update. When I have some time I'll take a look at what the update should be and modify the script in my answer. $\endgroup$ Apr 15, 2022 at 13:41
  • $\begingroup$ Thank you! I look forward to it. $\endgroup$
    – mehnoy
    Apr 15, 2022 at 15:38
  • $\begingroup$ did you find a workaround? $\endgroup$
    – mehnoy
    Apr 19, 2022 at 15:15
  • $\begingroup$ @mehnoy not yet. Big holiday over the weekend meant entertaining family. $\endgroup$ Apr 19, 2022 at 15:19

You must log in to answer this question.

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