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:


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

enter image description here

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.

  • 1
    $\begingroup$ Thanks @poor! The script works great. BTW You've changed avatar, right? Looks more modern that your previous one ;). $\endgroup$ – Paul Gonet Oct 22 '15 at 0:59
  • $\begingroup$ 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! $\endgroup$ – Paul Gonet Nov 19 '15 at 17:03
  • $\begingroup$ 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? ;). $\endgroup$ – Paul Gonet Nov 19 '15 at 21:40
  • $\begingroup$ 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. $\endgroup$ – 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'
        # 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)
  • $\begingroup$ 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. $\endgroup$ – Paul Gonet Oct 21 '15 at 23:06
  • $\begingroup$ 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. $\endgroup$ – Paul Gonet Oct 22 '15 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.