Skip to content

Instantly share code, notes, and snippets.

@MarkC-b3d
Last active May 14, 2018 20:44
Show Gist options
  • Save MarkC-b3d/8751265d9b2474c1ef0eb055ab2925cf to your computer and use it in GitHub Desktop.
Save MarkC-b3d/8751265d9b2474c1ef0eb055ab2925cf to your computer and use it in GitHub Desktop.
snaptools
# ##### 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 #####
# <pep8 compliant>
bl_info = {
"name": "Snaptools",
"description": "Pie Menu For Snapping",
# "author": "pitiwazou, meta-androcto, chromoly, italic (original creators) modifications made my BlendedMarks"
# "version": (0, 1, 0),
"blender": (2, 78, 0),
"location": "3d View",
"warning": "",
"wiki_url": "",
"category": "Snap Pie Menu"
}
import bpy
from bpy.types import (
Menu,
Operator,
)
# Snap Pie Menu - D
class PieSnaping(Menu):
bl_idname = "pie.snapping"
bl_label = "Snap Tools"
def draw(self, context):
layout = self.layout
pie = layout.menu_pie()
# 4 - LEFT
pie.operator("snap.increment", text="Increment", icon='SNAP_INCREMENT')
# 6 - RIGHT
pie.operator("snap.volume", text="Volume", icon='SNAP_VOLUME')
# 2 - BOTTOM
pie.operator("snap.edge", text="Edge", icon='SNAP_EDGE')
# 8 - TOP
pie.prop(context.tool_settings, "use_snap", text="Snap On/Off")
# 7 - TOP - LEFT
pie.operator("snap.alignrotation", text="Align rotation", icon='SNAP_NORMAL')
# 9 - TOP - RIGHT
pie.operator("wm.call_menu_pie", text="Snap Target", icon='SNAP_SURFACE').name = "snap.targetmenu"
# 1 - BOTTOM - LEFT
pie.operator("snap.face", text="Face", icon='SNAP_FACE')
# 3 - BOTTOM - RIGHT
pie.operator("snap.vertex", text="Vertex", icon='SNAP_VERTEX')
class SnapActive(Operator):
bl_idname = "snap.active"
bl_label = "Snap Active"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
ts = context.tool_settings
if ts.use_snap == True:
ts.use_snap = False
elif ts.use_snap == False:
ts.use_snap = True
return {'FINISHED'}
class SnapVolume(Operator):
bl_idname = "snap.volume"
bl_label = "Snap Volume"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
ts = context.tool_settings
if ts.use_snap == False:
ts.use_snap = True
ts.snap_element = 'VOLUME'
if ts.snap_element != 'VOLUME':
ts.snap_element = 'VOLUME'
return {'FINISHED'}
class SnapFace(Operator):
bl_idname = "snap.face"
bl_label = "Snap Face"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
ts = context.tool_settings
if ts.use_snap == False:
ts.use_snap = True
ts.snap_element = 'FACE'
if ts.snap_element != 'FACE':
ts.snap_element = 'FACE'
return {'FINISHED'}
class SnapEdge(Operator):
bl_idname = "snap.edge"
bl_label = "Snap Edge"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
ts = context.tool_settings
if ts.use_snap == False:
ts.use_snap = True
ts.snap_element = 'EDGE'
if ts.snap_element != 'EDGE':
ts.snap_element = 'EDGE'
return {'FINISHED'}
class SnapVertex(Operator):
bl_idname = "snap.vertex"
bl_label = "Snap Vertex"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
ts = context.tool_settings
if ts.use_snap == False:
ts.use_snap = True
ts.snap_element = 'VERTEX'
if ts.snap_element != 'VERTEX':
ts.snap_element = 'VERTEX'
return {'FINISHED'}
class SnapIncrement(Operator):
bl_idname = "snap.increment"
bl_label = "Snap Increment"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
ts = context.tool_settings
if ts.use_snap == False:
ts.use_snap = True
ts.snap_element = 'INCREMENT'
if ts.snap_element != 'INCREMENT':
ts.snap_element = 'INCREMENT'
return {'FINISHED'}
class SnapAlignRotation(Operator):
bl_idname = "snap.alignrotation"
bl_label = "Snap Align rotation"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
ts = context.tool_settings
if ts.use_snap_align_rotation == True:
ts.use_snap_align_rotation = False
elif ts.use_snap_align_rotation == False:
ts.use_snap_align_rotation = True
return {'FINISHED'}
class SnapTargetVariable(Operator):
bl_idname = "object.snaptargetvariable"
bl_label = "Snap Target Variable"
bl_options = {'REGISTER', 'UNDO'}
variable = bpy.props.StringProperty()
@classmethod
def poll(cls, context):
return True
def execute(self, context):
ts = context.tool_settings
ts.snap_target = self.variable
return {'FINISHED'}
# Menu Snap Target - Shift + Tab
class SnapTargetMenu(Menu):
bl_idname = "snap.targetmenu"
bl_label = "Snap Target Menu"
def draw(self, context):
layout = self.layout
pie = layout.menu_pie()
# 4 - LEFT
pie.operator("object.snaptargetvariable", text="Active").variable = 'ACTIVE'
# 6 - RIGHT
pie.operator("object.snaptargetvariable", text="Median").variable = 'MEDIAN'
# 2 - BOTTOM
pie.operator("object.snaptargetvariable", text="Center").variable = 'CENTER'
# 8 - TOP
pie.operator("object.snaptargetvariable", text="Closest").variable = 'CLOSEST'
# 7 - TOP - LEFT
# 9 - TOP - RIGHT
# 1 - BOTTOM - LEFT
# 3 - BOTTOM - RIGHT
# Snap tools - D
classes = (
PieSnaping,
SnapTargetMenu,
SnapActive,
SnapVolume,
SnapFace,
SnapEdge,
SnapVertex,
SnapIncrement,
SnapAlignRotation,
SnapTargetVariable
)
addon_keymaps = []
def register():
for cls in classes:
bpy.utils.register_class(cls)
wm = bpy.context.window_manager
if wm.keyconfigs.addon:
# Snapping
km = wm.keyconfigs.addon.keymaps.new(name='3D View Generic', space_type='VIEW_3D')
kmi = km.keymap_items.new('wm.call_menu_pie', 'D', 'PRESS',)
kmi.properties.name = "pie.snapping"
# kmi.active = True
addon_keymaps.append((km, kmi))
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
km = kc.keymaps['3D View Generic']
for kmi in km.keymap_items:
if kmi.idname == 'wm.call_menu_pie':
if kmi.properties.name == "pie.snapping":
km.keymap_items.remove(kmi)
if __name__ == "__main__":register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment