# can not set the camera viewpoint?

I want to use python to set the camera viewpoint but failed to do so, here is my code to render the model through python

import sys
import os
import bpy
import math
import datetime

if __name__=="__main__":

configureBlender()
setCameraPosition([0.49123,-2.22974,6.74501],[123.942,0.578,0.1])
run(os.path.dirname(os.path.realpath(__file__))+'/test')

def configureBlender():
# rendering constants
RENDER_RES_X = 640
RENDER_RES_Y = 480
FRAME_NO = 1
bpy.context.scene.frame_current = FRAME_NO
bpy.context.scene.render.resolution_x = RENDER_RES_X
bpy.context.scene.render.resolution_y = RENDER_RES_Y
bpy.context.scene.render.use_border = False

def run(output_dir):
bpy.context.scene.render.filepath = output_dir
nodes = bpy.context.scene.node_tree.nodes
nodes['File Output'].base_path = output_dir
bpy.ops.render.render(write_still=True)

def setCameraPosition(location, rotation):
bpy.data.objects['Camera'].location = location


I don't know what is wrong with code or maybe model(anywhere to upload the model, it is 5mb)

update

for obj in bpy.data.objects:
print(obj.name)


it prints out only one Camera object, i guess if there is only one camera, this camera must be the active camera.

Another wield thing is i changed the camera position and saved the model but it is still rendering with the previous viewpoint. so I guess maybe the code is fine but something wrong with the model.

• You can. What's your question about? Rendering, setting up the camera in your scene? Compositor nodes? Command line? Don't get it... please add more context. – brockmann Oct 25 '17 at 10:26
• Rendering through the code is fine however no matter how I set the parameters in the camera viewpoint, the rendered picture still keeps the same camera viewpoint set previously in the model. – user824624 Oct 25 '17 at 11:16
• You really want to set context.scene.camera location and rotation. There may or may not be a camera in the file named "Camera" but it is not always going to be the scene's active camera. – batFINGER Oct 25 '17 at 12:20
• i print out all the objects, and it shows the Camera object with exact the name – user824624 Oct 26 '17 at 2:23
• @batFINGER I have uploaded the model – user824624 Oct 26 '17 at 2:34

Is this the entire script? It looks like you forgot to import required modules.

import os
import math

Edit:

Er, well, I think Blender automatically does import math as *
so you'd remove the preceding math. from radians.

I believe you'd still need to import os, unless I'm mistaken about that point.

Edit You have to define functions before you can run them in Python. Don't know exactly what you're trying to accomplish with node tree in your run function, so I chopped that out for now, but this script at least runs.

import bpy, os
from math import *

# rendering constants
RENDER_RES_X = 640
RENDER_RES_Y = 480
FRAME_NO = 1

def configureBlender():
bpy.context.scene.frame_current = FRAME_NO
bpy.context.scene.render.resolution_x = RENDER_RES_X
bpy.context.scene.render.resolution_y = RENDER_RES_Y
bpy.context.scene.render.use_border = False

def run(output_dir):
bpy.context.scene.render.filepath = output_dir
bpy.ops.render.render(write_still=True)

def setCameraPosition(location, rotation):
bpy.data.objects['Camera'].location = location

• I don't think that this is the issue. (there is no import bpy either) Suggest asking "Is this the entire... " in a comment. – batFINGER Oct 25 '17 at 12:23
• You can import math and math.radians() or from math import * and radians(). If only using radians recommend from math import radians. The fix is your Edit, re where the if __name__ ==... was defined. – batFINGER Oct 26 '17 at 4:42