0
$\begingroup$

I decided to try and write an addon in Blender for a school project. I am 100% new to the Blender api and I am trying to get my bearings from the api docs. The below code works sometimes and most other times crashes Blender.

Anyone that can take the time to give me some pointers on what i am doing wrong here it would be greatly appreciated

import bpy

class SCULPT_MT_pie_menu(bpy.types.Menu):
    bl_label = "Pie Menu"
    
    def draw(self, context):
        layout = self.layout
        layout.menu_pie().operator("sculpt.mask_brush", text = "Mask Brush", icon = "BLENDER")
        layout.menu_pie().operator("sculpt.remesh", text = "Remesh", icon = "BLENDER")

class SCULPT_OT_mask_brush(bpy.types.Operator):
    """Mask Brush"""
    bl_idname = "sculpt.mask_brush"
    bl_label = "Mask Brush"

    def execute(self, context):
        bpy.ops.object.mode_set(mode='SCULPT')   
        bpy.ops.wm.tool_set_by_id(name="builtin_brush.Mask", cycle=False, as_fallback=False, space_type='VIEW_3D')
        return {'FINISHED'}

class SCULPT_OT_remesh(bpy.types.Operator):
    """Remesh"""
    bl_idname = "sculpt.remesh"
    bl_label = "Remesh"

    def execute(self, context):
        bpy.ops.object.voxel_remesh()
        return {'FINISHED'}


bpy.ops.wm.call_menu_pie(name="SCULPT_MT_pie_menu")

def register():
    bpy.utils.register_class(SCULPT_MT_pie_menu)
    bpy.utils.register_class(SCULPT_OT_mask_brush)
    bpy.utils.register_class(SCULPT_OT_remesh)
  
def unregister():
    bpy.utils.unregister_class(SCULPT_MT_pie_menu)
    bpy.utils.unregister_class(SCULPT_OT_mask_brush)
    bpy.utils.unregister_class(SCULPT_OT_remesh)

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

1 Answer 1

1
$\begingroup$

The way i was dealing with input was not correct i needed to use the more advanced invoke and modal options to get the desired results. https://docs.blender.org/api/2.90/bpy.types.Operator.html?highlight=operator#module-bpy.types.Operator

Here is the updated code:

import bpy

class PS_Remesh(bpy.types.Operator):
    bl_idname = "ps.remesh"
    bl_label = "Remesh"

    def execute(self, context):
        if context.active_object is None:
            print("Active object == none: Cancel")
            return {'CANCELLED'}
        else:
            bpy.ops.object.voxel_remesh()
            return {'FINISHED'}

class PS_MaskBrush(bpy.types.Operator):
    bl_idname = "ps.mask_brush" #use this as the pie.operator("ps.remesh") to use in pie menu
    bl_label = "Mask Brush"
    
    def execute(self, context):
        if context.active_object is None:
            print("Active object == none: Cancel")
            return {'CANCELLED'}
        elif context.mode == 'SCULPT':
            print("In sculpt mode sculpt mode select mask brush")
            bpy.ops.wm.tool_set_by_id(name="builtin_brush.Mask",space_type="VIEW_3D")
            return {'FINISHED'}
        elif context.mode != 'SCULPT':
            print("Not in sculpt mode sculpt mode activate")
            bpy.ops.object.mode_set(mode='SCULPT')
            bpy.ops.wm.tool_set_by_id(name="builtin_brush.Mask", space_type="VIEW_3D")
            return {'FINISHED'}

class VIEW3D_MT_pie_sculpt(bpy.types.Menu):
    # label is displayed at the center of the pie menu.
    bl_label = "Pie Sculpt"

    def draw(self, context):
        pie = self.layout.menu_pie()
        pie.operator("ps.remesh", text = "Remesh", icon="BLENDER")
        pie.operator("ps.mask_brush", text="Mask Brush", icon="BLENDER")


def register():
    bpy.utils.register_class(PS_Remesh)
    bpy.utils.register_class(PS_MaskBrush)
    bpy.utils.register_class(VIEW3D_MT_pie_sculpt)


def unregister():
    bpy.utils.unregister_class(PS_Remesh)
    bpy.utils.unregister_class(PS_MaskBrush)
    bpy.utils.unregister_class(VIEW3D_MT_pie_sculpt)

if __name__ == "__main__":
    register()
    
    bpy.ops.wm.call_menu_pie(name="VIEW3D_MT_pie_sculpt")
$\endgroup$

You must log in to answer this question.

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