Skip to content
Snippets Groups Projects
space_view3d_align_tools.py 37.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • class OBJECT_OT_AlignScaleZOperator(Operator):
    
        bl_idname = "object.align_objects_scale_z"
        bl_label = "Align Selected Scale Z To Active"
        bl_description = "Align Selected Scale Z To Active"
    
        @classmethod
        def poll(cls, context):
            return context.active_object is not None
    
        def execute(self, context):
            ScaleZ(context)
            return {'FINISHED'}
    
    
    # Interface Panel
    
    class VIEW3D_PT_AlignUi(Panel):
        bl_space_type = 'VIEW_3D'
        bl_region_type = 'UI'
        bl_label = "Align Tools"
        bl_context = "objectmode"
        bl_category = 'Item'
        bl_options = {'DEFAULT_CLOSED'}
    
        def draw(self, context):
            layout = self.layout
            obj = context.object
    
            if obj is not None:
                row = layout.row()
                row.label(text="Active object is: ", icon='OBJECT_DATA')
                box = layout.box()
                box.label(text=obj.name, icon='EDITMODE_HLT')
    
            col = layout.column()
            col.label(text="Align Loc + Rot:")
    
            col = layout.column(align=False)
            col.operator("object.align", text="XYZ")
    
            col = layout.column()
            col.label(text="Align Location:")
    
            col = layout.column_flow(columns=4, align=True)
            col.operator("object.align_location_x", text="X")
            col.operator("object.align_location_y", text="Y")
            col.operator("object.align_location_z", text="Z")
            col.operator("object.align_location_all", text="All")
    
            col = layout.column()
            col.label(text="Align Rotation:")
    
            col = layout.column_flow(columns=4, align=True)
            col.operator("object.align_rotation_x", text="X")
            col.operator("object.align_rotation_y", text="Y")
            col.operator("object.align_rotation_z", text="Z")
            col.operator("object.align_rotation_all", text="All")
    
            col = layout.column()
            col.label(text="Align Scale:")
    
            col = layout.column_flow(columns=4, align=True)
            col.operator("object.align_objects_scale_x", text="X")
            col.operator("object.align_objects_scale_y", text="Y")
            col.operator("object.align_objects_scale_z", text="Z")
            col.operator("object.align_objects_scale_all", text="All")
    
            if obj is not None:
                col = layout.column()
                col.label(text="Advanced Align Operations")
                layout = self.layout
                self.layout.operator("object.align_tools", text="Advanced")
    
    
    # Add-ons Preferences Update Panel
    
    # Define Panel classes for updating
    panels = (
            VIEW3D_PT_AlignUi,
            )
    
    
    def update_panel(self, context):
        message = "Align Tools: Updating Panel locations has failed"
        try:
            for panel in panels:
                if "bl_rna" in panel.__dict__:
                    bpy.utils.unregister_class(panel)
    
            for panel in panels:
                panel.bl_category = context.preferences.addons[__name__].preferences.category
                bpy.utils.register_class(panel)
    
        except Exception as e:
            print("\n[{}]\n{}\n\nError:\n{}".format(__name__, message, e))
            pass
    
    
    class AlignAddonPreferences(AddonPreferences):
        # this must match the addon name, use '__package__'
        # when defining this in a submodule of a python package.
        bl_idname = __name__
    
        category: StringProperty(
                name="Tab Category",
                description="Choose a name for the category of the panel",
                default="Item",
                update=update_panel
                )
    
        def draw(self, context):
            layout = self.layout
    
            row = layout.row()
            col = row.column()
            col.label(text="Tab Category:")
            col.prop(self, "category", text="")
    
    
    # Class List
    classes = (
        VIEW3D_PT_AlignUi,
    
        OBJECT_OT_AlignOperator,
        OBJECT_OT_AlignLocationOperator,
        OBJECT_OT_AlignLocationXOperator,
        OBJECT_OT_AlignLocationYOperator,
        OBJECT_OT_AlignLocationZOperator,
        OBJECT_OT_AlignRotationOperator,
        OBJECT_OT_AlignRotationXOperator,
        OBJECT_OT_AlignRotationYOperator,
        OBJECT_OT_AlignRotationZOperator,
        OBJECT_OT_AlignScaleOperator,
        OBJECT_OT_AlignScaleXOperator,
        OBJECT_OT_AlignScaleYOperator,
        OBJECT_OT_AlignScaleZOperator,
    
        OBJECT_OT_align_tools,
        AlignAddonPreferences,
        )
    
    
    # Register all operators and panels
    def register():
        for cls in classes:
            bpy.utils.register_class(cls)
    
    
    def unregister():
        for cls in classes:
            bpy.utils.unregister_class(cls)
    
    
    if __name__ == "__main__":
        register()