python code for camera.

Discussions about MakeHuman, Blender and MPFB. It is ok to ask for general Blender support here, even if it isn't directly related to MakeHuman

python code for camera.

Postby b1bb2 » 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")
b1bb2
 
Posts: 12
Joined: Thu Aug 12, 2021 2:48 am

Re: python code for camera.

Postby joepal » Fri Sep 24, 2021 10:09 am

If you know the name of the camera, you can use that to find the relevant object:

Code: Select all
camera_obj = bpy.context.scene.objects["my_primary_camera"]
camera_obj.select_set(True)
bpy.context.scene.objects.active = camera_obj
bpy.ops.object.select_camera()
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4140
Joined: Wed Jun 04, 2008 11:20 am

Re: python code for camera.

Postby b1bb2 » Fri Sep 24, 2021 9:26 pm

Is this what you said to do? It will not work.
Code: Select all
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))
camera_obj = bpy.context.scene.objects["cameranxabouty"]
camera_obj.select_set(True)
bpy.context.scene.objects.active = camera_obj
bpy.ops.object.select_camera()
# 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")

camera_obj = bpy.context.scene.objects["cameranxabouty"]
KeyError: 'bpy_prop_collection[key]: key "cameranxabouty" not found'

In my original code, I tried to add more light:
Code: Select all
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'
b1bb2
 
Posts: 12
Joined: Thu Aug 12, 2021 2:48 am

Re: python code for camera.

Postby b1bb2 » Sat Sep 25, 2021 6:45 am

I fixed it. There are still a few minor bugs. Thanks for your help.
Code: Select all
# 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")
b1bb2
 
Posts: 12
Joined: Thu Aug 12, 2021 2:48 am


Return to Blender and MPFB

Who is online

Users browsing this forum: No registered users and 1 guest

cron