0
$\begingroup$

I would like to keep the memory of a scene in order to reproduce a documentation without the original Blender file. In the scene, I have several cameras, 6 by default, but users can add depending of the context. Camera are set as child of a cube in order to place each one easily in a coherent way around the object I want to document. So, getting the location through bpy.data.objects["Camera"].location[0] is not relevant...

My idea is to snap the 3D cursor to selected object, so locations of 3d cursor = locations of a camera. but how to insert this inside a loop to get locations of every camera? I begin with something like this, but I am not so fluent in Python... :/

select camera
for obj in bpy.data.objects:
  if obj.type == 'CAMERA':
    # how can I save the list in a variable?
    bpy.data.objects["variable"].select = True
    # move 3d cursor to selected object
    bpy.ops.view3d.snap_cursor_to_selected()
    # get location of 3d cursor
    cursor_x = bpy.data.scenes["Scene"].cursor.location[0]
    cursor_y = bpy.data.scenes["Scene"].cursor.location[1]
    cursor_z = bpy.data.scenes["Scene"].cursor.location[2]

Thank you for your help :)

$\endgroup$
1
  • 1
    $\begingroup$ This doesn't make a lot of sense to me, seems like your doing circles around what you really wish to accomplish. What is your real goal here? Moving the 3D cursor inside a for loop will make it jump real quick over a bunch of objects, which doesn't seem productive. Why snap it in the first place, why not print the camera object coordinates directly instead? $\endgroup$ Oct 13, 2021 at 12:24

1 Answer 1

2
$\begingroup$

You can get the location in world space through obj.matrix_world.

for obj in bpy.data.objects:
    if obj.type == 'CAMERA':
        loc = obj.matrix_world.to_translation()
        print(obj.name, loc)
$\endgroup$
1
  • $\begingroup$ oh... so it's much easier than I thought!!! Thank you! :) $\endgroup$
    – ValiGrimO
    Oct 13, 2021 at 13:51

You must log in to answer this question.

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