0
$\begingroup$

I have a blender file with 4 objects. Every object has position [0,0,0], rotation [0º,0º,0º] and scale [1,1,1] but they are located in different positions in relation to the world origin - [0,0,0]

I need to move them all to the world origin except the z-axis.

I have a script that I use to rotate all the objects in the scene but I can't figure it out how what to do in this case.

# rotate objects:
sel_objs = [obj for obj in bpy.context.selectable_objects if obj.type == 'MESH']
for obj in sel_objs:
    obj.rotation_euler = (0, radians(-90), radians(270))

I'm using Blender 2.83.0

$\endgroup$
1
$\begingroup$

You've lost the original locations of the objects so technically they are at the origin. In the script below I've cheated and used bpy.ops to reset the objects origins to Center of Mass.

import bpy
C = bpy.context
D = bpy.data

bpy.ops.object.select_all(action='DESELECT')

for obj in D.objects:
    obj.select_set(True)
    C.view_layer.objects.active = obj
    ### Pick one ###
    #bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='MEDIAN')
    #bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_VOLUME', center='MEDIAN')
    bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS', center='MEDIAN')
    obj.location[0] = 0 # x
    obj.location[1] = 0 # y
    
    C.view_layer.objects.active=None
    obj.select_set(False)

After batFINGER's suggestion I modified the code to not loop through bpy.ops calls. I also changed it to only affect selected objects. The original version would have moved cameras, lights and whatever else.

import bpy
C = bpy.context
D = bpy.data

### Pick one ###
#bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='MEDIAN')
bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_VOLUME', center='MEDIAN')
#bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS', center='MEDIAN')

for obj in  C.selected_objects:
    obj.location[0] = 0 # x
    obj.location[1] = 0 # y
$\endgroup$
8
  • $\begingroup$ Thanks Ron. Now that I saw your answer I noticed one detail that I missed. In the blend file I have "perfect" cubes but in fact it was a bad example because what I have is not a perfect square in the z-axis. What I mean is that from top view all my 4 objects are a rectangle or square but in the z-axis it's not. I attached a screenshoot to show better what I mean. $\endgroup$
    – zephirus
    Sep 6 '20 at 4:27
  • $\begingroup$ Instead of a screenshot, I attached a new blend file. If you choose the top view you can see that all 4 objects are square (they also can be rectangles from top) but because they are not uniform in the z-axis, when I use your script I don't get the objects centered in the x and y axis. What I need is to ignore the z-axis completely when I define the center of mass. I don't know if this is possible. $\endgroup$
    – zephirus
    Sep 6 '20 at 4:38
  • $\begingroup$ I left the other two options for setting the origin in the code commented out, but the script doesn't move the Z axis at all. The numbers get set but that isn't a big deal, you can always 'Apply Location' $\endgroup$
    – Ron Jensen
    Sep 6 '20 at 4:42
  • $\begingroup$ @zephirus Suggest also adding a screen shot example as to how it is now and expected result, Ron, running the origin change operator once (outside loop) on all selected objects will be a lot quicker when object count is high.. $\endgroup$
    – batFINGER
    Sep 6 '20 at 4:44
  • $\begingroup$ I know that the z-axis isn't changed. But in the x and y, the objects are not centered. That is what I need. I explained myself wrong in my question. $\endgroup$
    – zephirus
    Sep 6 '20 at 4:45

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.