How to simply swap object positions?

So I have experience in other programming languages, but this seemingly simple task is getting the better of me. All I want to do is swap one object with another, the position, the rotation and the scaling.

I came up with this:


import bpy;

ob1 = bpy.context.selected_objects[0];
ob2 = bpy.context.selected_objects[1];

location = ob1.location;
rotation = ob1.rotation_euler;
scale = ob1.scale;

location2 = ob2.location;
rotation2 = ob2.rotation_euler;
scale2 = ob2.scale;

ob2.location = location;
ob2.rotation_euler = rotation;
ob2.scale = scale;

ob1.location = location2;
ob1.rotation_euler = rotation2;
ob1.scale = scale2;

Store the first 2 selected object’s properties and then assign them to each other. But whatever I do one object will move and scale etc but the other will stay just where it is e.g. using 2 cubes would result in 2 cubes in exactly the same place, with same rotation and scale. I feel I must be doing something obviously stupid, so any help is appreciated.

Thanks.

Tried it. Same behavior. Apparently the object.location etc updates after you move the object. So once you move the first object, when you go to move the second, it wants to move it…nowhere. Interesting.


import bpy

ob1 = bpy.context.selected_objects[0]
ob2 = bpy.context.selected_objects[1]

location = ob1.location.copy()
rotation = ob1.rotation_euler.copy()
scale = ob1.scale.copy()

location2 = ob2.location.copy();
rotation2 = ob2.rotation_euler.copy();
scale2 = ob2.scale.copy();

ob2.location = location
ob2.rotation_euler = rotation
ob2.scale = scale

ob1.location = location2
ob1.rotation_euler = rotation2
ob1.scale = scale2

Thanks! That works really, well, I was starting to think I would have to create an empty and swap to that. Anyway thanks very much.

You are very welcome…Make sure you go to “thread tools -> mark as solved”

You only ever need one set of temp variables when you swap. I’m typing on my phone so forgive me for not giving the full example… But after you make the first set of copies you can assign directly to ob1.* from ob2.*

I am pretty sure you’ll still need all 6 .copy()s, though.

-rking

you could also change the mesh name
and it would take the other mesh shape!

but need to make a copy of object to keep the original shape anyway !

salutations

You could also use


ob1.location, ob2.location = ob2.location.copy() , ob1.location.copy()

You only ever need one set of temp variables when you swap. I’m typing on my phone so forgive me for not giving the full example… But after you make the first set of copies you can assign directly to ob1.* from ob2.*
Yeah, this is the way I first did it, only changed to 2 sets of variables when it wasn’t working, changed back now though.

you could also change the mesh name
and it would take the other mesh shape!
but need to make a copy of object to keep the original shape anyway !
That’s a nice solution, I just felt there should have been a way to do it without adding extra objects.

ob1.location, ob2.location = ob2.location.copy() , ob1.location.copy()

Wow, now that’s efficient, can I have a bit of an explanation as to quite how that works?

Thanks again everyone, a great welcome to the Blender Artists forum.

Hi Madog09,

python lets you do

a, b, c = d, e, f

which is handy for swapping values without the var.

Ah, I see, thanks. Is there a name for assigning values in this way?