0
$\begingroup$

I'm writing a simple script to rename an array of cameras to match a sequence on a grid so that I can plot GPS co-ordinate to them. See the screenshot below where I show what I want to happen.

I'm looking to rename the array of cameras so that they follow this pattern

1 2 3
4 5 6

But, when I run the script, invariably it outputs this:

1 2 3
6 3 4

Here's the .blend file example

https://drive.google.com/drive/folders/1emGQRb6LgLambTk0Y_7vIjouZ62gNbHq?usp=sharing.

When you hit "run" script, you'll it happen.

It doesn't matter how extensive the array is, the second, third fourth (and so on) rows start with the number which should end the row sequence, e.g. This is the output I get with an example larger array.

1   2  3  4  5 
10  6  7  8  9 
15  11 12 13 14 

Here is the script that I am working on:

import bpy
cameras = bpy.context.selected_objects
TOL = 0.0001
if cameras:
    cameras = sorted(cameras, key = lambda o: (-o.location.y, o.location.x))
    y = cameras[0].location.y
    for i, camera in enumerate(cameras):
        if abs(camera.location.y - y) > TOL:
            z = camera.location.y
        camera.name = "%d" % (i+1)

This should have been renamed "4"

enter image description here

$\endgroup$
6
  • $\begingroup$ if this [1]: i.sstatic.net/CRnCJ.png isn't what you want, you should clearly tell, what you want. A sketch with (!) camera names would be great. So for me it looks like your code works (beside rounding issues). And including a blend file would also be great so we can see what you are testing. And you didn't even show us how you got that result (maybe a print statement?) $\endgroup$
    – Chris
    Commented Oct 11, 2022 at 5:44
  • $\begingroup$ can be (as usual) a lot of reason. Maybe you have "double" cameras on same position? or whatever...that's why a blend file helps a lot... $\endgroup$
    – Chris
    Commented Oct 11, 2022 at 5:46
  • 3
    $\begingroup$ i in for loop starts with 0 so you are lying in your question. The script should give you names 0, 1, 2, 3 ... It seems to work fine as well. Maybe there is some problem with the camera objects. Maybe the cameras have delta transforms or something else like constraints effecting their location?.. $\endgroup$ Commented Oct 11, 2022 at 6:35
  • $\begingroup$ Thank you for your comments guys, I've edited the question and added a link to the blender file and a sketch. Unfortunately, the solution posted as no. 1 didn't work out. $\endgroup$ Commented Oct 12, 2022 at 1:08
  • $\begingroup$ @SimonYoung, how were the cameras created and manipulated after creation? If I create new cameras, the script works, but with those specific ones there is some problem almost as if they were somehow corrupted. $\endgroup$ Commented Oct 12, 2022 at 22:46

1 Answer 1

1
$\begingroup$

You could try taking the actual object's world location from its world matrix:

import bpy
cameras = bpy.context.selected_objects
TOL = 0.0001
if cameras:         
    cameras = sorted(cameras, key = lambda o: (-o.matrix_world.to_translation().y, o.matrix_world.to_translation().x))
    y = cameras[0].matrix_world.to_translation().y
    for i, camera in enumerate(cameras):
        if abs(camera.matrix_world.to_translation().y - y) > TOL:
            z = camera.matrix_world.to_translation().y
        camera.name = "%d" % (i+1)
$\endgroup$

You must log in to answer this question.

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