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?



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"]


bpy.context.scene.camera = bpy.data.objects["Camera1"]
| improve this answer | |
  • $\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 Jul 17 '19 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$ – Robert Gützkow Jul 17 '19 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 Jul 17 '19 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$ – Robert Gützkow Jul 18 '19 at 9:20

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.