Guest User

Origin Pie/Menu

a guest
Dec 3rd, 2019
6,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.87 KB | None | 0 0
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. #  This program is free software; you can redistribute it and/or
  4. #  modify it under the terms of the GNU General Public License
  5. #  as published by the Free Software Foundation; either version 2
  6. #  of the License, or (at your option) any later version.
  7. #
  8. #  This program is distributed in the hope that it will be useful,
  9. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. #  GNU General Public License for more details.
  12. #
  13. #  You should have received a copy of the GNU General Public License
  14. #  along with this program; if not, write to the Free Software Foundation,
  15. #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18.  
  19. # <pep8 compliant>
  20.  
  21. bl_info = {
  22.     "name": "Origin Pie/Menu",
  23.     "description": "Origin Snap/Place Menu, Hotkey: 'Ctrl Alt X'",
  24.     "author": "Zuorion, pitiwazou, meta-androcto",
  25.     "version": (0, 1, 3),
  26.     "blender": (2, 81, 0),
  27.     "location": "3D View",
  28.     "warning": "",
  29.     "wiki_url": "",
  30.     "category": "Object"
  31.     }
  32.  
  33. import bpy
  34. from bpy.types import (
  35.         Menu,
  36.         Operator,
  37.         )
  38.  
  39.  
  40. # Pivot to selection
  41. class PIE_OT_PivotToSelection(Operator):
  42.     bl_idname = "object.pivot2selection"
  43.     bl_label = "Pivot To Selection"
  44.     bl_description = "Pivot Point To Selection"
  45.     bl_options = {'REGISTER', 'UNDO'}
  46.  
  47.     @classmethod
  48.     def poll(cls, context):
  49.         return context.active_object is not None
  50.  
  51.     def execute(self, context):
  52.         saved_location = context.scene.cursor.location.copy()
  53.         bpy.ops.view3d.snap_cursor_to_selected()
  54.         bpy.ops.object.mode_set(mode='OBJECT')
  55.         bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
  56.         context.scene.cursor.location = saved_location
  57.  
  58.         return {'FINISHED'}
  59.  
  60. # Pivot to Bottom
  61. class PIE_OT_PivotBottom(Operator):
  62.     bl_idname = "object.pivotobottom"
  63.     bl_label = "Pivot To Bottom"
  64.     bl_description = ("Set the Pivot Point To Lowest Point\n"
  65.                       "Needs an Active Object of the Mesh type")
  66.     bl_options = {'REGISTER', 'UNDO'}
  67.  
  68.     @classmethod
  69.     def poll(cls, context):
  70.         obj = context.active_object
  71.         return obj is not None and obj.type == "MESH"
  72.  
  73.     def execute(self, context):
  74.         bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
  75.         bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
  76.         o = context.active_object
  77.         init = 0
  78.         for x in o.data.vertices:
  79.             if init == 0:
  80.                 a = x.co.z
  81.                 init = 1
  82.             elif x.co.z < a:
  83.                 a = x.co.z
  84.  
  85.         for x in o.data.vertices:
  86.             x.co.z -= a
  87.  
  88.         o.location.z += a
  89.  
  90.         return {'FINISHED'}
  91.  
  92.  
  93. # Pivot to Bottom
  94. class PIE_OT_PivotBottom_edit(Operator):
  95.     bl_idname = "object.pivotobottom_edit"
  96.     bl_label = "Pivot To Bottom"
  97.     bl_description = ("Set the Pivot Point To Lowest Point\n"
  98.                       "Needs an Active Object of the Mesh type")
  99.     bl_options = {'REGISTER', 'UNDO'}
  100.  
  101.     @classmethod
  102.     def poll(cls, context):
  103.         obj = context.active_object
  104.         return obj is not None and obj.type == "MESH"
  105.  
  106.     def execute(self, context):
  107.         bpy.ops.object.mode_set(mode='OBJECT')
  108.         bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
  109.         bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
  110.         o = context.active_object
  111.         init = 0
  112.         for x in o.data.vertices:
  113.             if init == 0:
  114.                 a = x.co.z
  115.                 init = 1
  116.             elif x.co.z < a:
  117.                 a = x.co.z
  118.  
  119.         for x in o.data.vertices:
  120.             x.co.z -= a
  121.  
  122.         o.location.z += a
  123.         bpy.ops.object.mode_set(mode='EDIT')
  124.  
  125.         return {'FINISHED'}
  126.  
  127.  
  128. # Pivot to Cursor Edit Mode
  129. class PIE_OT_PivotToCursor_edit(Operator):
  130.     bl_idname = "object.pivot2cursor_edit"
  131.     bl_label = "Pivot To Cursor"
  132.     bl_description = "Pivot Point To Cursor"
  133.     bl_options = {'REGISTER', 'UNDO'}
  134.  
  135.     @classmethod
  136.     def poll(cls, context):
  137.         return context.active_object is not None
  138.  
  139.     def execute(self, context):
  140.         bpy.ops.object.mode_set(mode='OBJECT')
  141.         bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
  142.         bpy.ops.object.mode_set(mode='EDIT')
  143.  
  144.         return {'FINISHED'}
  145.  
  146.  
  147. # Origin to Center of Mass Edit Mode
  148. class PIE_OT_OriginToMass_edit(Operator):
  149.     bl_idname = "object.origintomass_edit"
  150.     bl_label = "Origin"
  151.     bl_description = "Origin to Center of Mass"
  152.     bl_options = {'REGISTER', 'UNDO'}
  153.  
  154.     @classmethod
  155.     def poll(cls, context):
  156.         return context.active_object is not None
  157.  
  158.     def execute(self, context):
  159.         bpy.ops.object.mode_set(mode='OBJECT')
  160.         bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS', center='MEDIAN')
  161.         bpy.ops.object.mode_set(mode='EDIT')
  162.  
  163.         return {'FINISHED'}
  164.  
  165.  
  166. # Origin to Geometry Edit Mode
  167. class PIE_OT_OriginToGeometry_edit(Operator):
  168.     bl_idname = "object.origintogeometry_edit"
  169.     bl_label = "Origin to Geometry"
  170.     bl_description = "Origin to Geometry"
  171.     bl_options = {'REGISTER', 'UNDO'}
  172.  
  173.     @classmethod
  174.     def poll(cls, context):
  175.         return context.active_object is not None
  176.  
  177.     def execute(self, context):
  178.         bpy.ops.object.mode_set(mode='OBJECT')
  179.         bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='MEDIAN')
  180.         bpy.ops.object.mode_set(mode='EDIT')
  181.  
  182.         return {'FINISHED'}
  183.  
  184.  
  185. # Origin to Geometry Edit Mode
  186. class PIE_OT_GeometryToOrigin_edit(Operator):
  187.     bl_idname = "object.geometrytoorigin_edit"
  188.     bl_label = "Geometry to Origin"
  189.     bl_description = "Geometry to Origin"
  190.     bl_options = {'REGISTER', 'UNDO'}
  191.  
  192.     @classmethod
  193.     def poll(cls, context):
  194.         return context.active_object is not None
  195.  
  196.     def execute(self, context):
  197.         bpy.ops.object.mode_set(mode='OBJECT')
  198.         bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN', center='MEDIAN')
  199.         bpy.ops.object.mode_set(mode='EDIT')
  200.  
  201.         return {'FINISHED'}
  202.  
  203.  
  204. # Origin To Selected Edit Mode #
  205. def vfeOrigin_pie(context):
  206.     try:
  207.         cursorPositionX = context.scene.cursor.location[0]
  208.         cursorPositionY = context.scene.cursor.location[1]
  209.         cursorPositionZ = context.scene.cursor.location[2]
  210.         bpy.ops.view3d.snap_cursor_to_selected()
  211.         bpy.ops.object.mode_set()
  212.         bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
  213.         bpy.ops.object.mode_set(mode='EDIT')
  214.         context.scene.cursor.location[0] = cursorPositionX
  215.         context.scene.cursor.location[1] = cursorPositionY
  216.         context.scene.cursor.location[2] = cursorPositionZ
  217.         return True
  218.     except:
  219.         return False
  220.  
  221.  
  222. class PIE_OT_SetOriginToSelected_edit(Operator):
  223.     bl_idname = "object.setorigintoselected_edit"
  224.     bl_label = "Set Origin to Selected"
  225.     bl_description = "Set Origin to Selected"
  226.  
  227.     @classmethod
  228.     def poll(cls, context):
  229.         return (context.area.type == "VIEW_3D" and context.active_object is not None)
  230.  
  231.     def execute(self, context):
  232.         check = vfeOrigin_pie(context)
  233.         if not check:
  234.             self.report({"ERROR"}, "Set Origin to Selected could not be performed")
  235.             return {'CANCELLED'}
  236.  
  237.         return {'FINISHED'}
  238.  
  239. class RAlignOrientationToSelection(bpy.types.Operator):
  240.     '''Tooltip'''
  241.     bl_description = "Align object's orientation to the selected elements."
  242.     bl_idname = "object.ralign_orientation_to_selection"
  243.     bl_label = "Align Orientation To Selection"
  244.     bl_options = {'REGISTER', 'UNDO'}
  245.  
  246.     @classmethod
  247.     def poll(cls, context):
  248.         obj = context.active_object
  249.         obj_type = obj.type
  250.         return(obj and obj_type in {'MESH'})
  251.  
  252.     def execute(self, context):
  253.         selected = context.selected_objects
  254.         obj = context.active_object
  255.         objects = bpy.data.objects    
  256.         #Create custom orientation
  257.         prev_orientation = bpy.context.scene.transform_orientation_slots[0].type
  258.         bpy.context.scene.transform_orientation_slots[0].type = 'NORMAL'
  259.         bpy.ops.transform.create_orientation(name="rOrientation", use=True, overwrite=True)
  260.         #Exit to Object Mode
  261.         bpy.ops.object.editmode_toggle()
  262.         utdo = bpy.context.scene.tool_settings.use_transform_data_origin
  263.         #Add empty aligned to the orientation
  264.         bpy.context.scene.tool_settings.use_transform_data_origin = True
  265.         bpy.ops.transform.transform(mode='ALIGN', value=(0, 0, 0, 0), orient_type='rOrientation', mirror=True, use_proportional_edit=False)
  266.         #Restore settings and editmode
  267.         bpy.ops.transform.delete_orientation()
  268.         bpy.context.scene.transform_orientation_slots[0].type = prev_orientation
  269.         bpy.context.scene.tool_settings.use_transform_data_origin = utdo
  270.         bpy.ops.object.editmode_toggle()
  271.         return {'FINISHED'}
  272.  
  273.  
  274.  
  275. # Pie Origin/Pivot - Shift + S
  276. class PIE_MT_OriginPivot(Menu):
  277.     bl_idname = "ORIGIN_MT_pivotmenu"
  278.     bl_label = "Origin Menu"
  279.  
  280.     def draw(self, context):
  281.         layout = self.layout
  282.         obj = context.object
  283.         pie = layout.menu_pie()
  284.         if obj and obj.type == 'MESH' and obj.mode in {'OBJECT'}:
  285.             # 4 - LEFT
  286.             pie.operator("object.origin_set", text="Origin to Center of Mass",
  287.                          icon='NONE').type = 'ORIGIN_CENTER_OF_MASS'
  288.             # 6 - RIGHT
  289.             pie.operator("object.origin_set", text="Origin to Cursor",
  290.                         icon='PIVOT_CURSOR').type = 'ORIGIN_CURSOR'
  291.             # 2 - BOTTOM
  292.             pie.operator("object.pivotobottom", text="Origin to Bottom",
  293.                         icon='TRIA_DOWN')
  294.             # 8 - TOP
  295.             pie.operator("object.pivot2selection", text="Origin To Selection",
  296.                         icon='SNAP_INCREMENT')
  297.             # 7 - TOP - LEFT
  298.             pie.operator("object.origin_set", text="Geometry To Origin",
  299.                         icon='NONE').type = 'GEOMETRY_ORIGIN'
  300.             # 9 - TOP - RIGHT
  301.             pie.operator("object.origin_set", text="Origin To Geometry",
  302.                         icon='NONE').type = 'ORIGIN_GEOMETRY'
  303.  
  304.  
  305.         elif obj and obj.type == 'MESH' and obj.mode in {'EDIT'}:                        
  306.             # 4 - LEFT
  307.             pie.operator("object.origintomass_edit", text="Origin to Center of Mass",
  308.                          icon='NONE')
  309.             # 6 - RIGHT
  310.             pie.operator("object.pivot2cursor_edit", text="Origin to Cursor",
  311.                         icon='PIVOT_CURSOR')
  312.             # 2 - BOTTOM
  313.             pie.operator("object.pivotobottom_edit", text="Origin to Bottom",
  314.                         icon='TRIA_DOWN')
  315.             # 8 - TOP
  316.             pie.operator("object.setorigintoselected_edit", text="Origin To Selected",
  317.                         icon='SNAP_INCREMENT')        
  318.             # 7 - TOP - LEFT
  319.             pie.operator("object.geometrytoorigin_edit", text="Geometry To Origin",
  320.                         icon='NONE')
  321.             # 9 - TOP - RIGHT
  322.             pie.operator("object.origintogeometry_edit", text="Origin To Geometry",
  323.                         icon='NONE')
  324.             # 10 - TOP - RIGHT          
  325.             pie.operator("object.ralign_orientation_to_selection", text="Align Orientation To Selection",
  326.                         icon='NONE')
  327.  
  328.         else:
  329.             # 4 - LEFT
  330.             pie.operator("object.origin_set", text="Origin to Center of Mass",
  331.                          icon='NONE').type = 'ORIGIN_CENTER_OF_MASS'
  332.             # 6 - RIGHT
  333.             pie.operator("object.origin_set", text="Origin To 3D Cursor",
  334.                         icon='PIVOT_CURSOR').type = 'ORIGIN_CURSOR'
  335.             # 2 - BOTTOM
  336.             pie.operator("object.pivot2selection", text="Origin To Selection",
  337.                         icon='SNAP_INCREMENT')
  338.             # 8 - TOP
  339.             pie.operator("object.origin_set", text="Origin To Geometry",
  340.                         icon='NONE').type = 'ORIGIN_GEOMETRY'
  341.             # 7 - TOP - LEFT
  342.             pie.operator("object.origin_set", text="Geometry To Origin",
  343.                         icon='NONE').type = 'GEOMETRY_ORIGIN'
  344.  
  345.  
  346.  
  347. class VIEW3D_MT_OriginPivotMenu(Menu):
  348.     bl_idname = "VIEW3D_MT_OriginPivotMenu"
  349.     bl_label = "Origin Menu"
  350.    
  351.     def draw(self, context):
  352.         layout = self.layout
  353.         layout.operator_context = 'INVOKE_REGION_WIN'
  354.         obj = context.object
  355.  
  356.         if obj and obj.type == 'MESH' and obj.mode in {'OBJECT'}:
  357.             # 4 - LEFT
  358.             layout.operator("object.origin_set", text="Origin to Center of Mass",
  359.                          icon='NONE').type = 'ORIGIN_CENTER_OF_MASS'
  360.             # 6 - RIGHT
  361.             layout.operator("object.origin_set", text="Origin to Cursor",
  362.                         icon='PIVOT_CURSOR').type = 'ORIGIN_CURSOR'
  363.             # 2 - BOTTOM
  364.             layout.operator("object.pivotobottom", text="Origin to Bottom",
  365.                         icon='TRIA_DOWN')
  366.             # 8 - TOP
  367.             layout.operator("object.pivot2selection", text="Origin To Selection",
  368.                         icon='SNAP_INCREMENT')
  369.             # 7 - TOP - LEFT
  370.             layout.operator("object.origin_set", text="Geometry To Origin",
  371.                         icon='NONE').type = 'GEOMETRY_ORIGIN'
  372.             # 9 - TOP - RIGHT
  373.             layout.operator("object.origin_set", text="Origin To Geometry",
  374.                         icon='NONE').type = 'ORIGIN_GEOMETRY'
  375.  
  376.  
  377.         elif obj and obj.type == 'MESH' and obj.mode in {'EDIT'}:                        
  378.             # 4 - LEFT
  379.             layout.operator("object.origintomass_edit", text="Origin to Center of Mass",
  380.                          icon='NONE')
  381.             # 6 - RIGHT
  382.             layout.operator("object.pivot2cursor_edit", text="Origin to Cursor",
  383.                         icon='PIVOT_CURSOR')
  384.             # 2 - BOTTOM
  385.             layout.operator("object.pivotobottom_edit", text="Origin to Bottom",
  386.                         icon='TRIA_DOWN')
  387.             # 8 - TOP
  388.             layout.operator("object.setorigintoselected_edit", text="Origin To Selected",
  389.                         icon='SNAP_INCREMENT')        
  390.             # 7 - TOP - LEFT
  391.             layout.operator("object.geometrytoorigin_edit", text="Geometry To Origin",
  392.                         icon='NONE')
  393.             # 9 - TOP - RIGHT
  394.             layout.operator("object.origintogeometry_edit", text="Origin To Geometry",
  395.                         icon='NONE')
  396.             layout.operator("object.ralign_orientation_to_selection", text="Align Orientation To Selection",
  397.                         icon='NONE')
  398.  
  399.         else:
  400.             # 4 - LEFT
  401.             layout.operator("object.origin_set", text="Origin to Center of Mass",
  402.                          icon='NONE').type = 'ORIGIN_CENTER_OF_MASS'
  403.             # 6 - RIGHT
  404.             layout.operator("object.origin_set", text="Origin To 3D Cursor",
  405.                         icon='PIVOT_CURSOR').type = 'ORIGIN_CURSOR'
  406.             # 2 - BOTTOM
  407.             layout.operator("object.pivot2selection", text="Origin To Selection",
  408.                         icon='SNAP_INCREMENT')
  409.             # 8 - TOP
  410.             layout.operator("object.origin_set", text="Origin To Geometry",
  411.                         icon='NONE').type = 'ORIGIN_GEOMETRY'
  412.             # 7 - TOP - LEFT
  413.             layout.operator("object.origin_set", text="Geometry To Origin",
  414.                         icon='NONE').type = 'GEOMETRY_ORIGIN'
  415.  
  416. def addmenu_callback(self, context):
  417.     self.layout.menu("VIEW3D_MT_OriginPivotMenu")
  418.  
  419. classes = (
  420.     PIE_MT_OriginPivot,
  421.     PIE_OT_PivotToSelection,
  422.     PIE_OT_PivotBottom,
  423.     PIE_OT_PivotToCursor_edit,
  424.     PIE_OT_OriginToMass_edit,
  425.     PIE_OT_PivotBottom_edit,
  426.     PIE_OT_OriginToGeometry_edit,
  427.     PIE_OT_GeometryToOrigin_edit,
  428.     PIE_OT_SetOriginToSelected_edit,
  429.     RAlignOrientationToSelection,
  430.     VIEW3D_MT_OriginPivotMenu
  431.     )
  432.  
  433. addon_keymaps = []
  434.  
  435.  
  436. def register():
  437.     for cls in classes:
  438.         bpy.utils.register_class(cls)
  439.  
  440.     wm = bpy.context.window_manager
  441.     if wm.keyconfigs.addon:
  442.         # Origin/Pivot
  443.         km = wm.keyconfigs.addon.keymaps.new(name='3D View Generic', space_type='VIEW_3D')
  444.         kmi = km.keymap_items.new('wm.call_menu', 'X', 'PRESS', ctrl=True, alt=True)
  445.         kmi.properties.name = "VIEW3D_MT_OriginPivotMenu"
  446.         addon_keymaps.append((km, kmi))
  447.        
  448.     bpy.types.VIEW3D_MT_edit_mesh.append(addmenu_callback)
  449.  
  450.  
  451. def unregister():
  452.     for cls in classes:
  453.         bpy.utils.unregister_class(cls)
  454.  
  455.     wm = bpy.context.window_manager
  456.     kc = wm.keyconfigs.addon
  457.     if kc:
  458.         for km, kmi in addon_keymaps:
  459.             km.keymap_items.remove(kmi)
  460.     addon_keymaps.clear()
  461.    
  462.     bpy.types.VIEW3D_MT_edit_mesh.remove(addmenu_callback)
  463.  
  464.  
  465. if __name__ == "__main__":
  466.     register()
Advertisement
Add Comment
Please, Sign In to add comment