Wednesday, 3 June 2015

Blender Set Origin Script

I have not done much work on my game recently but to do so I need to get round to creating the levels and I'm back to using Blender again.

When I started to tidy up one of my models I came across an odd feature in Blender. You can only set the origin on the active object.

My main reason for setting an origin is to get all of the origins of all of the objects in a scene at the zero point. I find it easier to import in to other programmes and line them up if they all have the same centre.


Using the built in button, it is frustrating having to select one object at a time to set all the origins. I decided to create a script to do this for me.

Not as easy as I expected because even in code the set origin only works on the active object.


I just typed the lines in to the console to run it. After the last line press return one extra time to run it.



for item in bpy.context.scene.objects: 
 if item.type == 'MESH': 
  item.select = True 
  bpy.context.scene.objects.active = item 
  bpy.ops.object.origin_set(type='ORIGIN_CURSOR') 
  item.select = False



The above worked in version 2.74 of Blender. It goes through ALL objects in the scene which is what I wanted.



for item in bpy.context.selected_objects:
 if item.type == 'MESH': 
  bpy.context.scene.objects.active = item 
  bpy.ops.object.origin_set(type='ORIGIN_CURSOR') 



In theory the above should work for the currently selected objects but I have not tested that script.



I came across another oversight when trying to copy that code to save it. To get the Windows Ctrl-C (copy) and Ctrl-V (paste) to work in the console window you first have to click on the console menu item at least once. After that Ctrl-C and Ctrl-V work as expected.

2 comments:

Unknown said...

Love following this blog, been following it from the start, keep up the good work, nice to see it back!

John C Brown said...

Thank you for the encouragement.
Changing to using off the shelf components and the ready made Unity engine required a bit of a rethink about the way I work.
I can code on the train or on the sofa but 3D modelling requires a good desk based setup and long uninterrupted sessions.
There will probably only be sporadic updates for a while but then I'm sure I'll be back in full swing.
Regards