Generating random numbers inside seeds

Relatively new to python, so apologize for any terrible coding.

I am using blender to generate random sets of stimuli using soot addition to create something like enter image description here

I also want to determine a random camera position and an angle in a hemisphere above a plane, which I do by generating two random numbers (u and v in my example below).

However, calling the py.ops.curve.tree_add function (which generates the tree) sets some sort of seed, which means the arbitrary numbers I produce are always the same.

eg. in the example code, it creates a number of different trees depending on randint () generated for baseize / basesplit.

However, for every unique tree they generate, the random numbers u and v are always the same. This means that for every arbitrary tree I generate, the camera angle is specific to that tree (and not completely random)

I'm guessing this is happening through some seed, so I was wondering if there is a way to tell python to generate a random number and ignore any seeds?

Best,

Example code: (import bpy is a python api module for blender)

### libraries
import bpy
from random import random, randint

u = random()
v = random()
obj = bpy.ops.curve.tree_add(bevel = True,
                                prune = True,
                                showLeaves = True,
                                baseSize = randint(1,10)/10,
                                baseSplits = randint(0,4))
print(u)
print(v)

      

in case it helps, my function is to generate a sphere to place the camera and then point it to the object (I haven't included libraries / rest of the script, etc. here for brevity - it creates a point around a certain center that has a radius r and works separately from the above problem):

#generate the position of the new camera
def randomSpherePoint(sphere_centre, r, u, v):
    theta = 2 * pi * u
    phi = acos(2 * v - 1)
    x = centre[0] + (r * sin(phi) * cos(theta))
    y = centre[1] + (r * sin(phi) * sin(theta))
    z = fabs(centre[2] + (r * cos(phi)))
    return(x,y,z)

hemisphere_point = randomSpherePoint(centre, radius, u, v)
print(hemisphere_point)
#add a camera at this randomly generated hemispheric location
bpy.ops.object.camera_add(location = hemisphere_point)
the_camera = bpy.data.objects["Camera"]
#rotate the camera to the centre of the plane
camera_direction = centre - camera_location
camera_rotation = camera_direction.to_track_quat('-Z', 'Y')
the_camera.rotation_euler = camera_rotation.to_euler()

      

+3


source to share


2 answers


You can create a random use case for the random.Random class. An example would be:

randomgen = random.Random()
randomgen.uniform(0,1)

      

This reason is as follows:



The functions provided by this module are actually bound methods of the hidden instance of the random.Random class. You can create your own Random instances to generate generators that don't share state

(from https://docs.python.org/3/library/random.html )

+3


source


Python provides a random moduleseed()

to set the seed.

import random
random.seed(12)
random.randint(0,100)

      



Another option for getting variations in your trees is to provide a different garden add-on seed to work with. You can find it in the operator settings panel above the tree scale, the python API also accepts a semantic parameter.

bpy.ops.curve.tree_add(seed=myseed)

      

0


source







All Articles