Moving origin to selection in Blender

Moving the center point of a mesh (the origin) to the current selection is one of the routine operations when modeling in Blender. However, there’s no base operator for this in Blender, and it usually has to be performed in several steps: moving the 3D cursor to the center of the selection, moving the origin to the 3D cursor, and then, if necessary, returning the 3D cursor to its original position. All these operations can be combined in a simple script and performed in a single click.

Let’s create a script that will move the origin of the current mesh to the center of the selection.

First, save the original position of the 3D cursor. We’ll need it later to return the cursor after all the operations.

Note that we save the location value in the copy using the copy() call. Otherwise, instead of the cursor coordinates, we’ll get a reference to the current cursor coordinates, and then we won’t be able to get the original coordinates later.

Since we’re executing the code from a text editor, we need to override the context to the 3D viewport area.

A pointer to the 3D viewport area:

Use it to temporarily override the context:

Now we can call operators whose context is bound to the 3D viewport without errors.

Move the 3D cursor to the center of the selection.

Now we can move the origin to the 3D cursor.

However, the move origin operator only works in object mode. Therefore, we first need to switch to object mode, then move the origin, and then switch back to edit mode.

Now the origin is exactly in the center of the selection.

All that’s left is to return the 3D cursor to its original coordinates.

Now, by pressing just one button to execute the script, we immediately move the mesh’s origin to the center of the current selection.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments