Skip to content
Snippets Groups Projects
operators.py 32.1 KiB
Newer Older
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# Copyright 2011, Ryan Inch

from copy import deepcopy

from bpy.types import (
    Operator,
)

from bpy.props import (
    BoolProperty,
    StringProperty,
    IntProperty
from .internals import (
    expanded,
    layer_collections,
    rto_history,
    expand_history,
    phantom_history,
    copy_buffer,
    swap_buffer,
    update_property_group,
    get_move_selection,
    get_move_active,
from .operator_utils import (
    get_rto,
    set_rto,
    apply_to_children,
    isolate_rto,
    toggle_children,
    activate_all_rtos,
    invert_rtos,
    copy_rtos,
    swap_rtos,
    clear_copy,
    clear_swap,
)

class SetActiveCollection(Operator):
    '''Set the active collection'''
    bl_label = "Set Active Collection"
    bl_idname = "view3d.set_active_collection"

    collection_index: IntProperty()
    collection_name: StringProperty()

    def execute(self, context):
        if self.collection_index == -1:
            layer_collection = context.view_layer.layer_collection

        else:
            laycol = layer_collections[self.collection_name]
            layer_collection = laycol["ptr"]

            # set selection to this row
            cm = context.scene.collection_manager
            cm.cm_list_index = laycol["row_index"]

        context.view_layer.active_layer_collection = layer_collection

        if context.view_layer.active_layer_collection != layer_collection:
            self.report({'WARNING'}, "Can't set excluded collection as active")

class ExpandAllOperator(Operator):
    '''Expand/Collapse all collections'''
    bl_label = "Expand All Items"
    bl_idname = "view3d.expand_all_items"
    bl_options = {'REGISTER', 'UNDO'}
    def execute(self, context):
        global expand_history

        if len(expanded) > 0:
            expanded.clear()
        else:
            for laycol in layer_collections.values():
                if laycol["ptr"].children:
                    expanded.add(laycol["name"])
        # clear expand history
        expand_history["target"] = ""
        expand_history["history"].clear()

        # update tree view
        update_property_group(context)
class ExpandSublevelOperator(Operator):
    bl_label = "Expand Sublevel Items"
    bl_description = (
        "  * Ctrl+LMB - Expand/Collapse all sublevels\n"
        "  * Shift+LMB - Isolate tree/Restore\n"
        "  * Alt+LMB - Discard history"
        )
    bl_idname = "view3d.expand_sublevel"
    bl_options = {'REGISTER', 'UNDO'}
    expand: BoolProperty()
    name: StringProperty()
    index: IntProperty()
    def invoke(self, context, event):
        global expand_history
        cls = ExpandSublevelOperator

        modifiers = get_modifiers(event)

        if modifiers == {"alt"}:
            expand_history["target"] = ""
            expand_history["history"].clear()

        elif modifiers == {"ctrl"}:
            # expand/collapse all subcollections
            expand = None
            # check whether to expand or collapse
            if self.name in expanded:
                expanded.remove(self.name)
                expand = False
            else:
                expanded.add(self.name)
            # do expanding/collapsing
            def set_expanded(layer_collection):
                if expand:
                    expanded.add(layer_collection.name)
                else:
                    expanded.discard(layer_collection.name)
            apply_to_children(layer_collections[self.name]["ptr"], set_expanded)
            expand_history["target"] = ""
            expand_history["history"].clear()

        elif modifiers == {"shift"}:
            def isolate_tree(current_laycol):
                parent = current_laycol["parent"]

                for laycol in parent["children"]:
                    if laycol["name"] != current_laycol["name"] and laycol["name"] in expanded:
                        expanded.remove(laycol["name"])
                        expand_history["history"].append(laycol["name"])

                if parent["parent"]:
                    isolate_tree(parent)

            if self.name == expand_history["target"]:
                for item in expand_history["history"]:
                    expanded.add(item)

                expand_history["target"] = ""
                expand_history["history"].clear()

            else:
                expand_history["target"] = ""
                expand_history["history"].clear()

                isolate_tree(layer_collections[self.name])
                expand_history["target"] = self.name

        else:
            # expand/collapse collection
            if self.expand:
                expanded.add(self.name)
Loading
Loading full blame...