0
$\begingroup$

How would I go about creating a hotkey that would operate as follows?:

  1. With an object selected in the modeling viewport:
  2. Press Shift + D (this would be the custom shortcut to trigger what follows ,not the default 'duplicate' command)
  3. Selected Object gets a catmull-clark subdiv modifier added with default settings bpy.ops.object.modifier_add(type='SUBSURF')

  4. Subdivision modifier gets applied to mesh (bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Subdivision")

?

I am new to Blender as of 2.8 and am interested in customizing the UI to make it more like the software packages I am coming from (Maya and Modo). At the moment I am interested in understanding how to consolidate multiple commands into a single operation that can be bound to a hotkey. I figured this would be a simple enough one to start with. I have some limited python experience from Maya and have been looking at the API but I have been having difficulty understanding how it all works. I understand I would likely need a hotkey .py file to bind the operation to a key and an operator .py file to define the operations and possibly to include the commands. How would I get this working?

I tried a hotkey file in startup that looked like this:

import bpy
import os

    km = kc.keymaps.new('Window', space_type='EMPTY',     region_type='WINDOW', modal=False)
    Global_Keys()

    kmi = km.keymap_items.new('mesh.testSubdivide', 'D', 'PRESS', shift=True)

With an operator .py file that looked like this:

class testSubdivide(bpy.types.Operator):
    bl_idname = "mesh.testSubdivide"
    bl_label = "testSubdivide"        
    bl_options = {'REGISTER', 'UNDO'} 

def execute(self, context):
    bpy.ops.object.modifier_add(type='SUBSURF')
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Subdivision")
    return {'FINISHED'}

I know this is wrong...but i'm blocked. How would I do this?

$\endgroup$

2 Answers 2

0
$\begingroup$

I usually create a quick addon for those kinds of macro (not sure if there is another way).

When you have the script you need to install it, activate and save preference so when you quit blender it doesn't disappear.

enter image description here

Once you have the script installed you can take the bl_idname and use that when you create a shortcut. In the case of the script that I am sharing the bl_idname is object.simple_macro

Then in the Input tab you can create a new shortcut.

enter image description here enter image description here

if you want to use an existing key binding you probably should disable them before.

enter image description here

bl_info = {
    "name": "My Macro",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "",
    "description": "",
    "warning": "",
    "wiki_url": "",
    "category": "",
}

import bpy


def main(context):
    """Your code here."""

    if bpy.context.selected_objects:
        bpy.ops.object.modifier_add(type='SUBSURF')
        bpy.context.object.modifiers["Subdivision"].levels = 2
        bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Subdivision")
        bpy.ops.object.shade_smooth()

    else:
        print('no object selected')


class SimpleMacro(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_macro"
    bl_label = "Simple Macro"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        main(context)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleMacro)


def unregister():
    bpy.utils.unregister_class(SimpleMacro)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.simple_macro()
$\endgroup$
1
  • 1
    $\begingroup$ Thank you so much for this thorough and complete explanation! This is exactly the info I was looking for including the steps on how to add the scripts. Big thanks. $\endgroup$
    – jnse
    Commented Dec 13, 2018 at 1:21
-1
$\begingroup$

If you just want to apply a subsurface modifier you can just press CTRL+2..., where the number (2 in this case) is the number of subdivisions you see in the viewport. If not, i always prefer AutoHotkey (https://www.autohotkey.com/) to bind keystroke sequences to a custom hotkey, it's very easy to program and you can turn it off insantly...

$\endgroup$
1
  • $\begingroup$ Thanks for the info! I should have made it clear that I'm more interested in learning how to customize the UI with python in order to recreate a lot of the ease-of-use features of modo that I am used to in order to streamline modeling. Bender absolutely seems to have all the functionality that I need - but often requires multiple time-consuming steps that could be simplified with hotkeys if I could understand how to customize the configuration. $\endgroup$
    – jnse
    Commented Dec 10, 2018 at 17:42

You must log in to answer this question.

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