I have an object at the origin of the global coordinate system. I want to render images of the object from many different randomly chosen viewpoints. How can I randomize the camera position by following these 2 steps:

  1. Pick an octant (e.g. one of the octants is the space in which all points have positive x, positive y, and positive z values) in the 3D coordinate system, then randomize the camera location in that octant, given that the object is at a distance from the object within a pre-defined range.
  2. Automatically point the camera at the object after translation
  • $\begingroup$ What is an octant? $\endgroup$
    – WhatAMesh
    Aug 31 '18 at 15:13
  • $\begingroup$ @WhatAMesh for example one of the octants is the space in which all points have positive x, positive y, and positive z values $\endgroup$
    – AJ Z.
    Aug 31 '18 at 15:29
  • $\begingroup$ @WhatAMesh, with a cube centered at 0,0,0, cut through it along each axis and you get eight cubes, each of these is an octant. $\endgroup$
    – sambler
    Sep 1 '18 at 5:13

I am not quite sure that I have understand what an Octant is, but you may try this:

first create a camera and an empty, then add to your camera a "TrackTo" constraint and set its Target to be the empty.

set camera target

This is how it should look if you move you camera along the Y axis

enter image description here

after finishing your setup this code will pick random points in positive space, you have only to set min and max values so it can avoid overlaps and define the maximum length of your octant as you wish.

import bpy
import random

#pick random point in Octant
def Octant(minX, maxX, minY, maxY, minZ, maxZ):
    rndX = random.uniform(minX, maxX)
    rndY = random.uniform(minY, maxY)
    rndZ = random.uniform(minZ, maxZ)
    return (rndX, rndY, rndZ)

#select your main camera
camera = bpy.context.scene.objects["Camera"]
#set camera random position
camera.location = Octant(1,5, 1,4, 1,8)

it can be also enriched by the same logic so you may have small differences on the focal length and the empty's position too.

  • $\begingroup$ It is easy to also add the constraint with python. $\endgroup$
    – sambler
    Sep 1 '18 at 5:08
  • $\begingroup$ Absolutely, I just tried to avoid anything else would be done easy from the blender's UI. But you are right, I will re-edit this and I ll add a second version also so there can be one more option. $\endgroup$
    – cnisidis
    Sep 1 '18 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.