3
$\begingroup$

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?

$\endgroup$
1

3 Answers 3

5
$\begingroup$

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:

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.

$\endgroup$
5
  • 1
    $\begingroup$ Thanks @poor! The script works great. BTW You've changed avatar, right? Looks more modern that your previous one ;). $\endgroup$
    – Paul Gonet
    Commented Oct 22, 2015 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
    Commented Nov 19, 2015 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
    Commented Nov 19, 2015 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
    Commented Nov 28, 2015 at 15:01
  • 1
    $\begingroup$ To have the first script to work for multiple selected objects: blender.stackexchange.com/a/273932/8282 $\endgroup$ Commented Sep 6, 2022 at 12:56
1
$\begingroup$
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)
$\endgroup$
2
  • $\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
    Commented Oct 21, 2015 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
    Commented Oct 22, 2015 at 15:01
1
$\begingroup$

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)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .