Making registered script "permanent"

I have a small script to have random functions available, and it works fine. But I have to load and run the script for each new Blend file that I create. I do register it, so it loads with each file (I think). Is there a way to have this little script open automatically whenever I start Blender?

Is it an addon? If not, making it an addon would probably be the easiest way.

No, it’s a small script I found online to add some random functions to use for drivers. I have no idea how to make it be an add-on. I know that for a new .blend file, if I want to use it, I have to open a text editor, paste it in from the text file I saved, then make sure script running is enabled in preferences. Then I have to run the script and then click “Register”. After that, it loads with the .blend file the next time I open it. It works, but would be nice to just have it always available every time I start Blender.

[EDIT]: Could I just save this little script in the 2.78 startup folder, just as it’s written? Or would that create problems?

Yeah, addon would be the easiest way imo. Post the code.

You can save the script running in your preferences to have it always on too.

It’s this script that was in a tutorial:

import bpy
import random

Random floating point number between lo and hi

def randf(lo, hi):
return random.uniform(lo, hi)

Random integer from lo (inclusive) to hi (inclusive)

def randi(lo, hi):
return random.randint(lo, hi)

Random values given mean and standard deviation

def gauss(mean, stdev):
return random.gauss(mean, stdev)

bpy.app.driver_namespace[“randf”] = randf
bpy.app.driver_namespace[“randi”] = randi
bpy.app.driver_namespace[“gauss”] = gauss

(Note: the three lines that start with “return” are indented in the file but doesn’t show here.)

Could I just save this little script in the 2.78 startup folder, just as it’s written? Or would that create problems?

Anyone have any info?

If it runs with no errors, you can put it in your startup folder.

OK Thanks. I’ll try that.

Surround your code with [noparse]

 and 

[/noparse] tags and then it’ll be shown properly.

how would you even make an addon for driver functions? i just posted a question asking about that as i am having trouble getting it to work
https://blenderartists.org/forum/showthread.php?413988-how-to-make-an-addon-with-custom-driver-function&p=3140274#post3140274

cmomoney, I put it in the startup folder, and no errors, however I still have to run the script myself from a copy, or else it doesn’t work.

I tested, its working, but it may be working too soon. Try this instead:


import bpy
import random
from bpy.app.handlers import persistent




# Random floating point number between lo and hi


def randf(lo, hi):
    return random.uniform(lo, hi)


# Random integer from lo (inclusive) to hi (inclusive)


def randi(lo, hi):
    return random.randint(lo, hi)


# Random values given mean and standard deviation


def gauss(mean, stdev):
    return random.gauss(mean, stdev)


@persistent
def add_namespace(dummy):
    bpy.app.driver_namespace["randf"] = randf
    bpy.app.driver_namespace["randi"] = randi
    bpy.app.driver_namespace["gauss"] = gauss
    
bpy.app.handlers.load_post.append(add_namespace)

I will try it. Thanks. Do I still need to have “autorun scripts” checked in my preferences though?