4
$\begingroup$

I've been struggling all day to add icon with action from loop tools (circularize selection). Firstly I've added a hotkey into my keymap:

Keymap settings

Then I looked into it's structure and tried to make it happen with ui_tool_simple template in scripting tab.

I came up with something like this:

# This example adds an object mode tool to the toolbar.
# This is just the circle-select and lasso tools tool.
import bpy
from bpy.types import WorkSpaceTool

class MyTool(WorkSpaceTool):
    bl_space_type='VIEW_3D'
    bl_context_mode='EDIT_MESH'

    # The prefix of the idname should be your add-on name.
    bl_idname = "ui_plus.circularize"
    bl_label = "Circularize"
    bl_options = {'REGISTER', 'UNDO'}
    bl_description = (
    "Move selected vertices into a circle shape"
    )
    bl_icon = "ops.generic.select_circle"
    bl_widget = None
    bl_keymap = (
        ("mesh.looptools_circle", {"type": 'LEFTMOUSE', "value": 'PRESS'}, 
        {"properties": [False, "best", True, 100, False, False, False, 1, True]})
    )

    # bpy.ops.mesh.looptools_circle(custom_radius=False, fit='best', flatten=True, influence=100, lock_x=False, lock_y=False, lock_z=False, radius=1, regular=True)

    def draw_settings(context, layout, tool):
        props = tool.operator_properties("mesh.looptools_circle")
        layout.prop(props, "custom_radius")
        layout.prop(props, "fit")
        layout.prop(props, "flatten")
        layout.prop(props, "influence")
        layout.prop(props, "lock_x")
        layout.prop(props, "lock_y")
        layout.prop(props, "lock_z")
        layout.prop(props, "radius")
        layout.prop(props, "regular")

def register():
    bpy.utils.register_tool(MyTool, after={"builtin.scale_cage"}, separator=True, group=True)

def unregister():
    bpy.utils.unregister_tool(MyTool)

if __name__ == "__main__":
    register()

Although it just gives bunch of errors.

Traceback (most recent call last):
  File "\ui_tool_simple.py", line 45, in <module>
  File "\ui_tool_simple.py", line 39, in register
  File "2.80\scripts\modules\bpy\utils\__init__.py", line 795, in register_tool
    tool_converted = tool_from_class(tool_cls)
  File "2.80\scripts\modules\bpy\utils\__init__.py", line 792, in tool_from_class
    cls._km_action_simple(kc, context_descr, tool_def.label, keymap_data)
  File "2.80\scripts\startup\bl_ui\space_toolsystem_common.py", line 317, in _km_action_simple
    keymap_fn[0](km)
  File "2.80\scripts\startup\bl_ui\space_toolsystem_common.py", line 52, in keymap_fn
    keymap_init_from_data(km, keymap_fn.keymap_data)
  File "2.80\scripts\modules\bl_keymap_utils\io.py", line 231, in keymap_init_from_data
    for (kmi_idname, kmi_args, kmi_data) in km_items:
ValueError: too many values to unpack (expected 3)
Error: Python script failed, check the message in the system console

If I delete draw_settings, it will let go - no errors, but when I press newly created button nothing happens. When I hold mouse and drag it only moves selected faces.

Circularize

PS. Commented line meaning:

    # bpy.ops.mesh.looptools_circle(custom_radius=False, fit='best', flatten=True, influence=100, lock_x=False, lock_y=False, lock_z=False, radius=1, regular=True)

Is what python console says is being done when pressing shift + ctrl + c (as a hotkey from keymap)

$\endgroup$
  • $\begingroup$ According to EDIT 2, have you fixed it? If yes then please answer it below, so it is noticeable to future users! ;) $\endgroup$ – Yash Mar 19 at 8:50
  • $\begingroup$ Yes, problem is FIXED, for solution look into EDIT2 inside first post. Here's a video showing how it works: Youtube $\endgroup$ – Koniotaur Mar 19 at 10:14
  • $\begingroup$ Then please remove the code of EDIT 2, and add it in the answer section! :) It will help others(future users) to get an answer, and if you want add some additional info, then its up to you! $\endgroup$ – Yash Mar 19 at 10:18
  • $\begingroup$ It has to stay as it is because if I would like to paste all of the code there it keeps saying that comment is too long by over 600 characters. $\endgroup$ – Koniotaur Mar 19 at 10:21
  • $\begingroup$ Hey buddy, I am not telling you to comment, I am telling you to add your answer in answer section, down at the bottom! $\endgroup$ – Yash Mar 19 at 10:23
4
$\begingroup$

To everyone who wanted to also know why this doesn't work - here after long fight I managed to make it work, here's code:

# This example adds an object mode tool to the toolbar.
# This is just the circle-select and lasso tools tool.
import bpy
from bpy.types import WorkSpaceTool

class Circularize(WorkSpaceTool):
    bl_space_type='VIEW_3D'
    bl_context_mode='EDIT_MESH'

    # The prefix of the idname should be your add-on name.
    bl_idname = "ui_plus.circularize"
    bl_label = "Circularize"
    bl_options = {'REGISTER', 'UNDO'}
    bl_description = (
        "Move selected vertices into a circular shape"
    )
    bl_icon = "ops.generic.select_circle"
    bl_widget = None
    bl_keymap = (
        ("mesh.looptools_circle", {"type": 'LEFTMOUSE', "value": 'PRESS'},
    {"properties": [("custom_radius", False), ("radius", 1)]}),
        )


def register():
    bpy.utils.register_tool(Circularize, after={"builtin.scale_cage"}, separator=True, group=True)

def unregister():
    bpy.utils.unregister_tool(Circularize)

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

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.