Moving origin to selection – wrapping to an add-on

Last time, we wrote a small script to quickly move an object’s origin to the center of a selection. Now, we can perform this operation quickly, but we need to keep an additional area open with the script text. To avoid this inconvenience, we can turn our script into an add-on and assign it to a convenient key bind.

First, let’s define a dictionary with the required name bl_info, which contains the basic data for our add-on.

Here we’ve specified the add-on name “origin2” (this is the name it will be listed under in the add-ons list), the general category “All”, the version of our add-on, and the Blender version (4.5) for which it is intended.

Let’s define a class for the operator from which our script code will be executed.

We define the operator class name ourselves, but we add the required _OT_ suffix to it, which indicates to the Blender API that we want to create an operator.

In the “bl_idname” parameter, we specify a unique identifier for our operator. It will be used to call our operator from the API.

“bl_label” is the text label for the operator.

And in the “bl_options”, we specify parameters for registering our operator with the Blender API and including its calls in the global undo system (so that its actions can be undone by pressing Ctrl+z).

The main executable code, which we almost exactly copy from our previous script, we should place to the execute() operator function.

The return value from the execute() function should always be {“FINISHED”}, this is a feature of Blender operators.

Since we pass the “context” as a function parameter, we can access the “context” in our code not through the global “bpy.context” pointer, but through the pointer passed in the function parameter.

Now let’s bind the operator execution to a keystroke.

For this, define a KeyMap class.

Define a list in it, empty for now, in which we’ll place the operator call binding to a keystroke.

Define two functions, register() and unregister(), to create and remove the keystroke binding.

The register() function:

In the register() function, we first create a new keymap—a list of keypress operator binding, and then add the created “keymap_item” binding to it.

In the keymap parameters, we specify our operator’s “bl_idname”, the keycode to which the operator call is assigned, and additional parameters, such as the requirement to simultaneously hold down the additional function keys Ctrl, Alt, or Shift.

In our add-on, we create a keymap for pressing (“PRESS”) the “c” key with simultaneously holding the Shift and Ctrl keys.

We place the created keymap in a list so that we can delete it when the add-on is uninstalled.

Define the unregister() function.

It will be called when our add-on is uninstalled or deactivated.

This function’s code simply deletes all the keymap bindings we created, and then deletes the keymap itself.

One last thing: define two functions to register and unregister our add-on in the Blender Python API.

In the register() function, we register our operator in the Blender API and call the function to register our keymap.

In the unregister() function, we reverse the process, removing the operator from the Blender API and unbinding the operator call from the keypress.

Now save the add-on to the origin2.py file.

And install it in Blender in the common way: open the Preferences area, switch to the Add-ons tab, click the arrow in the upper right corner, and select “Install from Disk…”.

Start typing “origin” in the search field until you see the line with the add-on. Check the box in this line to activate the add-on. Click “Save Preferences” (if you have automatic saving of settings disabled) so that the add-on will automatically activate the next time you launch Blender.

That’s all.

In the 3D viewport workspace in edit mode, when you press the key combination Ctrl + Shift + c, the origin will be instantly moved to the center of the selected geometry.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments