5

What I want:
I want to set the origin of an object to the center of its bounding box using a python command.

What I've tried:

bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')

and

bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')

What happened:
Image of a not-centered origin

As you can see, the origin isn't centered to the bounding box.

EDIT: I asked the same question as Get center of geometry of an object with the addition of setting the origin to this center.

froggyman
  • 503
  • 5
  • 16

2 Answers2

14

Have you tried setting the center to BOUNDS?

bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')
brockmann
  • 12,613
  • 4
  • 50
  • 93
gandalf3
  • 157,169
  • 58
  • 601
  • 1,133
  • 1
    It works on its own. But when I add it to my script, it doesn't work for some reason. https://drive.google.com/file/d/1O_G_REJSVIRMu6ndof6ivK6QdJNtzxDk/view?usp=sharing – June Wang Sep 24 '20 at 11:37
0

I noted that this can be further confusing if you have a group with joined geometry inside it.

objGrp = bpy.data.objects.new(scn.baseName, None)

if objGrp has children that have there own origin set wrong sleecting it in the view will still have the origin wrong while the group parent object might be fine. In this case you simply need to make sure you've selected the internal object first and the same will apply.

So Center of Mass etc. will work as expected on the kid too. Here we see this situation where a bunch of generated objects are joined and then have the center set. Still maybe you want to loose the parent object at that point but it's just an example of how the Question poster (or others with this variation) might have not see the desired effect.

obs = objGrp.children
ctx = bpy.context.copy()

ctx['active_object'] = obs[0]
ctx['selected_objects'] = obs
ctx['selected_editable_bases'] = [scn.object_bases[ob.name] for ob in obs]

ob = bpy.ops.object
ob.join(ctx)
obs[0].select = True
ob.origin_set(type='ORIGIN_CENTER_OF_MASS')
Master James
  • 299
  • 2
  • 9