Re: Pouvoir choisir l'outil actif avec python

Posté par pierre-y le 25/5/2020 13:20:16
Merci a testure pour l'aide :
import bpy

bl_info = {
    "name": "Grease Pencil switch Draw<-->select",
    "author": "Django950",
    "version": (1, 0),
    "blender": (2, 82, 0),
    "location": "View3D > UI> GP select draw",
    "description": "switch draw select, takes draw and select in activ tool",
    "warning": "",
    "wiki_url": "",
    "category": "Animation",
}

# -----------------------------------------------------------------------------------
# creation of an addon that will switch select to draw in Grease Pencil
# -----------------------------------------------------------------------------------

# ---------------------------------------------------
# PROPERTY

# Armature name
bpy.types.WindowManager.bool_select = bpy.props.BoolProperty(name = "select", default=True)
bpy.types.WindowManager.bool_draw = bpy.props.BoolProperty(name = "draw", default=True)

# ---------------------------------------------------
        
# ---------------------------------------------------
#  OP_Switch_GP : switch between select and draw of the grease pencil

def Switch() :
    print("def Switch start")
    
    context = bpy.context
    scene = context.scene
    
    select =context.window_manager.bool_select
    draw =context.window_manager.bool_draw
  
    if context.mode == 'EDIT_GPENCIL':
        bpy.ops.object.mode_set(mode='PAINT_GPENCIL')
        
        if draw :
            bpy.ops.wm.tool_set_by_id(name='builtin_brush.Draw')
        return
        
    elif context.mode == 'PAINT_GPENCIL':
        bpy.ops.object.mode_set(mode='EDIT_GPENCIL')
        
        if select :
            bpy.ops.wm.tool_set_by_id(name='builtin.select_box')
        return
    
            
class OP_Switch_GP (bpy.types.Operator) :
    
    bl_idname = "object.oper_select_draw"
    bl_label = "switch"
    bl_description = "switch select to draw in Grease Pencil"
    
    def execute(self, context) :
        
        Switch()
        
        return {'FINISHED'}

    
# ---------------------------------------------------
#  PANEL GREASE PENCIL SWITCH : SELECT <--> DRAW

class InterfacePanel (bpy.types.Panel) :
    
    bl_label = "Switch Draw <-> select"
#    bl_label = "Switch"
    bl_idname = "VIEW_3D_PT_SELECT_DRAW"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "GP select-draw"
    bl_description = "Grease Pencil switch Draw <-> select"
                                
     # --- draw --- #
        
    def draw(self, context) :
        
        window_m = context.window_manager
        l = self.layout
        c=l.column(align = True)
        
        c.operator(OP_Switch_GP.bl_idname)
        
        c.label(text ="edit mode :")
        c.prop(window_m, "bool_select")
        
        c.label(text ="draw :")
        c.prop(window_m, "bool_draw")
                    
# ---------------------------------------------------
# REGISTER
classes = (InterfacePanel, OP_Switch_GP)

        
def register() :
   
    for cls in classes:
        bpy.utils.register_class(cls)
     
def unregister() :
    
    for cls in classes:
        bpy.utils.unregister_class(cls)
     
if __name__ == "__main__" :
    register()


Cette contribution était de : http://blenderclan.tuxfamily.org/html/newbb/viewtopic.php?forum=2&topic_id=49964&post_id=583266