0
$\begingroup$

Id like to execute different action on the first run of the script

for example the first try i want to execute:

Print("First Hello")

and on the second run.

Print("Hello World")

i don't understant how to do this, because if i store a variable within the script itself every run the variable will be redefined.

i tried something like this, playing with a new stored variable within blender,

    try:
        bpy.context.scene["Auto-Copy Start"] += 1

    except:
        bpy.context.scene["Auto-Copy Start"] = 1

and it can totally work. for example

try:
    bpy.context.scene["Auto-Copy Start"] += 1
    Print("Hello World")


except:
    bpy.context.scene["Auto-Copy Start"] = 1 #that mean its the first run
    Print("First Hello")

But if i save the file and re-run my script it will not work anymore with the saved file, this technique work only for the first run once per file only.

Or i want the first run to execute the first code per blender session. Is this possible ?

$\endgroup$

1 Answer 1

2
$\begingroup$

Use the window manager

Window manager properties only exist for a session, whereas scene properties are stored in the file.

It's good practice to avoid try except clauses. If a custom property "xxx" on object ob is defined, it will be a member of ob.keys() and can be tested for with "xxx" in ob.keys()

import bpy
context = bpy.context
wm = context.window_manager

if "new" in wm.keys():
    print("Not New")
    wm["new"] += 1
else:
    wm["new"] = 1
    print("New")

May also be an idea to investigate using a non persistent on-load handler. (Remove the decorator from the example)

$\endgroup$

You must log in to answer this question.

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