Skip to content
Snippets Groups Projects
space_view3d_align_tools.py 38.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • # Align Scale Y
    class AlignScaleYOperator(Operator):
    
        bl_idname = "object.align_objects_scale_y"
        bl_label = "Align Selected Scale Y To Active"
    
        bl_description = "Align Selected Scale Y To Active"
    
    
        @classmethod
        def poll(cls, context):
    
            return context.active_object is not None
    
    
        def execute(self, context):
            ScaleY(context)
            return {'FINISHED'}
    
    
    
    # Align Scale Z
    class 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 AlignUi(Panel):
    
        bl_space_type = 'VIEW_3D'
        bl_region_type = 'TOOLS'
        bl_label = "Align Tools"
        bl_context = "objectmode"
        bl_category = 'Tools'
        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:", icon='MANIPUL')
    
            col = layout.column(align=False)
    
            col.operator("object.align", text="XYZ")
    
    
            col = layout.column()
            col.label(text="Align Location:", icon='MAN_TRANS')
    
    
            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:", icon='MAN_ROT')
    
    
            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:", icon='MAN_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")
                layout = self.layout
                self.layout.operator("object.align_tools", text="Advanced")
    
    
    # Add-ons Preferences Update Panel
    
    # Define Panel classes for updating
    panels = (
            AlignUi,
            )
    
    def update_panel(self, context):
    
        message = "Align Tools: Updating Panel locations has failed"
    
            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))
    
    
    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="Tools",
    
                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="")
    
    
    classes = (
    
        AlignUi,
        AlignOperator,
        AlignLocationOperator,
        AlignLocationXOperator,
        AlignLocationYOperator,
        AlignLocationZOperator,
        AlignRotationOperator,
        AlignRotationXOperator,
        AlignRotationYOperator,
        AlignRotationZOperator,
        AlignScaleOperator,
        AlignScaleXOperator,
        AlignScaleYOperator,
        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()