Skip to content
Snippets Groups Projects
operators.py 45.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    import bpy
    from bpy.types import Operator
    from bpy.props import (
        BoolProperty,
        StringProperty,
        IntProperty
        )
    
    from .internals import *
    
    rto_history = {"exclude": {},
                   "exclude_all": {},
                   "select": {},
                   "select_all": {},
                   "hide": {},
                   "hide_all": {},
                   "disable": {},
                   "disable_all": {},
                   "render": {},
                   "render_all": {}
                   }
    
    class ExpandAllOperator(bpy.types.Operator):
        '''Expand/Collapse all collections'''
        bl_label = "Expand All Items"
        bl_idname = "view3d.expand_all_items"
        bl_options = {'REGISTER', 'UNDO'}
        
        def execute(self, context):
            if len(expanded) > 0:
                expanded.clear()
            else:
                for laycol in layer_collections.values():
                    if laycol["ptr"].children:
                        expanded.append(laycol["name"])
            
            # update tree view
            update_property_group(context)
            
            return {'FINISHED'}
    
    
    class ExpandSublevelOperator(bpy.types.Operator):
        '''  * Shift-Click to expand/collapse all sublevels'''
        bl_label = "Expand Sublevel Items"
        bl_idname = "view3d.expand_sublevel"
        bl_options = {'REGISTER', 'UNDO'}
        
        expand: BoolProperty()
        name: StringProperty()
        index: IntProperty()
        
        def invoke(self, context, event):
            if event.shift:
                # 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.append(self.name)
                    expand = True
                
                # do expanding/collapsing
                def loop(laycol):
                    for item in laycol.children:
                        if expand:
                            if not item.name in expanded:
                                expanded.append(item.name)
                        else:
                            if item.name in expanded:
                                expanded.remove(item.name)
                            
                        if len(item.children) > 0:
                            loop(item)
                
                loop(layer_collections[self.name]["ptr"])
                
            else:
                # expand/collapse collection
                if self.expand:
                    expanded.append(self.name)
                else:
                    expanded.remove(self.name)
            
            
            # set selected row to the collection you're expanding/collapsing and update tree view
            context.scene.CMListIndex = self.index
            update_property_group(context)
            
            return {'FINISHED'}
    
    
    class CMSetCollectionOperator(bpy.types.Operator):
        '''  * Click to move object to collection.\n  * Shift-Click to add/remove object from collection'''
        bl_label = "Set Object Collection"
        bl_idname = "view3d.set_collection"
        bl_options = {'REGISTER', 'UNDO'}
        
        collection_index: IntProperty()
        collection_name: StringProperty()
        
        def invoke(self, context, event):
            collection = layer_collections[self.collection_name]["ptr"].collection
            
            if event.shift:
                # add object to collection
                
                # check if in collection
                in_collection = True
                
                for obj in context.selected_objects:
                    if obj.name not in collection.objects:
                        in_collection = False
                
                if not in_collection:
                    # add to collection
                    bpy.ops.object.link_to_collection(collection_index=self.collection_index)
                    
                else:
                    # check and disallow removing from all collections
                    for obj in context.selected_objects:
                        if len(obj.users_collection) == 1:
                            send_report("Error removing 1 or more objects from this collection.\nObjects would be left without a collection")
                            
                            return {'FINISHED'}
                    
                    # remove from collection
                    bpy.ops.collection.objects_remove(collection=collection.name)
            
            else:
                # move object to collection
                bpy.ops.object.move_to_collection(collection_index=self.collection_index)
            
            return {'FINISHED'}
    
    
    class CMExcludeOperator(bpy.types.Operator):
        '''  * Shift-Click to isolate/restore previous state\n  * Ctrl-Click to toggle children'''
        bl_label = "Exclude Collection from View Layer"
        bl_idname = "view3d.exclude_collection"
        bl_options = {'REGISTER', 'UNDO'}
        
        name: StringProperty()
        
        def invoke(self, context, event):
            global rto_history
            
            view_layer = context.view_layer.name
            laycol_ptr = layer_collections[self.name]["ptr"]
            
            if not view_layer in rto_history["exclude"]:
                rto_history["exclude"][view_layer] = {"target": "", "history": []}
            
            rto_history["exclude"][view_layer]["target"] = self.name
            exclude_history = rto_history["exclude"][view_layer]["history"]
            
            if event.shift:
                # isolate/de-isolate exclusion of collections
                
                # get active layer collections
                active_layer_collections = [x for x in layer_collections.values() \
                                              if x["ptr"].exclude == False]
                
                # check if collection isolated
                if len(active_layer_collections) == 1 and active_layer_collections[0]["name"] == self.name:
                    if len(exclude_history) > 1:
                        # restore previous state
                        for x, item in enumerate(layer_collections.values()):
                            item["ptr"].exclude = exclude_history[x]
                    
                    else:
                        # enable all collections
                        for item in layer_collections.values():
                            item["ptr"].exclude = False
                    
                    # reset exclude history
                    del rto_history["exclude"][view_layer]
                
                else:
                    # isolate collection
                    
                    # reset exclude history
                    exclude_history.clear()
                    
                    # save state
                    keep_history = -1
                    for item in layer_collections.values():
                        exclude_history.append(item["ptr"].exclude)
                        
                        if item["ptr"].exclude == False:
                            keep_history += 1
                    
                    if not keep_history:
                        del rto_history["exclude"][view_layer]
                    
                    
                    # isolate collection
                    for item in layer_collections.values():
                        if item["name"] != laycol_ptr.name:
                            item["ptr"].exclude = True
                    
                    laycol_ptr.exclude = False
                    
                    # exclude all children
                    laycol_iter_list = [laycol_ptr.children]
                    while len(laycol_iter_list) > 0:
                        new_laycol_iter_list = []
                        for laycol_iter in laycol_iter_list:
                            for layer_collection in laycol_iter:
                                layer_collection.exclude = True
                                if len(layer_collection.children) > 0:
                                    new_laycol_iter_list.append(layer_collection.children)
                        
                        laycol_iter_list = new_laycol_iter_list
                                
            
            elif event.ctrl:
                # toggle children
                
                # reset exclude history
                del rto_history["exclude"][view_layer]
                
                # toggle exclusion of collection (this propagates to children)
                laycol_ptr.exclude = not laycol_ptr.exclude
            
            else:
                # toggle exclusion
                
                # reset exclude history
                del rto_history["exclude"][view_layer]
                
                
                # get current child exclusion state
                child_exclusion = []
                
                laycol_iter_list = [laycol_ptr.children]
                while len(laycol_iter_list) > 0:
                    new_laycol_iter_list = []
                    for laycol_iter in laycol_iter_list:
                        for layer_collection in laycol_iter:
                            child_exclusion.append([layer_collection, layer_collection.exclude])
                            if len(layer_collection.children) > 0:
                                new_laycol_iter_list.append(layer_collection.children)
                    
                    laycol_iter_list = new_laycol_iter_list
                
                
                # toggle exclusion of collection
                laycol_ptr.exclude = not laycol_ptr.exclude
                
                
                # set correct state for all children
                for laycol in child_exclusion:
                    laycol[0].exclude = laycol[1]
                
            
            # reset exclude all history
            if view_layer in rto_history["exclude_all"]:
                del rto_history["exclude_all"][view_layer]
            
            return {'FINISHED'}
    
    
    class CMUnExcludeAllOperator(bpy.types.Operator):
        '''  * Click to toggle between current excluded state and all included.\n  * Shift-Click to invert excluded status of all collections'''
        bl_label = "Toggle Excluded Status Of All Collections"
        bl_idname = "view3d.un_exclude_all_collections"
        bl_options = {'REGISTER', 'UNDO'}
        
        def invoke(self, context, event):
            global rto_history
            
            view_layer = context.view_layer.name
            
            if not view_layer in rto_history["exclude_all"]:
                rto_history["exclude_all"][view_layer] = []
            
            exclude_all_history = rto_history["exclude_all"][view_layer]
            
            if len(exclude_all_history) == 0:
                exclude_all_history.clear()
                keep_history = False
                
                if event.shift:
                    for item in layer_collections.values():
                        keep_history = True
                        exclude_all_history.append(item["ptr"].exclude)
                    
                    for x, item in enumerate(layer_collections.values()):
                        item["ptr"].exclude = not exclude_all_history[x]
                    
                    print([not i for i in exclude_all_history])
                
                else:
                    for item in reversed(list(layer_collections.values())):
                        if item["ptr"].exclude:
                            keep_history = True
                        
                        exclude_all_history.append(item["ptr"].exclude)
                        
                        item["ptr"].exclude = False
                        
                    exclude_all_history.reverse()
                
                if not keep_history:
                    del rto_history["exclude_all"][view_layer]
            
            else:
                for x, item in enumerate(layer_collections.values()):
                    item["ptr"].exclude = exclude_all_history[x]
                
                del rto_history["exclude_all"][view_layer]
            
            return {'FINISHED'}
    
    
    class CMRestrictSelectOperator(bpy.types.Operator):
        '''  * Shift-Click to isolate/restore previous state\n  * Ctrl-Click to toggle children'''
        bl_label = "Disable Selection of Collection"
        bl_idname = "view3d.restrict_select_collection"
        bl_options = {'REGISTER', 'UNDO'}
        
        name: StringProperty()
        
        def invoke(self, context, event):
            global rto_history
            
            view_layer = context.view_layer.name
            laycol_ptr = layer_collections[self.name]["ptr"]
            
            if not view_layer in rto_history["select"]:
                rto_history["select"][view_layer] = {"target": "", "history": []}
            
            rto_history["select"][view_layer]["target"] = self.name
            select_history = rto_history["select"][view_layer]["history"]
            
            if event.shift:
                # isolate/de-isolate selectability of collections
                
                # get active collections
                active_layer_collections = [x for x in layer_collections.values() \
                                              if x["ptr"].collection.hide_select == False]
                
                layerchain = []
                laycol = layer_collections[self.name]
                
                # get chain of parents up to top level collection
                while laycol["id"] != 0:
                        layerchain.append(laycol)
                        laycol = laycol["parent"]
                        
                # check if reversed layerchain matches active collections
                if layerchain[::-1] == active_layer_collections:
                    if len(select_history) > 1:
                        # restore previous state
                        for x, item in enumerate(layer_collections.values()):
                            item["ptr"].collection.hide_select = select_history[x]
                    
                    else:
                        # make all collections selectable
                        for item in layer_collections.values():
                            item["ptr"].collection.hide_select = False
                
                    # reset select history
                    del rto_history["select"][view_layer]
                
                else:
                    # reset select history
                    select_history.clear()
                    
                    # save state
                    keep_history = -1
                    for item in layer_collections.values():
                        select_history.append(item["ptr"].collection.hide_select)
                        
                        if item["ptr"].collection.hide_select == False:
                            keep_history += 1
                    
                    if not keep_history:
                        del rto_history["select"][view_layer]
                    
                    # make all collections unselectable
                    for item in layer_collections.values():
                        item["ptr"].collection.hide_select = True
                    
                    # allow selection of active collection plus parents
                    laycol_ptr.collection.hide_select = False
                    
                    laycol = layer_collections[self.name]
                    while laycol["id"] != 0:
                        laycol["ptr"].collection.hide_select = False
                        laycol = laycol["parent"]
                                
            
            elif event.ctrl:
                # toggle children
                
                # reset select history
                del rto_history["select"][view_layer]
                
                # toggle selectability of collection
                state = not laycol_ptr.collection.hide_select
                laycol_ptr.collection.hide_select = state
                
                # pass state to children
                laycol_iter_list = [laycol_ptr.children]
                while len(laycol_iter_list) > 0:
                    new_laycol_iter_list = []
                    for laycol_iter in laycol_iter_list:
                        for layer_collection in laycol_iter:
                            layer_collection.collection.hide_select = state
                            if len(layer_collection.children) > 0:
                                new_laycol_iter_list.append(layer_collection.children)
                    
                    laycol_iter_list = new_laycol_iter_list
            
            else:
                # reset select history
                del rto_history["select"][view_layer]
                
                # toggle selectability of collection
                laycol_ptr.collection.hide_select = not laycol_ptr.collection.hide_select
            
            
            # reset select all history
            if view_layer in rto_history["select_all"]:
                del rto_history["select_all"][view_layer]
            
            return {'FINISHED'}
    
    
    class CMUnRestrictSelectAllOperator(bpy.types.Operator):
        '''  * Click to toggle between current selectable state and all selectable.\n  * Shift-Click to invert selectable status of all collections'''
        bl_label = "Toggle Selectable Status Of All Collections"
        bl_idname = "view3d.un_restrict_select_all_collections"
        bl_options = {'REGISTER', 'UNDO'}
        
        def invoke(self, context, event):
            global rto_history
            
            view_layer = context.view_layer.name
    
            if not view_layer in rto_history["select_all"]:
                rto_history["select_all"][view_layer] = []
    
            select_all_history = rto_history["select_all"][view_layer]
            
            if len(select_all_history) == 0:
                select_all_history.clear()
                keep_history = False
                
                for item in layer_collections.values():
                    if event.shift:
                        keep_history = True
                        select_all_history.append(item["ptr"].collection.hide_select)
                        item["ptr"].collection.hide_select = not item["ptr"].collection.hide_select
                    
                    else:
                        if item["ptr"].collection.hide_select:
                            keep_history = True
                            
                        select_all_history.append(item["ptr"].collection.hide_select)
                        item["ptr"].collection.hide_select = False
                
                if not keep_history:
                    del rto_history["select_all"][view_layer]
            
            else:
                for x, item in enumerate(layer_collections.values()):
                    item["ptr"].collection.hide_select = select_all_history[x]
                
                del rto_history["select_all"][view_layer]
            
            return {'FINISHED'}
    
    
    class CMHideOperator(bpy.types.Operator):
        '''  * Shift-Click to isolate/restore previous state\n  * Ctrl-Click to toggle children'''
        bl_label = "Hide Collection"
        bl_idname = "view3d.hide_collection"
        bl_options = {'REGISTER', 'UNDO'}
        
        name: StringProperty()
        
        def invoke(self, context, event):
            global rto_history
            
            view_layer = context.view_layer.name
            laycol_ptr = layer_collections[self.name]["ptr"]
            
            if not view_layer in rto_history["hide"]:
                rto_history["hide"][view_layer] = {"target": "", "history": []}
            
            rto_history["hide"][view_layer]["target"] = self.name
            hide_history = rto_history["hide"][view_layer]["history"]
            
            if event.shift:
                # isolate/de-isolate view of collections
                
                # get active collections
                active_layer_collections = [x for x in layer_collections.values() \
                                              if x["ptr"].hide_viewport == False]
                
                layerchain = []
                laycol = layer_collections[self.name]
                
                # get chain of parents up to top level collection
                while laycol["id"] != 0:
                        layerchain.append(laycol)
                        laycol = laycol["parent"]
                        
                # check if reversed layerchain matches active collections
                if layerchain[::-1] == active_layer_collections:
                    if len(hide_history) > 1:
                        # restore previous state
                        for x, item in enumerate(layer_collections.values()):
                            item["ptr"].hide_viewport = hide_history[x]
                    
                    else:
                        # show all collections
                        for laycol in layer_collections.values():
                            laycol["ptr"].hide_viewport = False
                        
                    # reset hide history
                    del rto_history["hide"][view_layer]
                    
                else:
                    # reset hide history
                    hide_history.clear()
                    
                    # save state
                    keep_history = -1
                    for item in layer_collections.values():
                        hide_history.append(item["ptr"].hide_viewport)
                        
                        if item["ptr"].hide_viewport == False:
                            keep_history += 1
                    
                    if not keep_history:
                        del rto_history["hide"][view_layer]
                    
                    # hide all collections
                    for laycol in layer_collections.values():
                        laycol["ptr"].hide_viewport = True
                    
                    # show active collection plus parents
                    laycol_ptr.hide_viewport = False
                    
                    laycol = layer_collections[self.name]
                    while laycol["id"] != 0:
                        laycol["ptr"].hide_viewport = False
                        laycol = laycol["parent"]
            
            elif event.ctrl:
                # toggle children
                
                # reset hide history
                del rto_history["hide"][view_layer]
                
                # toggle view of collection
                state = not laycol_ptr.hide_viewport
                laycol_ptr.hide_viewport = state
                
                # pass state to children
                laycol_iter_list = [laycol_ptr.children]
                while len(laycol_iter_list) > 0:
                    new_laycol_iter_list = []
                    for laycol_iter in laycol_iter_list:
                        for layer_collection in laycol_iter:
                            layer_collection.hide_viewport = state
                            if len(layer_collection.children) > 0:
                                new_laycol_iter_list.append(layer_collection.children)
                    
                    laycol_iter_list = new_laycol_iter_list
            
            else:
                # reset hide history
                del rto_history["hide"][view_layer]
                
                # toggle view of collection
                laycol_ptr.hide_viewport = not laycol_ptr.hide_viewport
            
            
            # reset hide all history
            if view_layer in rto_history["hide_all"]:
                del rto_history["hide_all"][view_layer]
            
            return {'FINISHED'}
    
    
    class CMUnHideAllOperator(bpy.types.Operator):
        '''  * Click to toggle between current visibility state and all visible.\n  * Shift-Click to invert visibility status of all collections'''
        bl_label = "Toggle Hidden Status Of All Collections"
        bl_idname = "view3d.un_hide_all_collections"
        bl_options = {'REGISTER', 'UNDO'}
        
        def invoke(self, context, event):
            global rto_history
            
            view_layer = context.view_layer.name
    
            if not view_layer in rto_history["hide_all"]:
                rto_history["hide_all"][view_layer] = []
    
            hide_all_history = rto_history["hide_all"][view_layer]
            
            if len(hide_all_history) == 0:
                hide_all_history.clear()
                keep_history = False
                
                for item in layer_collections.values():
                    if event.shift:
                        keep_history = True
                        hide_all_history.append(item["ptr"].hide_viewport)
                        item["ptr"].hide_viewport = not item["ptr"].hide_viewport
                    
                    else:
                        if item["ptr"].hide_viewport:
                            keep_history = True
                            
                        hide_all_history.append(item["ptr"].hide_viewport)
                        item["ptr"].hide_viewport = False
                
                if not keep_history:
                    del rto_history["hide_all"][view_layer]
            
            else:
                for x, item in enumerate(layer_collections.values()):
                    item["ptr"].hide_viewport = hide_all_history[x]
                
                del rto_history["hide_all"][view_layer]
            
            return {'FINISHED'}
    
    
    class CMDisableViewportOperator(bpy.types.Operator):
        '''  * Shift-Click to isolate/restore previous state\n  * Ctrl-Click to toggle children'''
        bl_label = "Disable Collection in Viewport"
        bl_idname = "view3d.disable_viewport_collection"
        bl_options = {'REGISTER', 'UNDO'}
        
        name: StringProperty()
        
        def invoke(self, context, event):
            global rto_history
            
            view_layer = context.view_layer.name
            laycol_ptr = layer_collections[self.name]["ptr"]
            
            if not view_layer in rto_history["disable"]:
                rto_history["disable"][view_layer] = {"target": "", "history": []}
            
            rto_history["disable"][view_layer]["target"] = self.name
            disable_history = rto_history["disable"][view_layer]["history"]
            
            if event.shift:
                # isolate/de-isolate disablement of collections in viewport
                
                # get active collections
                active_layer_collections = [x for x in layer_collections.values() \
                                              if x["ptr"].collection.hide_viewport == False]
                
                layerchain = []
                laycol = layer_collections[self.name]
                
                # get chain of parents up to top level collection
                while laycol["id"] != 0:
                        layerchain.append(laycol)
                        laycol = laycol["parent"]
                        
                # check if reversed layerchain matches active collections
                if layerchain[::-1] == active_layer_collections:
                    if len(disable_history) > 1:
                        # restore previous state
                        for x, item in enumerate(layer_collections.values()):
                            item["ptr"].collection.hide_viewport = disable_history[x]
                    
                    else:
                        # enable all collections in viewport
                        for laycol in layer_collections.values():
                            laycol["ptr"].collection.hide_viewport = False
                    
                    # reset disable history
                    del rto_history["disable"][view_layer]
                    
                else:
                    # reset disable history
                    disable_history.clear()
                    
                    # save state
                    keep_history = -1
                    for item in layer_collections.values():
                        disable_history.append(item["ptr"].collection.hide_viewport)
                        
                        if item["ptr"].collection.hide_viewport == False:
                            keep_history += 1
                    
                    if not keep_history:
                        del rto_history["disable"][view_layer]
                    
                    # disable all collections in viewport
                    for laycol in layer_collections.values():
                        laycol["ptr"].collection.hide_viewport = True
                    
                    # enable active collection plus parents in viewport
                    laycol_ptr.collection.hide_viewport = False
                    
                    laycol = layer_collections[self.name]
                    while laycol["id"] != 0:
                        laycol["ptr"].collection.hide_viewport = False
                        laycol = laycol["parent"]
            
            elif event.ctrl:
                # toggle children
                
                # reset disable history
                del rto_history["disable"][view_layer]
                
                # toggle view of collection
                state = not laycol_ptr.collection.hide_viewport
                laycol_ptr.collection.hide_viewport = state
                
                # pass state to children
                laycol_iter_list = [laycol_ptr.children]
                while len(laycol_iter_list) > 0:
                    new_laycol_iter_list = []
                    for laycol_iter in laycol_iter_list:
                        for layer_collection in laycol_iter:
                            layer_collection.collection.hide_viewport = state
                            if len(layer_collection.children) > 0:
                                new_laycol_iter_list.append(layer_collection.children)
                    
                    laycol_iter_list = new_laycol_iter_list
            
            else:
                # reset disable history
                del rto_history["disable"][view_layer]
                
                # toggle disable of collection in viewport
                laycol_ptr.collection.hide_viewport = not laycol_ptr.collection.hide_viewport
            
            
            # reset disable all history
            if view_layer in rto_history["disable_all"]:
                del rto_history["disable_all"][view_layer]
            
            return {'FINISHED'}
    
    
    class CMUnDisableViewportAllOperator(bpy.types.Operator):
        '''  * Click to toggle between current viewport display and all enabled.\n  * Shift-Click to invert viewport display of all collections'''
        bl_label = "Toggle Viewport Display of All Collections"
        bl_idname = "view3d.un_disable_viewport_all_collections"
        bl_options = {'REGISTER', 'UNDO'}
        
        def invoke(self, context, event):
            global rto_history
            
            view_layer = context.view_layer.name
    
            if not view_layer in rto_history["disable_all"]:
                rto_history["disable_all"][view_layer] = []
    
            disable_all_history = rto_history["disable_all"][view_layer]
            
            if len(disable_all_history) == 0:
                disable_all_history.clear()
                keep_history = False
                
                for item in layer_collections.values():
                    if event.shift:
                        keep_history = True
                        disable_all_history.append(item["ptr"].collection.hide_viewport)
                        item["ptr"].collection.hide_viewport = not \
                            item["ptr"].collection.hide_viewport
                    
                    else:
                        if item["ptr"].collection.hide_viewport:
                            keep_history = True
                            
                        disable_all_history.append(item["ptr"].collection.hide_viewport)
                        item["ptr"].collection.hide_viewport = False
                
                if not keep_history:
                    del rto_history["disable_all"][view_layer]
            
            else:
                for x, item in enumerate(layer_collections.values()):
                    item["ptr"].collection.hide_viewport = disable_all_history[x]
                
                del rto_history["disable_all"][view_layer]
            
            return {'FINISHED'}
    
    
    class CMDisableRenderOperator(bpy.types.Operator):
        '''  * Shift-Click to isolate/restore previous state\n  * Shift-Click to invert viewport display of all collections'''
        bl_label = "Disable Collection in Render"
        bl_idname = "view3d.disable_render_collection"
        bl_options = {'REGISTER', 'UNDO'}
        
        name: StringProperty()
        
        def invoke(self, context, event):
            global rto_history
            
            view_layer = context.view_layer.name
            laycol_ptr = layer_collections[self.name]["ptr"]
            
            if not view_layer in rto_history["render"]:
                rto_history["render"][view_layer] = {"target": "", "history": []}
    
            rto_history["render"][view_layer]["target"] = self.name
            render_history = rto_history["render"][view_layer]["history"]
            
            if event.shift:
                # isolate/de-isolate render of collections
                
                # get active collections
                active_layer_collections = [x for x in layer_collections.values() \
                                              if x["ptr"].collection.hide_render == False]
                
                layerchain = []
                laycol = layer_collections[self.name]
                
                # get chain of parents up to top level collection
                while laycol["id"] != 0:
                        layerchain.append(laycol)
                        laycol = laycol["parent"]
                        
                # check if reversed layerchain matches active collections
                if layerchain[::-1] == active_layer_collections:
                    if len(render_history) > 1:
                        # restore previous state
                        for x, item in enumerate(layer_collections.values()):
                            item["ptr"].collection.hide_render = render_history[x]
                    
                    else:
                        # allow render of all collections
                        for laycol in layer_collections.values():
                            laycol["ptr"].collection.hide_render = False
                    
                    # reset render history
                    del rto_history["render"][view_layer]
                    
                else:
                    # reset render history
                    render_history.clear()
                    
                    # save state
                    keep_history = -1
                    for item in layer_collections.values():
                        render_history.append(item["ptr"].collection.hide_render)
                        
                        if item["ptr"].collection.hide_render == False:
                            keep_history += 1
                    
                    if not keep_history:
                        del rto_history["render"][view_layer]
                    
                    # disallow render of all collections
                    for laycol in layer_collections.values():
                        laycol["ptr"].collection.hide_render = True
                    
                    # allow render of active collection plus parents
                    laycol_ptr.collection.hide_render = False
                    
                    laycol = layer_collections[self.name]
                    while laycol["id"] != 0:
                        laycol["ptr"].collection.hide_render = False
                        laycol = laycol["parent"]
            
            elif event.ctrl:
                # toggle children
                
                # reset render history
                del rto_history["render"][view_layer]
                
                # toggle view of collection
                state = not laycol_ptr.collection.hide_render
                laycol_ptr.collection.hide_render = state
                
                # pass state to children
                laycol_iter_list = [laycol_ptr.children]
                while len(laycol_iter_list) > 0:
                    new_laycol_iter_list = []
                    for laycol_iter in laycol_iter_list:
                        for layer_collection in laycol_iter:
                            layer_collection.collection.hide_render = state
                            if len(layer_collection.children) > 0:
                                new_laycol_iter_list.append(layer_collection.children)
                    
                    laycol_iter_list = new_laycol_iter_list
            
            else:
                # reset render history
                del rto_history["render"][view_layer]
                
                # toggle renderability of collection
                laycol_ptr.collection.hide_render = not laycol_ptr.collection.hide_render
            
            
            # reset render all history
            if view_layer in rto_history["render_all"]:
                del rto_history["render_all"][view_layer]
            
            return {'FINISHED'}
    
    
    class CMUnDisableRenderAllOperator(bpy.types.Operator):
        '''  * Click to toggle between current render status and all rendered.\n  * Shift-Click to invert render status of all collections'''
        bl_label = "Toggle Render Status of All Collections"
        bl_idname = "view3d.un_disable_render_all_collections"
        bl_options = {'REGISTER', 'UNDO'}
        
        def invoke(self, context, event):
            global rto_history
            
            view_layer = context.view_layer.name
    
            if not view_layer in rto_history["render_all"]:
                rto_history["render_all"][view_layer] = []
    
            render_all_history = rto_history["render_all"][view_layer]
            
            if len(render_all_history) == 0:
                render_all_history.clear()
                keep_history = False
                
                for item in layer_collections.values():
                    if event.shift:
                        keep_history = True
                        render_all_history.append(item["ptr"].collection.hide_render)
                        item["ptr"].collection.hide_render = not \
                            item["ptr"].collection.hide_render
                    
                    else:
                        if item["ptr"].collection.hide_render:
                            keep_history = True
                            
                        render_all_history.append(item["ptr"].collection.hide_render)
                        item["ptr"].collection.hide_render = False
                
                if not keep_history:
                    del rto_history["render_all"][view_layer]
            
            else:
                for x, item in enumerate(layer_collections.values()):
                    item["ptr"].collection.hide_render = render_all_history[x]
                
                del rto_history["render_all"][view_layer]
            
            return {'FINISHED'}
    
    
    class CMRemoveCollectionOperator(bpy.types.Operator):
        '''Remove Collection'''
        bl_label = "Remove Collection"
        bl_idname = "view3d.remove_collection"
        bl_options = {'UNDO'}
        
        collection_name: StringProperty()
        
        def execute(self, context):
            global rto_history
            
            laycol = layer_collections[self.collection_name]
            collection = laycol["ptr"].collection
            laycol_parent = laycol["parent"]
            
            # save state and remove all hiding properties of parent collection
            orig_parent_hide_select = False
            orig_parent_exclude = False
            orig_parent_hide_viewport = False
            
            if laycol_parent["ptr"].collection.hide_select:
                orig_parent_hide_select = True
            
            if laycol_parent["ptr"].exclude:
                orig_parent_exclude = True
            
            if laycol_parent["ptr"].hide_viewport:
                orig_parent_hide_viewport = True
            
            laycol_parent["ptr"].collection.hide_select = False
            laycol_parent["ptr"].exclude = False
            laycol_parent["ptr"].hide_viewport = False
            
            
            # remove all hiding properties of this collection
            collection.hide_select = False
            laycol["ptr"].exclude = False
            laycol["ptr"].hide_viewport = False
            
            
            # shift all objects in this collection to the parent collection
            if collection.objects: