7

I have over 300 shapekeys added to my mesh. How can I disable and enable the visibility for all of them using scripting?

Paul Gonet
  • 33,368
  • 17
  • 91
  • 171

2 Answers2

7

Iterate through the shape keys and check the current state of the mute attribute. Since it is a simple bool property which only can be True or False, I'd suggest write a toggle in order to enable or disable all shape keys of the selected object per execution:

import bpy

iterate through the shape keys of the active object

for shape_key in bpy.context.object.data.shape_keys.key_blocks:

# toggle (if true set it to false and vice versa)
shape_key.mute = not shape_key.mute


Considering your last question and especially for a better workflow, I'd recommend writing a small Add-on instead of running code in the editor for each step:

enter image description here

shape_key_extras.py (updated for 2.8+)

# ##### 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": "Shape Key Extras", "description": "", "author": "p2or", "version": (0, 0, 1), "blender": (2, 80, 0), "location": "Properties > Object Data > Shape Keys", "warning": "", "wiki_url": "", "tracker_url": "", "category": "Mesh" }

import bpy import random

from bpy.props import (IntProperty, BoolProperty, FloatProperty, StringProperty, PointerProperty)

from bpy.types import (Operator, PropertyGroup)

-------------------------------------------------------------------

Helper

-------------------------------------------------------------------

def exclude_by_name(char_sequence, name): if char_sequence: char_list = [i.strip() for i in char_sequence.split(",")] if name.startswith(tuple(char_list)): return True else: return False else: return False

-------------------------------------------------------------------

Properties

-------------------------------------------------------------------

class SKE_PG_settings(PropertyGroup):

value: FloatProperty(
    name = "Value",
    description = "Set static value",
    default = 0,
    #min = 0,
    #max =1
    )
random_min: FloatProperty(
    name = "Min",
    description = "Set minimum random value",
    default = 0,
    #min = 0,
    #max =1
    )
random_max: FloatProperty(
    name = "Max",
    description = "Set maximum random value",
    default = 1,
    #min = 0,
    #max =1
    )
apply_enabled: BoolProperty (
    name = "Apply Values to enabled Keys only",
    description = "Apply values to enabled Keys only",
    default = True 
    )
exclude: StringProperty (
    name = "Exclude",
    description = "Exclude by first character",
    default = "#, =, *"
    )

-------------------------------------------------------------------

Operators

-------------------------------------------------------------------

class SKE_OT_enableAll(Operator): bl_idname = "shapekeyextras.enable_all" bl_label = "Enable All" bl_description = "Enable all Shape Keys"

def execute(self, context):       
    for shapekey in context.object.data.shape_keys.key_blocks:
        shapekey.mute = False

    self.report({'INFO'}, "All enabled")     
    return {'FINISHED'}


class SKE_OT_disableAll(Operator): bl_idname = "shapekeyextras.disable_all" bl_label = "Disable All" bl_description = "Disable all Shape Keys"

def execute(self, context):       
    for shapekey in context.object.data.shape_keys.key_blocks:
        shapekey.mute = True

    self.report({'INFO'}, "All Shape Keys disabled")        
    return {'FINISHED'}

class SKE_OT_toggleAll(Operator): bl_idname = "shapekeyextras.toggle" bl_label = "Toggle Mute" bl_description = "Toggle Mute of all Shape Keys"

def execute(self, context):        
    for shapekey in context.object.data.shape_keys.key_blocks:
        shapekey.mute = not shapekey.mute

    self.report({'INFO'}, "Enabled Shape Keys disabled and Disabled Shape Keys enabled")          
    return {'FINISHED'}

class SKE_OT_randomizeValues(Operator): bl_idname = "shapekeyextras.randomize" bl_label = "Randomize Values" bl_description = "Randomize all Shape Key Values"

def execute(self, context):
    scn = context.scene
    ske = scn.shape_key_extras

    if ske.apply_enabled is True:       
        for shapekey in context.object.data.shape_keys.key_blocks:
            exclude_char = exclude_by_name(ske.exclude, shapekey.name)
            if shapekey.name is not 'Basis' and exclude_char is not True:
                if shapekey.mute is False:
                    shapekey.value = random.uniform(ske.random_min, ske.random_max)
    else:
        for shapekey in context.object.data.shape_keys.key_blocks:
            exclude_char = exclude_by_name(ske.exclude, shapekey.name)
            if shapekey.name is not 'Basis' and exclude_char is not True:
                shapekey.value = random.uniform(ske.random_min, ske.random_max)

    self.report({'INFO'}, "Values for Shape Keys generated")
    return {'FINISHED'}

class SKE_OT_applyValues(Operator): bl_idname = "shapekeyextras.apply" bl_label = "Apply Values" bl_description = "Apply a static Value to all Shape Keys"

def execute(self, context):
    scn = context.scene
    ske = scn.shape_key_extras

    if ske.apply_enabled is True:       
        for shapekey in context.object.data.shape_keys.key_blocks:
            exclude_char = exclude_by_name(ske.exclude, shapekey.name)
            if shapekey.name is not 'Basis' and exclude_char is not True:              
                if shapekey.mute is False:
                    shapekey.value = ske.value
    else:
        for shapekey in context.object.data.shape_keys.key_blocks:
            exclude_char = exclude_by_name(ske.exclude, shapekey.name)
            if shapekey.name is not 'Basis' and exclude_char is not True:
                shapekey.value = ske.value

    self.report({'INFO'}, "Value assigned to Shape Keys")        
    return {'FINISHED'}

-------------------------------------------------------------------

UI

-------------------------------------------------------------------

def draw_shapekey_extras(self, context):

scn = context.scene
layout = self.layout
ske = scn.shape_key_extras

layout.separator()
layout.use_property_decorate = False  # No animation.
layout.use_property_split = False

row = layout.row()
row.label(text="Mute State")
col = layout.column(align=True)
rowsub = col.row(align=True)
rowsub.operator(SKE_OT_enableAll.bl_idname, icon="RESTRICT_VIEW_OFF")
rowsub.operator(SKE_OT_disableAll.bl_idname, icon="RESTRICT_VIEW_ON")
col.operator(SKE_OT_toggleAll.bl_idname, icon="FILE_REFRESH")

layout.separator()
row = layout.row()
row.label(text="Set Shape Key Values")
col = layout.column(align=True)
rowsub = col.row(align=True)
rowsub.prop(ske, "random_min")
rowsub.prop(ske, "random_max")
col.operator(SKE_OT_randomizeValues.bl_idname)

row = layout.row()
col = layout.column(align=True)
col.prop(ske, "value")
col.operator(SKE_OT_applyValues.bl_idname)

row = layout.row()
col = layout.column(align=True)
col.prop(ske, "exclude")
col = layout.column(align=True)
col.prop(ske, "apply_enabled")
layout.separator()


-------------------------------------------------------------------

register

-------------------------------------------------------------------

classes = ( SKE_PG_settings, SKE_OT_enableAll, SKE_OT_disableAll, SKE_OT_toggleAll, SKE_OT_randomizeValues, SKE_OT_applyValues, )

def register(): from bpy.utils import register_class for cls in classes: register_class(cls)

bpy.types.DATA_PT_shape_keys.append(draw_shapekey_extras)
bpy.types.Scene.shape_key_extras = PointerProperty(type=SKE_PG_settings)

def unregister(): from bpy.utils import unregister_class for cls in reversed(classes): unregister_class(cls)

bpy.types.DATA_PT_shape_keys.remove(draw_shapekey_extras)
del bpy.types.Scene.shape_key_extras


if name == "main": register()

p2or
  • 15,860
  • 10
  • 83
  • 143
  • 1
    WOW WOW WOW!!! :O. @poor I can't believe my eyes! I am super grateful :D. If I only could give you some of my reputation to thank you. BTW Maybe you should think about selling this add-on on Blender Market. I'll be the first person to buy it and repay you for your effort. You've made my day. Can't wait to test it out :). – Paul Gonet Oct 31 '15 at 23:39
5
import bpy

for key_block in bpy.context.active_object.data.shape_keys.key_blocks:
    key_block.mute = True
Jaroslav Jerryno Novotny
  • 51,077
  • 7
  • 129
  • 218