1
$\begingroup$

I'm trying to make script which will get object.location but it doesn't works: result of my script is:

Trawa.196 and its location is: Vector (0.0000, 0.0000, 0.0000)>

And every object in my scene has these values

ObjectName = "Grass."
ObjectNameNumber = 1


for i in range(1,200):
if(i<10):
    ObjectName = "Trawa.00" + str(ObjectNameNumber)


    objectTrawa = bpy.data.objects[str(ObjectName)]
    objectTrawa.select = True
    print(ObjectName, "and its location is: ", objectTrawa.location)
    bpy.ops.object.select_all(action='DESELECT')
    ObjectNameNumber+=1
if(i>=10 and i<100):
    ObjectName = "Trawa.0" + str(ObjectNameNumber)
    objectTrawa = bpy.data.objects[str(ObjectName)]
    objectTrawa.select = True
    print(ObjectName, "and its location is: ", objectTrawa.location)
    bpy.ops.object.select_all(action='DESELECT')
    ObjectNameNumber+=1
if(i>=100):
    ObjectName = "Trawa." + str(ObjectNameNumber)
    objectTrawa = bpy.data.objects[str(ObjectName)]
    objectTrawa.select = True
    print(ObjectName, "and its location is: ", objectTrawa.location)
    bpy.ops.object.select_all(action='DESELECT')
    ObjectNameNumber+=1
$\endgroup$
3
  • $\begingroup$ have you tried debugging parts of your script using the build in python console? $\endgroup$ Commented Jun 3, 2018 at 14:29
  • 1
    $\begingroup$ Try objectTrawa.matrix_world.translation for the final world matrix location. $\endgroup$ Commented Jun 3, 2018 at 14:49
  • $\begingroup$ Also this is how you pad zeros before string to keep it length 3: str(15).zfill(3), that should simplify your code a lot. $\endgroup$ Commented Jun 3, 2018 at 14:55

1 Answer 1

4
$\begingroup$

Thanks @Jaroslav Jerryno Novotny for help: I'm dumb because I forgot to select everything and set origin to center of its surface/volume (every object had pivot in the middle). But anyway thanks for "objectTrawa.matrix_world.translation" I will use it instead of location and zfill() function (I didn't even think that this would be so good for my code)

final code

ObjectNameNumber = 1

for i in range(1,649):      
        ObjectName = "Trawa." + (str(ObjectNameNumber).zfill(3))
        objectTrawa = bpy.data.objects[str(ObjectName)]
        objectTrawa.select = True
        print(ObjectName, "and its location is: ",     objectTrawa.matrix_world.translation)
        #bpy.ops.object.select_all(action='DESELECT')
        ObjectNameNumber+=1
$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.