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 a random value for all of them. How to do this via script?
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 a random value for all of them. How to do this via script?
I'd recommend random.uniform(a,b) for a random float range:
import bpy
import random
obj = bpy.context.object
test if the object is a mesh and check if shape keys have been added
if obj.type == 'MESH' and hasattr(obj.data.shape_keys, "key_blocks"):
# iterate through the shape keys of the object
for shape_key in obj.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.
May be helpful for testing the add-on.
– Paul Gonet
Nov 28 '15 at 15:01
For those who just want to have the Accepted Answer to work for multiple selected objects, do this:
import bpy
import random
iterate through all mesh objects in selection
for obj in [o for o in bpy.context.selected_objects if o.type == 'MESH']:
# iterate through the shape keys of the current object
if hasattr(obj.data.shape_keys, "key_blocks"):
for shape_key in obj.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)
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)