How to move a camera in Blender 2.61 with Python

后端 未结 3 492
臣服心动
臣服心动 2021-02-04 07:57

I\'m searching for a simple script to move a camera in Blender 2.61 with Python. I thought this would be an easy task, but the Camera object has no properties like loc or someth

相关标签:
3条回答
  • 2021-02-04 07:59

    Perhaps the camera rigs at the bottom of this page could be a nice starting point.

    0 讨论(0)
  • 2021-02-04 08:14

    furtelwart's answer was quite usefull. I did some more digging so you can also set some other very usefull properties regarding the camera and render.

    import bpy
    
    tx = 0.0
    ty = 0.0
    tz = 80.0
    
    rx = 0.0
    ry = 0.0
    rz = 0.0
    
    fov = 50.0
    
    pi = 3.14159265
    
    scene = bpy.data.scenes["Scene"]
    
    # Set render resolution
    scene.render.resolution_x = 480
    scene.render.resolution_y = 359
    
    # Set camera fov in degrees
    scene.camera.data.angle = fov*(pi/180.0)
    
    # Set camera rotation in euler angles
    scene.camera.rotation_mode = 'XYZ'
    scene.camera.rotation_euler[0] = rx*(pi/180.0)
    scene.camera.rotation_euler[1] = ry*(pi/180.0)
    scene.camera.rotation_euler[2] = rz*(pi/180.0)
    
    # Set camera translation
    scene.camera.location.x = tx
    scene.camera.location.y = ty
    scene.camera.location.z = tz
    

    I'm using this script to make batch rendering. You can check it out here: http://code.google.com/p/encuadro/source/browse/renders/marker/model/marker_a4.py

    It will be improved later to take command line arguments. I'm new to python and blender so this may be kind of amateur but it works.

    0 讨论(0)
  • 2021-02-04 08:16

    A friendly user on reddit pointed me to one correct solution: The trick is to retrieve the camera as an Object, not as a Camera. With this way, you can set the location via the standard way and set keyframes.

    If you want to set Camera specific objects, you have to retrieve it via bpy.data.cameras.

    import bpy
    
    if(len(bpy.data.cameras) == 1):
        obj = bpy.data.objects['Camera'] # bpy.types.Camera
        obj.location.x = 0.0
        obj.location.y = -10.0
        obj.location.z = 10.0
        obj.keyframe_insert(data_path="location", frame=10.0)
        obj.location.x = 10.0
        obj.location.y = 0.0
        obj.location.z = 5.0
        obj.keyframe_insert(data_path="location", frame=20.0)
    
    0 讨论(0)
提交回复
热议问题