简体   繁体   中英

Rotate camera in blender using python

I'm using Blender, I have a camera in position (x,y,z) pointing to (rx,ry,rz) and I need to rotate it around (0, 0, 0) (the radius is R) so that it still points to (rx,ry,rz). How can I implement it using python or is there a simple algorithm?

Not an actual algorithm, but may give you some hints.

Create an empty. Create a "Track To" constraint for your camera with the following parameters:

在此处输入图片说明

Set pivot point to "3D cursor":

在此处输入图片说明

Set 3D cursor where you want it. Since it is (0,0,0) in your case, you may simply press Shift + C , it will be set there.

Now you can simply rotate your camera and it will circle around the 3D cursor while pointing at the target:

在此处输入图片说明

You can also do it from Python using bpy.ops.transform.rotate (bear in mind that this method takes radians , not degrees), but you need to do some mumbo-jumbo with contexts, or else it won't rotate around pivot. Basically, this has already been done for you in this answer . The only thing I have to add is that if you have multiple viewports (3D Views, rather), it may not work at first, you just need to set pivot point to "3D cursor" in the others too, because the viewport Python grabs context from is kinda arbitrary. If you have only one, it shouldn't be a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM