0
$\begingroup$

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
    links = bpy.context.scene.node_tree.links
    nodes['File Output'].base_path = output_dir
    bpy.ops.render.render(write_still=True)

def setCameraPosition(location, rotation):
    bpy.data.objects['Camera'].location = location
    bpy.data.objects['Camera'].rotation_euler = [math.radians(rotation[0]), math.radians(rotation[1]), math.radians(rotation[2]) ]

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.

here is my model link

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – brockmann Oct 25 '17 at 10:26
  • $\begingroup$ 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. $\endgroup$ – user824624 Oct 25 '17 at 11:16
  • $\begingroup$ 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. $\endgroup$ – batFINGER Oct 25 '17 at 12:20
  • $\begingroup$ i print out all the objects, and it shows the Camera object with exact the name $\endgroup$ – user824624 Oct 26 '17 at 2:23
  • $\begingroup$ @batFINGER I have uploaded the model $\endgroup$ – user824624 Oct 26 '17 at 2:34
-1
$\begingroup$

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
    bpy.data.objects['Camera'].rotation_euler = [radians(rotation[0]), radians(rotation[1]), radians(rotation[2]) ]

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')
$\endgroup$
  • 2
    $\begingroup$ I don't think that this is the issue. (there is no import bpy either) Suggest asking "Is this the entire... " in a comment. $\endgroup$ – batFINGER Oct 25 '17 at 12:23
  • $\begingroup$ bpy is automatic, the way math was used was wrong. $\endgroup$ – Doyousketch2 Oct 26 '17 at 0:59
  • $\begingroup$ 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. $\endgroup$ – batFINGER Oct 26 '17 at 4:42
  • $\begingroup$ You can import it a number of ways. Blender by default uses from math import * My point was: math didn't appear to be imported at all. $\endgroup$ – Doyousketch2 Oct 26 '17 at 7:00
  • 2
    $\begingroup$ Calling me a nit picker doesn't make "Er, well, I think Blender automatically does import math as * so you'd remove the preceding math. from radians. " right. Why not just edit it out. I imagine most use the text editor to run scripts rather than paste them into interactive console. $\endgroup$ – batFINGER Oct 26 '17 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.