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$
  • $\begingroup$ have you tried debugging parts of your script using the build in python console? $\endgroup$ – Bert VdB Jun 3 '18 at 14:29
  • $\begingroup$ Try objectTrawa.matrix_world.translation for the final world matrix location. $\endgroup$ – Jaroslav Jerryno Novotny Jun 3 '18 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$ – Jaroslav Jerryno Novotny Jun 3 '18 at 14:55
1
$\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$

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.