mardi 4 avril 2017

Generate Random Numbers Within A Seed

Relatively new to python so apologies for any terrible coding.

I'm using blender to create random stimuli sets using the sapling add on to create something likeenter image description here

I also want to define a random camera position and angle in a hemisphere above the plane which I do by producing 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 kind of seed which means that the random numbers I produce are always the same.

E.g. in the example code it creates a range of different trees depending on the randint() generated for basesize/ basesplit.

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

I assume this occurs via 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 the python api module for blender)

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

u = random.uniform(0,1)
v = random.uniform(0,1)
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 to generate a sphere to place the camera and then point it towards the object is:

#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()




Aucun commentaire:

Enregistrer un commentaire