2
$\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 the random value for all of them . How to do it using scripting?

$\endgroup$
2
$\begingroup$

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 shapeKey in bpy.context.object.data.shape_keys.key_blocks:

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

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

        # print the values
        print (shapeKey.value)

The values should look like this:

0.5418003797531128
0.4634256660938263
0.77964186668396
0.37678709626197815
0.8787218332290649
...

For a better control and workflow 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$
  • 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
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$
  • $\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$ Thanks, just a quick note: The code above iterates through, and randomizes, all shapekeys in the blend file (bpy.data.shape_keys). poor's code randomizes every shapekey on the context object. (object.shape_keys). $\endgroup$ – batFINGER Oct 22 '15 at 8:37
  • $\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
  • $\begingroup$ Little harsh, lol. Changing line 4 to "for sk in context.object.data.shape_keys:" would have done trick too... $\endgroup$ – batFINGER Oct 22 '15 at 15:17

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.