Skip to content

Instantly share code, notes, and snippets.

@Fintan
Created February 3, 2019 18:43
Show Gist options
  • Select an option

  • Save Fintan/2289c27ee7e7208d6c1f3d0714d3441b to your computer and use it in GitHub Desktop.

Select an option

Save Fintan/2289c27ee7e7208d6c1f3d0714d3441b to your computer and use it in GitHub Desktop.
import bpy
import random
import mathutils
def rnd(fromVal, toVal):
return random.randrange(fromVal, toVal) + random.random()
def ranVec(fromVal, toVal):
x = rnd(fromVal, toVal);
y = rnd(fromVal, toVal);
z = rnd(fromVal, toVal);
if rnd(0,1) > 0.5 :
x = -x;
if rnd(0,1) > 0.5 :
y = -y;
if rnd(0,1) > 0.5 :
y = -y;
return mathutils.Vector((x,y,z))
# create a cameras collection if it doesn't already exist
if bpy.data.collections.find('cameras') == -1:
bpy.data.collections.new('cameras')
bpy.data.collections["Collection"].children.link(bpy.data.collections["cameras"])
# create a camera at a random location and add it to the collection
cam = bpy.data.cameras.new("Camera")
cam_ob = bpy.data.objects.new("Camera", cam)
cam_ob.location = ranVec(2,7)
bpy.data.collections['cameras'].objects.link(cam_ob)
# track to the default Cube
trackCube = cam_ob.constraints.new("TRACK_TO")
trackCube.track_axis = "TRACK_NEGATIVE_Z"
trackCube.up_axis = "UP_Y"
trackCube.target = bpy.data.objects['Cube']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment