相對較新的Python,所以任何可怕的編碼道歉。在種子內生成隨機數
我也想定義,我通過產生做平面上方的半球隨機攝像頭的位置和角度兩個隨機數字(在我的例子中,u和v)。
但是,調用py.ops.curve.tree_add函數(生成樹)設置某種種子,這意味着我生成的隨機數始終相同。
E.g.在示例代碼中,根據爲basesize/basesplit生成的randint()創建了一系列不同的樹。
但是,對於這些生成的每個唯一樹,隨機數u和v總是相同的。這意味着對於我生成的每個隨機樹,攝像機角度是特定於該樹(並且不是完全隨機的)
我假設這是通過某種子發生的,所以我想知道是否有辦法告訴python生成隨機數並忽略任何種子?
最佳,
實施例的代碼:(進口聯吡啶是用於混合器蟒API模塊)
### 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)
在情況下,它可以幫助,我的函數來生成的球體放置相機然後指向它(爲了簡潔起見,我沒有在這裏包括圖書館/劇本的其餘部分 - 它創建了一個圍繞一個半徑r的定義中心的點並與上述問題分開):
#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()
代碼存在問題:您從'random'導入某些函數,但隨後訪問'random.uniform'。可以使用'import random'命名空間'random',或者從其中導入'uniform'函數,而不要用'random'作爲前綴。 – trincot
啊對不起!它從今天早上的一些修改代碼。我一般只使用random()。 編輯示例中的代碼以反映此 –