python code for camera.
Posted: Fri Sep 24, 2021 12:52 am
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?
- Code: Select all
# 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")