Skip to content

Instantly share code, notes, and snippets.

@sambler
Last active Oct 12, 2017
Embed
What would you like to do?
Create a custom set origin menu
# made in response to https://blender.stackexchange.com/q/92105/935
bl_info = {
"name": "Test setting origin",
"author": "sambler",
"version": (1, 0),
"blender": (2, 75, 0),
"location": "blender",
"description": "test setting origin menu",
"warning": "Only testing",
"category": "Test",
}
import bpy
def my_custom_centring():
print('Setting origin my way')
class My_Operator(bpy.types.Operator):
bl_idname = 'object.my_set_origin'
bl_label = 'Custom set origin'
centre = bpy.props.StringProperty(default='')
def execute(self, context):
if self.centre == 'ORIGIN_TO_0Z':
my_custom_centring()
else:
return bpy.ops.object.origin_set(type=self.centre)
return {'FINISHED'}
class My_menu(bpy.types.Menu):
bl_label = "My Custom Menu"
bl_idname = "OBJECT_MT_my_custom_menu"
def draw(self, context):
layout = self.layout
layout.operator(My_Operator.bl_idname, text='Geom to orig').centre = 'GEOMETRY_ORIGIN'
layout.operator(My_Operator.bl_idname, text='orig to geom').centre = 'ORIGIN_GEOMETRY'
layout.operator(My_Operator.bl_idname, text='orig to cursor').centre = 'ORIGIN_CURSOR'
layout.operator(My_Operator.bl_idname, text='orig to mass').centre = 'ORIGIN_CENTER_OF_MASS'
layout.operator(My_Operator.bl_idname, text='orig to vol').centre = 'ORIGIN_CENTER_OF_VOLUME'
layout.operator(My_Operator.bl_idname, text='orig to zeroZ').centre = 'ORIGIN_TO_0Z'
def register():
bpy.utils.register_class(My_Operator)
bpy.utils.register_class(My_menu)
wm = bpy.context.window_manager
win_keymaps = wm.keyconfigs.user.keymaps.get('Object Non-modal')
if win_keymaps:
# disable standard set origin keymap
for kmi in win_keymaps.keymap_items:
if kmi.idname == 'object.origin_set':
kmi.active = False
# add a keymap for our set origin operator
kmi = win_keymaps.keymap_items.new('wm.call_menu', 'C',
'PRESS', ctrl=True, alt=True, shift=True)
setattr(kmi.properties, 'name', My_menu.bl_idname)
def unregister():
bpy.utils.unregister_class(My_menu)
bpy.utils.unregister_class(My_Operator)
wm = bpy.context.window_manager
win_keymaps = wm.keyconfigs.user.keymaps.get('Object Non-modal')
if win_keymaps:
for kmi in win_keymaps.keymap_items:
# re-enable standard set origin
if kmi.idname == 'object.origin_set':
kmi.active = True
# remove our custom set origin
if kmi.idname == 'wm.call_menu' and getattr(kmi.properties, 'name') == My_menu.bl_idname:
win_keymaps.keymap_items.remove(kmi)
if __name__ == "__main__":
register()
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment