Skip to content

Instantly share code, notes, and snippets.

@p2or
Last active May 21, 2023 19:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save p2or/77772f7ddf9bf18108496e741e599e61 to your computer and use it in GitHub Desktop.
Save p2or/77772f7ddf9bf18108496e741e599e61 to your computer and use it in GitHub Desktop.
Subsurf Utils #Blender
# for https://blender.stackexchange.com/q/46848/3710
# ##### 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 #####
bl_info = {
"name": "Subsurf Utilities",
"description": "Subsurf Utilities Pie Menu",
"author": "p2or",
"version": (0, 0, 1),
"blender": (2, 79, 0),
"location": "3D Viewport > Ctrl+Shift+Spacebar",
"category": "Pie Menu"
}
import bpy
# -----------------------------------------------------------------
# Operators
# -----------------------------------------------------------------
class ToggleSimplify(bpy.types.Operator):
"""Toggle Simplify"""
bl_idname = "view3d.toggle_simplify"
bl_label = "Toggle Simplify"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
context.scene.render.use_simplify = not context.scene.render.use_simplify
self.report({'INFO'}, "Simplify: {}"
.format(['Off','On'][context.scene.render.use_simplify]))
return {'FINISHED'}
class ToggleSubsurfVisOperator(bpy.types.Operator):
"""Toggle Visibility of Subsurf Modifiers"""
bl_idname = "view3d.toggle_subsurf_visibility"
bl_label = "Toggle Subsurf Visibility"
bl_options = {'REGISTER', 'UNDO'}
'''
@classmethod
def poll(cls, context):
return context.object.select
'''
def execute(self, context):
obj = context.object
# list of objects subsurf modifiers
subsurf_list = [m for m in obj.modifiers if m.type == "SUBSURF"]
if subsurf_list:
# iterate through the modifiers
for subsurf in subsurf_list:
# toggle show_viewport property
subsurf.show_viewport = not subsurf.show_viewport
self.report({'INFO'}, "Display Subsurf in Viewport: {}"
.format(['Off','On'][subsurf.show_viewport]))
else:
if obj.select: # check if object is selected
obj.modifiers.new(name='Subsurf', type='SUBSURF')
context.area.tag_redraw()
self.report({'INFO'}, "Subsurf Modifier added to {}"
.format(obj.name))
return {'FINISHED'}
# -----------------------------------------------------------------
# Custom Pie Menu
# -----------------------------------------------------------------
class VIEW3D_PIE_custom(bpy.types.Menu):
# label is displayed at the center of the pie menu.
bl_label = "Subsurf Utilities"
bl_idname = "view3d.toggle_subsurf_pie_menu"
def draw(self, context):
layout = self.layout
pie = layout.menu_pie()
# operator_enum will just spread all available options
# for the type enum of the operator on the pie
#pie.operator_enum("mesh.select_mode", "type")
pie.operator("view3d.toggle_subsurf_visibility", icon="GRID")
pie.operator("view3d.toggle_simplify", icon="FILTER")
# -----------------------------------------------------------------
# Register & Unregister
# -----------------------------------------------------------------
addon_keymaps = []
def register():
bpy.utils.register_class(ToggleSimplify)
bpy.utils.register_class(ToggleSubsurfVisOperator)
bpy.utils.register_class(VIEW3D_PIE_custom)
# 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')
kmi = km.keymap_items.new("wm.call_menu_pie", type='SPACE', value='PRESS', shift=True, ctrl=True)
kmi.properties.name = VIEW3D_PIE_custom.bl_idname
addon_keymaps.append((km, kmi))
def unregister():
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
bpy.utils.unregister_class(VIEW3D_PIE_custom)
bpy.utils.unregister_class(ToggleSubsurfVisOperator)
bpy.utils.unregister_class(ToggleSimplify)
if __name__ == "__main__":
register()
# test call
#bpy.ops.wm.call_menu_pie(name="VIEW3D_PIE_custom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment