2
$\begingroup$

I want to change camera views using python. I changed it using the gui, and then go to the "info" window log to see what the python commands are for that. But when I cut+paste that into the "Python Console", I get this error:

>>> bpy.context.space_data.camera = bpy.data.objects["Camera1"]
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'SpaceConsole' object has no attribute 'camera'

Any hints on how to change camera views using python?

Thanks.

$\endgroup$

1 Answer 1

3
$\begingroup$

You can set the camera in the current scene by using bpy.context.scene.camera. So your script should be:

bpy.context.scene.camera = bpy.context.scene.objects["Camera1"]

or

bpy.context.scene.camera = bpy.data.objects["Camera1"]
$\endgroup$
4
  • $\begingroup$ Using Blender 2.80, I switch to "Scripting" workspace and type your commands in the python Console window but I didn't see anything change in the 3D viewport. $\endgroup$
    – L. Phan
    Commented Jul 17, 2019 at 17:45
  • $\begingroup$ @L.Phan I may have misunderstood your question. Do you want to switch to the camera perspective in the viewport? I thought you wanted to switch to another camera for rendering (setting the object as current camera). $\endgroup$ Commented Jul 17, 2019 at 17:49
  • $\begingroup$ Yes, I want to switch the camera perspective in the viewport using python. I am sending commands to Blender from an external program using sockets and one of the commands is to switch camera perspective. $\endgroup$
    – L. Phan
    Commented Jul 17, 2019 at 20:34
  • $\begingroup$ @L.Phan I'm not sure how this would be useful for an external program, since it's only for visualization (it doesn't change anything for the rendering of the scene), but the following link should help you blender.stackexchange.com/questions/30643/… $\endgroup$ Commented Jul 18, 2019 at 9:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .