Skip to content

Instantly share code, notes, and snippets.

@cyaoeu
Created March 24, 2018 14:56
Show Gist options
  • Save cyaoeu/00803f1f29430301801150cbc710ae80 to your computer and use it in GitHub Desktop.
Save cyaoeu/00803f1f29430301801150cbc710ae80 to your computer and use it in GitHub Desktop.
stroke select addon (edited)
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# This script enables a selection method by moving the mouse over the element you want to select.
# Thanks for help with updating the checkbox when using hotkeys goes to the Blender Stack Exchange.
# The core of this script is based on paint select by Kjartan and minikuof
import bpy
from bpy.props import BoolProperty
bl_info = {
'name' : "Stroke Select (cyaoeu edit)",
'author' : "Reiner 'Tiles' Prokein, Henrik Berglund",
'description' : "Stroke Select - Enables selection by mouse strokes",
'category' : "Mesh",
'blender' : (2, 76),
'version' : (1, 0, 1),
'wiki_url' : 'http://www.reinerstilesets.de',
}
# ----------------------------------------------------------------------------------------------------
# Updating the UI when switching the select / deselect flag by hotkey.
# ----------------------------------------------------------------------------------------------------
#def area_ui_update(type):
# ''' tag redraw on areas of type '''
#
# def ui_update(self, context):
# # This part is not necessary it seems
# areas = [a for a in screen.areas if a.type == type]
# for a in areas:
# area.tag_redraw()
# return None
# return ui_update
# ----------------------------------------------------------------------------------------------------
# The core class for the stroke select. See outcommented part at the end of the script for more switches.
# But for our needs it's enough to have the deselect, extend and toggle flag.
# And to work with the tool it's enough to have access to the deselect flag.
# ----------------------------------------------------------------------------------------------------
class StrokeSelect(bpy.types.Operator):
"""Stroke select\nHold down hotkey, stroke select with left mouse button
To toggle between select and deselect hold down hotkey and right click"""
bl_idname = "view3d.stroke_select"
bl_label = "Stroke Select"
bl_options = {'REGISTER', 'UNDO'}
deselect = BoolProperty(name="Deselect", default=False)
extend = BoolProperty(name="Extend", default=False)
def modal(self, context, event):
#wm = context.window_manager # Our bool is in the windows_manager
if self.deselect:
#bpy.context.tool_settings.mesh_select_mode = (False, False, True)
bpy.ops.view3d.select('INVOKE_DEFAULT', extend=False, deselect=True, toggle=False)
elif self.extend:
bpy.ops.view3d.select('INVOKE_DEFAULT', extend=True, deselect=False, toggle=False)
else:
#bpy.context.tool_settings.mesh_select_mode = (False, False, True)
bpy.ops.view3d.select('INVOKE_DEFAULT', extend=True, deselect=False)
if event.value == 'RELEASE':
#bpy.context.tool_settings.mesh_select_mode = (True, False, False)
return {'FINISHED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if not self.extend:
bpy.ops.mesh.select_all(action='DESELECT')
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
# -----------------------------------------------------------------------------------------------------
# the panel in the properties sidebar. Visual feedback in what state the deselect bool currently is
# -----------------------------------------------------------------------------------------------------
class StrokeSelectPanel(bpy.types.Panel):
bl_label = "Stroke Select"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
# Just show when there is an object in the scene
@classmethod
def poll(self,context):
return context.object is not None
# now draw the panel and the checkbox
def draw(self, context):
layout = self.layout
wm = context.window_manager # Our bool is in the windows_manager
#layout.prop(wm, self.deselect) # Our checkbox
# The menu entry in the Select menu. The operator. See also def register for the location
def menu_func(self, context):
self.layout.operator("view3d.stroke_select")
# ------------------------------ register unregister --------------------------------------------------------
# store keymaps here to access after registration
addon_keymaps = []
def register():
bpy.utils.register_module(__name__)
# The menu entry in the Select menu. The locations.
bpy.types.VIEW3D_MT_select_object.append(menu_func)
bpy.types.VIEW3D_MT_select_edit_mesh.append(menu_func)
bpy.types.VIEW3D_MT_select_edit_curve.append(menu_func)
bpy.types.VIEW3D_MT_select_edit_surface.append(menu_func)
bpy.types.VIEW3D_MT_select_edit_lattice.append(menu_func)
bpy.types.VIEW3D_MT_select_edit_armature.append(menu_func)
bpy.types.VIEW3D_MT_select_pose.append(menu_func)
# handle the keymap
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
# the paint select hotkey
kmi = km.keymap_items.new("view3d.stroke_select", type = 'SELECTMOUSE', value = 'PRESS', key_modifier='Q', shift = True)
# the hotkey to toggle select/deselect painting
kmi = km.keymap_items.new("wm.context_toggle", type='ACTIONMOUSE', value='PRESS', key_modifier='Q', shift = True)
kmi.properties.data_path = "window_manager.stroke_select_bool"
addon_keymaps.append((km, kmi))
def unregister():
bpy.types.VIEW3D_MT_view.remove(menu_func)
del bpy.types.WindowManager.stroke_select_bool # Unregister our flag when unregister.
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
# Possible parameters for select. In case somebody wants to extend the script later:
#bpy.ops.view3d.select(extend=False, deselect=False, toggle=False, center=False, enumerate=False, object=False)
# Activate/select item(s)
# Parameters:
# extend (boolean, (optional)) – Extend, Extend selection instead of deselecting everything first
# deselect (boolean, (optional)) – Deselect, Remove from selection
# toggle (boolean, (optional)) – Toggle Selection, Toggle the selection
# center (boolean, (optional)) – Center, Use the object center when selecting, in editmode used to extend object selection
# enumerate (boolean, (optional)) – Enumerate, List objects under the mouse (object mode only)
# object (boolean, (optional)) – Object, Use object selection (editmode only)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment