# How to set a random value for multiple shape keys?

My question is related to my previous asked one: How to set a specific value for multiple shape keys?

I have about 300 shapekeys and want to set the random value for all of them . How to do it using scripting?

I'd recommend random.uniform(a,b) for a random float range:

import bpy
import random

# iterate through the shape keys of the context object
for shape_key in bpy.context.object.data.shape_keys.key_blocks:

# check if it's not the first one
if shape_key.name is not 'Basis':

# assign a random float between 0 and 1
shape_key.value = random.uniform(0, 1)

# print the values
print (shape_key.value)


The values should look like this:

0.5418003797531128
0.4634256660938263
0.77964186668396
0.37678709626197815
0.8787218332290649
...


For better access, I've implemented this functionality into this Add-on:

Exclude input field checks whether the 'name' of the shape key starts with 'your input' and also accepts multiple values (comma seperated) in order to ignore placeholder shape keys like
--- Test --- or ### Test ### via *, - at once.

• Thanks @poor! The script works great. BTW You've changed avatar, right? Looks more modern that your previous one ;). – Paul Gonet Oct 22 '15 at 0:59
• The script works really great :D!!! Could you please improve it adding the following options: 'randomize shapekeys visibility', 'add/remove drivers to/from visible/all shapekeys', 'apply shapekeys to left/right side of the mesh (as you can do in Blend window in ShapeKeys panel)'. Thanks again! – Paul Gonet Nov 19 '15 at 17:03
• Here you are: s11.postimg.org/js6bzdm5v/proposal_3_OK.jpg Not much has changed according to the UI you proposed though. One thing I would like to add is a reversed functionallity of the 'exclude' option (fantastic idea BTW :)), which may allow you to add values to only defined shape keys. The options I proposed are everything I need... I guess? ;). – Paul Gonet Nov 19 '15 at 21:40
• I've posted a comment 3 days ago on a GitHub with some changes proposal. Have you checked it? Also, here is a simple character basemesh with some shape keys: <img src="http://blend-exchange.giantcowfilms.com/embedImage.png?bid=705" /> May be helpful for testing the add-on. – Paul Gonet Nov 28 '15 at 15:01
import bpy
from random import random, uniform

for sk in bpy.data.shape_keys:
for i, kb in enumerate(sk.key_blocks.values()):
if not i:
# assume kb[0] is 'Basis'
continue
# set random value
kb.value = kb.slider_min + random() * (kb.slider_max - kb.slider_min)
# eqivalent using random.uniform suggested by poor.
#kb.value = uniform(kb.slider_min, kb.slider_max)

• Thanks a lot guys! You're incredible :O. Both scripts work super fine. As the @batFINGER came first with the script, I accept his answer. – Paul Gonet Oct 21 '15 at 23:06
• Sorry, but I have to uncheck the 'accept' button. poor 's solution is the one I wanted. Your solution'll be helpful for my future projects though, so thanks once again. – Paul Gonet Oct 22 '15 at 15:01