Set in invoke method.
If I understand correctly you wish to only set the random seed once when the operator is invoked.
The method is employed in this answer to only find the loose parts of a mesh once.
Altering properties only fires the execute method.
Adding a button to self layout.operator("test.random") in draw method will re-invoke and set a new random.
Here is simple operator example. Invoking sets the random property.
import bpy
import bmesh
from random import random
from bpy.props import (
FloatProperty,
)
class TEST_OT_random(bpy.types.Operator):
"""Add a simple box mesh"""
bl_idname = "test.random"
bl_label = "Random"
bl_options = {'REGISTER', 'UNDO'}
random = random()
width: FloatProperty(
name="Width",
description="Box Width",
min=0.01, max=100.0,
default=1.0,
)
height: FloatProperty(
name="Height",
description="Box Height",
min=0.01, max=100.0,
default=1.0,
)
def invoke(self, context, event):
self.random = random()
print("invoke", self.random)
return {'FINISHED'}
# or to run after invoke
return self.execute(context)
def execute(self, context):
print(
"execute:",
self.width,
self.height,
self.random,
)
return {'FINISHED'}
def draw(self, context):
layout = self.layout
col = layout.column()
col.prop(self, "width")
col.prop(self, "height")
col.operator("test.random", text="Re-random")
def register():
bpy.utils.register_class(TEST_OT_random)
def unregister():
bpy.utils.unregister_class(TEST_OT_random)
if __name__ == "__main__":
register()
# test calls
bpy.ops.test.random()
bpy.ops.test.random(width=3)
bpy.ops.test.random('INVOKE_DEFAULT')
Test run output
execute: 1.0 1.0 0.6802742102341347
execute: 3.0 1.0 0.6802742102341347
invoke 0.5433648186966845
Running from UI, sliding the width property
invoke 0.9656296014544301
execute: 1.059999942779541 1.0 0.9656296014544301
execute: 1.1200000047683716 1.0 0.9656296014544301
execute: 1.2999999523162842 1.0 0.9656296014544301
execute: 1.3299999237060547 1.0 0.9656296014544301
execute: 1.3600000143051147 1.0 0.9656296014544301
execute: 1.3299999237060547 1.0 0.9656296014544301
aternatively, remove invoke method above to have a random seed set only when addon is registered
random.seed(0)to set the seed. – Leander Apr 25 '19 at 08:55