Driver Expression for random?

What scripted expression can I use for a driver, so it would return a random number between 0 and 12 every frame?

round(noise.random()*12)

3 Likes

You can just define a driver call and use that as well. https://vimeo.com/40389198

a great tip, thanks

1 Like

I solved it by using a keyframe, and a noise modifier.

image
Btw, I did try the noise.random() command, as that is what google says, but it does nothing. Gives an error, that it’s not a valid python expression. Really confusing and weird.

1 Like

worked for me

Well, look at the screenshot.
Did I type it wrong or something?..

Yes, I looked at it the first time, and based on that information all I can comment is that I tested the suggested line before posting. “It doesn’t work” is not enough information to even guess of why it doesn’t work for you. There are no hints in the cropped screenshot, no .blend files to troubleshoot with, just the same information twice in written and image form.

The only hint is the look of the panel, which would suggest you’re using 2.8 but that can’t be right, because that would mean you left that information out too and would be wasting everyone’s time.

I explained why it doesn’t work - It says, that it’s not a valid python expression.
And this error message is also visible in the Screenshot.

If the python expression is different in 2.8, you could have easily reach a conclusion that the problem is that I’m using 2.8, and assuming that it would be the same for both 2.79 and 2.8.

So… What’s the expression like in 2.8?

Hello, By default Blender automatically disables custom drivers in order to prevent malicious interventions on one’s computer.
In order to allow Blender to compute noise.random() you should :

  • Apply the driver. It mentions an error and doesn’t work
  • Save the file
  • Go to file>Open recent> Your file
  • Agree to the prompt
  • Click to update dependencies in the driver subpanel if it doesn’t already work

If you wish to disable blender’s prevention system (not recommended if you download blend files online) you can disable it somewhere in the preferences.

Alternatively one can modify the keyframes directly and use a driver noise modifier (like OP suggested) followed by a step interpolation modifier or a limit modifier to limit the wonkiness.

I know the thread is a bit old but I stumbled upon it today via a google search and I managed to solve it that way.

Cheers.

4 Likes

Thank you for the heads up on how to solve it.
Yikes! Sharing info from back then seemed to raw halt because of few patience.

1 Like

Knowledge shall prevail…! :+1:
Unfortunately, there isn’t much information or in-depth tutorials about drivers online, the aforementioned video in this thread is really helpful but it is already 7 years old, and most problems everyone faces lead to this kind of buried threads…!
I think I read or heard somewhere that drivers will be wiped out once everything nodes arrives (don’t quote me though), but until then it is really a struggle to work with sometimes…
Although it is a VERY powerful concept.

Cheers.

Even better, after you enable python scripts as the default you can specify a list of paths where you store untrusted (downloaded, etc.) .blend files, and thus have the best of both worlds:

image

1 Like

Expressions can be finicky. “frame” is an expression, but to activate it in a number slot that doesn’t yet have a driver you have to type “#frame” without quotes of course. Same thing with “noise.random()”. I had to type “#noise.random()” without quotes.

1 Like

You forgot about # – #round(noise.random()*12)