0
$\begingroup$

I have no idea how to upgrade the 2.79 custom shortcuts I´ve done in the past. Currently I have this .py (how can I upgrade it to 2.8?) This is the most essential feature I prominently use when using Blender. Please help.

bl_info = {
'name': 'Move origin to selected',
'author': '',
'version': (0, 0, 1),
'blender': (2, 6, 7),
'location': '3d view > space bar > Origin Move to Selected',
'description': 'in edit mode, sets object origin to the median of selected verts/edges/faces',
'wiki_url': '',
'tracker_url': '',
'category': '3D View'}

import bpy

class MoveOrigin(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.origin_to_selected"
    bl_label = "Origin Move To Selected"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return obj is not None and obj.mode == 'EDIT'

    def execute(self, context):
        saved_location = bpy.context.scene.cursor_location.copy()
        bpy.ops.view3d.snap_cursor_to_selected()

        bpy.ops.object.mode_set(mode = 'OBJECT')
        bpy.ops.object.origin_set(type='ORIGIN_CURSOR')  
        bpy.context.scene.cursor_location = saved_location

        bpy.ops.object.mode_set(mode = 'EDIT')
        return {'FINISHED'}


def register():
    bpy.utils.register_class(MoveOrigin)


def unregister():
    bpy.utils.unregister_class(MoveOrigin)


if __name__ == "__main__":
    register()
$\endgroup$
1
$\begingroup$

The updated add-on for 2.8 could like the following code:

bl_info = {
'name': 'Move origin to selected',
'author': '',
'version': (0, 0, 1),
'blender': (2, 8, 0),
'location': '3D view > F3 > Origin Move to Selected',
'description': 'In edit mode, sets object origin to the median of selected verts/edges/faces',
'wiki_url': '',
'tracker_url': '',
'category': '3D View'
}

import bpy

class OBJECT_OT_origin_to_selected(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.origin_to_selected"
    bl_label = "Origin Move To Selected"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        obj = context.view_layer.objects.active
        if context.area.type == 'VIEW_3D' and obj is not None and obj.mode == 'EDIT':
            return True
        else:
            return False

    def execute(self, context):
        saved_location = bpy.context.scene.cursor.location.copy()
        bpy.ops.view3d.snap_cursor_to_selected()

        bpy.ops.object.mode_set(mode = 'OBJECT')
        bpy.ops.object.origin_set(type='ORIGIN_CURSOR')  
        bpy.context.scene.cursor.location = saved_location

        bpy.ops.object.mode_set(mode = 'EDIT')
        return {'FINISHED'}


def register():
    bpy.utils.register_class(OBJECT_OT_origin_to_selected)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_origin_to_selected)


if __name__ == "__main__":
    register()

API changes include:

  • How the active object is accessed
  • How to get the cursor location
  • Mandatory naming convention for operators
$\endgroup$
  • $\begingroup$ Thank you so much. Upvoted and answered. $\endgroup$ – Pierre Schiller Aug 7 '19 at 21:30

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.