Skip to content

Instantly share code, notes, and snippets.

@p2or
Last active May 21, 2023 18:06
Show Gist options
  • Save p2or/9095a4fd4ef2bb55b2637982c40a4a6d to your computer and use it in GitHub Desktop.
Save p2or/9095a4fd4ef2bb55b2637982c40a4a6d to your computer and use it in GitHub Desktop.
Blender - Toggle Visibility of Modifier by Type using Shift+Q, for https://blender.stackexchange.com/q/46848/ #Blender #BSE
# ##### 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": "Toggle Modifier Visibility",
"description": "Toggle modifier visibility for all objects in selection",
"author": "p2or",
"version": (0, 2),
"blender": (2, 93, 0),
"tracker_url": "https://gist.github.com/p2or/e0568d20cca23c8f5673",
"location": "3D View",
"category": "3D View"
}
import bpy
from bpy.types import Modifier, AddonPreferences, Operator
from bpy.props import EnumProperty
class OBJECT_AP_toggleModifier(AddonPreferences):
bl_idname = __name__
items = [
(p.identifier, p.name, p.description, p.icon, c)
for c, p in enumerate(Modifier.bl_rna.properties["type"].enum_items)
]
modifier_type: EnumProperty(
items=items,
name="Toggle",
default='SUBSURF')
def draw(self, context):
layout = self.layout
layout.prop(self, "modifier_type")
class OBJECT_OT_toggleModifierVisibility(bpy.types.Operator):
bl_idname = "view3d.toggle_modifier_visibility"
bl_label = "Toggle Modifier Visibility"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return len(context.selected_objects)
def execute(self, context):
prefs = context.preferences.addons[__name__].preferences
candidates = [] #candidates.setdefault(ob, []).append(m)
for ob in context.selected_objects:
for m in ob.modifiers:
if m.type == prefs.modifier_type:
candidates.append(m)
for subsurf in candidates:
subsurf.show_viewport = not subsurf.show_viewport
if candidates:
self.report({'INFO'}, "{}: {}".format(
prefs.modifier_type.title(),
['Off','On'][candidates[0].show_viewport]))
return {'FINISHED'}
addon_keymaps = []
def register():
bpy.utils.register_class(OBJECT_AP_toggleModifier)
bpy.utils.register_class(OBJECT_OT_toggleModifierVisibility)
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(OBJECT_OT_toggleModifierVisibility.bl_idname, type='Q', value='PRESS', shift=True)
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(OBJECT_OT_toggleModifierVisibility)
bpy.utils.unregister_class(OBJECT_AP_toggleModifier)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment