python code for camera.

python code for camera.
I am learning bpy. Here is code I put together that takes picture of object at origon, from cameras on axis. Some cameras are missing. More light needs to be added. The code works, but I do not understand some of it. Your comments are appreciated. How can a specific camera be selected?

# camera.bpy  b1bb2  2021 09 23
# blender/2.93.1/extract/blender-2.93.1-linux-x64/blender -P blender/2.93.1/bpy/camera.bpy

# in GUI:
# x axis is red
# y axis is green
# z axis is not shown
# positive x is at bottom of screen, near right side of screen.
# negative x is at top of screen, near left side of screen.
# origon is near center of screen
# -pi/2 radians = -90 degrees
# pi/4 radians = 45 degrees
# positive z is at top of screen
# negative z is at bottom of screen

import bpy
from math import pi

# take a picture of start, before we change it.
bpy.context.scene.render.filepath='blender/2.93.1/png/camera/start.png'
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type='CAMERA')
bpy.ops.object.delete()

# more light is needed.
# here are some ways to point camera toward origon:

# locate camera on positive x axis. rotate camera pi/2 radians about y axis.
bpy.ops.object.camera_add(location=(+10.0, 0.0, 0.0), rotation=(0.0, +pi/2, 0.0))
camerapxabouty=bpy.context.active_object
bpy.context.scene.camera=camerapxabouty
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath='blender/2.93.1/png/camera/camerapxabouty.png'
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type='CAMERA')
bpy.ops.object.delete()

# locate camera on negative x axis. rotate camera -pi/2 radians about y axis.
bpy.ops.object.camera_add(location=(-10.0, 0.0, 0.0), rotation=(0.0, -pi/2, 0.0))
cameranxabouty=bpy.context.active_object
bpy.context.scene.camera=cameranxabouty
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath='blender/2.93.1/png/camera/cameranxabouty.png'
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type='CAMERA')
bpy.ops.object.delete()

# locate camera on negative x axis. rotate camera pi/4 radians about x axis.
bpy.ops.object.camera_add(location=(-10.0, 0.0, 0.0), rotation=(+pi/4, 0.0, 0.0))
cameranxaboutx=bpy.context.active_object
bpy.context.scene.camera=cameranxaboutx
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath='blender/2.93.1/png/camera/cameranxaboutx.png'
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type='CAMERA')
bpy.ops.object.delete()

# locate camera on positive z axis. rotate camera 0 radians about all axis.
bpy.ops.object.camera_add(location=(0.0, 0.0, +10.0), rotation=(0.0, 0.0, 0.0))
camerapz=bpy.context.active_object
bpy.context.scene.camera=camerapz
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath='blender/2.93.1/png/camera/camerapz.png'
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type='CAMERA')
bpy.ops.object.delete()

# locate camera on negative z axis. rotate camera pi radians about x axis.
bpy.ops.object.camera_add(location=(0.0, 0.0, -10.0), rotation=(+pi, 0.0, 0.0))
cameranzaboutx=bpy.context.active_object
bpy.context.scene.camera=cameranzaboutx
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath='blender/2.93.1/png/camera/cameranzaboutx.png'
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type='CAMERA')
bpy.ops.object.delete()

# locate camera on negative z axis. rotate camera pi radians about y axis.
bpy.ops.object.camera_add(location=(0.0, 0.0, -10.0), rotation=(0.0, +pi, 0.0))
cameranzabouty=bpy.context.active_object
bpy.context.scene.camera=cameranzabouty
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath='blender/2.93.1/png/camera/cameranzabouty.png'
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type='CAMERA')
bpy.ops.object.delete()

print("end camera.bpy")

You can try Selecting the Camera by name and, Create them with different names. You can differentiate the name by just adding a number to the end of it. For Selecting, something like this should work.

bpy.data.objects[‘Camera_1’].select_set(True)

for adding camera

bpy.ops.object.camera_add(name=‘Camera’_ + str(i) ,location=(-10.0, 0.0, 0.0), rotation=(0.0, -pi/2, 0.0))

i += 1

Here is the code you gave me:

import bpy
from math import pi

# locate camera on negative x axis. rotate camera -pi/2 radians about y axis.
# bpy.ops.object.camera_add(location=(-10.0, 0.0, 0.0), rotation=(0.0, -pi/2, 0.0))
bpy.ops.object.camera_add(name='Camera'_ + str(i),location=(-10.0, 0.0, 0.0), rotation=(0.0, -pi/2, 0.0))i+= 1
bpy.data.objects['Camera_1'].select_set(True)
cameranxabouty=bpy.context.active_object
bpy.context.scene.camera=cameranxabouty
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath='/home/bullseye/blender/2.93.1/png/test/cameranxabouty.png'
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type='CAMERA')
bpy.ops.object.delete()
print("end test.bpy")

I tried to fix it:
bpy.ops.object.camera_add(name=‘Camera’_ + str(i),location=(-10.0, 0.0, 0.0), rotation=(0.0, -pi/2, 0.0))i+= 1
line 9 error: _ (underscore)

bpy.ops.object.camera_add(name=‘Camera’ + str(i),location=(-10.0, 0.0, 0.0), rotation=(0.0, -pi/2, 0.0))i+= 1
line 9 SyntaxError: invalid syntax i+= 1

bpy.ops.object.camera_add(name=‘Camera’ + str(i),location=(-10.0, 0.0, 0.0), rotation=(0.0, -pi/2, 0.0))
line 9 NameError: name ‘i’ is not defined

In my original code, I tried to name that camera cameranxabouty.
cameranxabouty.png has grey border around black object. It needs more light.

In my original code, I tried to add more light:
light = bpy.data.objects[“Lamp”]
light.location = (-12, 0, 0)
look_at(light, Vector((0.0, 0, 0)))
bpy.data.lamps[‘Lamp’].type = “HEMI”
bpy.data.lamps[‘Lamp’].energy = 10
bpy.data.lamps[‘Lamp’].use_specular = False
bpy.data.lamps[‘Lamp’].use_diffuse = True

light = bpy.data.objects[“Lamp”]
KeyError: ‘bpy_prop_collection[key]: key “Lamp” not found’

I fixed it. It works better now. Still a few minor bugs. Thanks for your help.

camera.bpy b1bb2 2021 09 24

/home/bullseye/blender/2.93.1/extract/blender-2.93.1-linux-x64/blender -P /home/bullseye/blender/2.93.1/bpy/camera.bpy

in GUI:

x axis is red

y axis is green

z axis is not shown

positive x is at bottom of screen, near right side of screen.

negative x is at top of screen, near left side of screen.

origon is near center of screen

-pi/2 radians = -90 degrees

pi/4 radians = 45 degrees

positive z is at top of screen

negative z is at bottom of screen

import bpy
from math import pi

take a picture of start, before we change it.

bpy.context.scene.render.filepath=‘/home/bullseye/blender/2.93.1/png/camera/start.png’
bpy.ops.render.render(write_still=True)

add two cones to make a good scene

bpy.ops.mesh.primitive_cone_add(location=(2.0, 0.0, 0.0), scale=(1.0, 1.0, 1.0))
bpy.ops.mesh.primitive_cone_add(location=(-2.0, 0.0, 0.0), rotation=(pi/4, 0.0, 0.0), scale=(1.0, 1.0, 1.0))
bpy.context.scene.render.filepath=‘/home/bullseye/blender/2.93.1/png/camera/cones.png’
bpy.ops.render.render(write_still=True)

remove default light

bpy.ops.object.select_by_type(type=‘LIGHT’)
bpy.ops.object.delete()

remove default camera

bpy.ops.object.select_by_type(type=‘CAMERA’)
bpy.ops.object.delete()

more cameras are needed.

object names should be used.

here are some ways to point camera toward origon:

locate camera on positive x axis. rotate camera pi/2 radians about y axis.

bpy.ops.object.light_add(location=(+12.0, 0.0, 0.0))
bpy.context.object.data.energy = 500
bpy.context.object.data.name=“light_px”
bpy.ops.object.camera_add(location=(+10.0, 0.0, 0.0), rotation=(0.0, +pi/2, 0.0))
camerapxabouty=bpy.context.active_object
bpy.context.scene.camera=camerapxabouty
bpy.context.object.data.name=“camerapxabouty”
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath=‘/home/bullseye/blender/2.93.1/png/camera/camerapxabouty.png’
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type=‘LIGHT’)
bpy.ops.object.delete()
bpy.ops.object.select_by_type(type=‘CAMERA’)
bpy.ops.object.delete()

locate camera on negative x axis. rotate camera -pi/2 radians about y axis.

bpy.ops.object.light_add(location=(-12.0, 0.0, 0.0))
bpy.context.object.data.energy = 500
bpy.context.object.data.name=“light_nx”
bpy.ops.object.camera_add(location=(-10.0, 0.0, 0.0), rotation=(0.0, -pi/2, 0.0))
cameranxabouty=bpy.context.active_object
bpy.context.scene.camera=cameranxabouty
bpy.context.object.data.name=“cameranxabouty”
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath=‘/home/bullseye/blender/2.93.1/png/camera/cameranxabouty.png’
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type=‘LIGHT’)
bpy.ops.object.delete()
bpy.ops.object.select_by_type(type=‘CAMERA’)
bpy.ops.object.delete()

locate camera on positive z axis. rotate camera 0 radians about all axis.

bpy.ops.object.light_add(location=(0.0, 0.0, +12.0))
bpy.context.object.data.energy = 500
bpy.context.object.data.name=“light_pz”
bpy.ops.object.camera_add(location=(0.0, 0.0, +10.0), rotation=(0.0, 0.0, 0.0))
camerapz=bpy.context.active_object
bpy.context.scene.camera=camerapz
bpy.context.object.data.name=“camerapz”
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath=‘/home/bullseye/blender/2.93.1/png/camera/camerapz.png’
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type=‘LIGHT’)
bpy.ops.object.delete()
bpy.ops.object.select_by_type(type=‘CAMERA’)
bpy.ops.object.delete()

locate camera on negative z axis. rotate camera pi radians about x axis.

bpy.ops.object.light_add(location=(0.0, 0.0, -12.0))
bpy.context.object.data.energy = 500
bpy.context.object.data.name=“light_nz”
bpy.ops.object.camera_add(location=(0.0, 0.0, -10.0), rotation=(+pi, 0.0, 0.0))
cameranzaboutx=bpy.context.active_object
bpy.context.scene.camera=cameranzaboutx
bpy.context.object.data.name=“cameranzaboutx”
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath=‘/home/bullseye/blender/2.93.1/png/camera/cameranzaboutx.png’
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type=‘LIGHT’)
bpy.ops.object.delete()
bpy.ops.object.select_by_type(type=‘CAMERA’)
bpy.ops.object.delete()

locate camera on negative z axis. rotate camera pi radians about y axis.

bpy.ops.object.light_add(location=(0.0, 0.0, -12.0))
bpy.context.object.data.energy = 500
bpy.context.object.data.name=“light_nz”
bpy.ops.object.camera_add(location=(0.0, 0.0, -10.0), rotation=(0.0, +pi, 0.0))
cameranzabouty=bpy.context.active_object
bpy.context.scene.camera=cameranzabouty
bpy.context.object.data.name=“cameranzabouty”
bpy.ops.object.select_camera()
bpy.context.scene.render.filepath=‘/home/bullseye/blender/2.93.1/png/camera/cameranzabouty.png’
bpy.ops.render.render(write_still=True)
bpy.ops.object.select_by_type(type=‘LIGHT’)
bpy.ops.object.delete()
bpy.ops.object.select_by_type(type=‘CAMERA’)
bpy.ops.object.delete()

print(“end camera.bpy”)