TransWikia.com

Driver with random output between a range of values ( -1 to 1)

Blender Asked by AxiomDes on September 29, 2021

I’m looking to make a driver that outputs a random value between a specific range (in this case -1 to 1) as a way to randomly change the factor value of the Simple Deform modifier. So that I can duplicate an object, and the factor is a random value, giving unique shapes for each, but within a range of acceptable values.

I’ve taken a look at other questions/answers, but I haven’t found anything that pertains to my situation, or that I don’t understand how it’s being done.

I’m not looking for other workflows or other ways to go about what I’m doing with the modifiers. I’m specifically looking for a way to make a random driver that outputs numbers within a range. Nothing more.
I’m not interested in animation nodes either, not at the moment anyway, so that’s not an acceptable answer either.

I’m not wanting to sound rude or anything.
Please just answer the question as it is in the topic.

Thanks.

2 Answers

It's a bit hacky but you can use the python hash method to generate a "random" number, for example using the name of each object since they must be different from each other.

The caveat is it isn't by default in the driver namespace, but you can very easily add it in. You can choose to permanently add it to the driver namespace for all sessions or just for the current session. More information in this answer.

import bpy

# Uncomment if you want the hash method to be permanently added to the driver namespace
#@bpy.app.handlers.persistent
#def setup_driver(*args):
#    bpy.app.driver_namespace["hash"] = hash

#bpy.app.handlers.load_post.append(setup_driver)
#setup_driver()

# Use this if you want the hash method to be added to the driver namespace for this session only
bpy.app.driver_namespace["hash"] = hash

How to run a script

I believe most string hashes will be very large numbers, so you can divide the resulting hash by a high number and take the modulo (%) by 1 to get a value between 0 and 1.

enter image description here

Example with a driver on the Z coordinate of my cubes with a modulo of 10 that I simply duplicated along the Y axis.

enter image description here

You can manipulate the % value to get the desired range. Example with the simple deform bend modifier where I used (2* pi).

enter image description here

enter image description here

If you want to restrict between 0 and 1, use % 2 - 1

Correct answer by Gorgious on September 29, 2021

Noise module is in the driver namespace

For our convenience the mathutils.noise module is added to the driver namespace by default. As have all members of mathutils and most of math module.

>>> bpy.app.driver_namespace['noise']
<module 'mathutils.noise'>

There are a number of options for randoms, for a simple range (-1.0, 1.0) can type #2 * noise.random() - 1 directly into a non-driven field. The hash # signifies make this a driver.

enter image description here

after, (good some random in range.

enter image description here

Click within the property can edit it directly (notice no hash since it is already a driver, signified by purple)

enter image description here

Note these values will change randomly for each frame. Remove the driver to "keep" a value.

Using self in drivers

Further to my comment re @Gorgious's answer can use self in drivers.

enter image description here

If the driver is on a modifier then self will be the modifier, self.id_data the object the modifier belongs to.

Simple test script, adds a method test to the driver namespace. The object the driver is on is passed to the method. The hash of the object's name is divided by some seed and the remainder returned.

Hash can be either positive or negative, so the values should fall within the range of (-1, 1)

import bpy
from bpy.app import driver_namespace as dns
from math import modf

seed = 203137

def test(ob):
    r = modf(hash(ob.id_data.name) / seed)[0]
    return r

dns["test"] = test

Answered by batFINGER on September 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP