0
$\begingroup$

I want to add "C" release circle select to my hotkeys and keep it in a portable file/addon. This seems to work when I run the script in blender, but the modal doesn't work when I start up blender.

bpy
import os


def Keymap():

    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon


    km = kc.keymaps.new('3D View', space_type='VIEW_3D', region_type='WINDOW', modal=False)
    kmi = km.keymap_items.new("view3d.select_circle", "C", "PRESS").properties.radius=1

    km = kc.keymaps.new(name='View3D Gesture Circle', space_type='VIEW_3D', region_type='WINDOW', modal=True)
    kmi = km.keymap_items.new_modal('CANCEL', 'C', 'RELEASE', any=True)    

def register():
    Keymap()

def unregister():
    Keymap()

if __name__ == "__main__":
    register()
$\endgroup$
0
$\begingroup$

Script below, named "km.py" and registered, works as expected for me on blender 2.8. It circle selects with set radius on C pressed and finishes on release.

Assumed the missing import bpy in question script was a copy paste error, but it may be your issue. Always check the system console for errors.

Script:

import bpy

def custom_keymaps():
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon

    km = kc.keymaps.new('3D View', space_type='VIEW_3D', region_type='WINDOW', modal=False)
    kmi = km.keymap_items.new("view3d.select_circle", "C", "PRESS")
    kmi.properties.radius = 10

    km = kc.keymaps.new(name='View3D Gesture Circle', space_type='VIEW_3D', region_type='WINDOW', modal=True)
    kmi = km.keymap_items.new_modal('CANCEL', 'C', 'RELEASE', any=True)    

custom_keymaps()

Only aesthetic changes re setting kmi after creating a new item. (otherwise kmi is set to the radius which is a little misleading) and calling the method directly rather than via register method. Script in question also works as expected with the import and calling register() at level 0.

enter image description here Given a py extension and register checked

How to auto-run a simple script?

If you are executing this in a registered script (sane text block name with .py extension and the register script checkbox ticked on text editor header) then same as answer here https://blender.stackexchange.com/a/134416/15543 also applies.

__name__ will be the sane textblock name without the python extension. When imported on register. Prob simpler to put register() as the last line of script in this case.

Currently your unregister method is also registering, however loading script this way will probably not trigger any unregisster activity.

You can import these into the python console. Let's imagine your script is called "foo.py" in the text editor then

import foo
foo.register()

will register it similarly to an addon. Later you can call

foo.unregister() 

to unregister while seeing any errors printed into the python console.

PS if this is the case could possibly mark this q as a dupe.

$\endgroup$
  • $\begingroup$ So, I removed the whole if _name_ == "_main_" bit and just left the register. It doesn't really solve the problem with having the circle select modals change. Currently if I just drop this script into startup, I can change most hotkeys, but it doesn't change the view_3d gesture circle mapping. If I run it from blender's text editor it does. $\endgroup$ – Brandon Bien Mar 17 at 17:39
  • $\begingroup$ See edit. Tested on 2.8 When test file is loaded it changes both the radius and cancels when released as expected. $\endgroup$ – batFINGER Mar 18 at 11:31
  • $\begingroup$ Thank you for your patience and input, I greatly appreciate the help. It works in editor as expected, but doesn't work when I copy it to my startup directory and restart blender. Blender will recognize the new keymap, but it will not recognize the new modal. $\endgroup$ – Brandon Bien Mar 18 at 17:41
  • $\begingroup$ I am suggesting you add the script to your blend file register it as shown and save. Then whenever you open or load that blend it (for me on 2.8) works as expected. It can be added to default startup blend. If in startup folder use the register method, as like for addons it gets called automatically. However the order might count in this case. Addons and startup scriipts are called during a restricted context state. any prints in them will show up first (or early) after blender starts. A registered script gets run last. $\endgroup$ – batFINGER Mar 18 at 18:12
  • $\begingroup$ Oh, I see. I was trying to have a portable hotkey file to share between work and home, but I suppose registering it will have to do. Thank you very much for your answer. $\endgroup$ – Brandon Bien Mar 18 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.