Skip to content
Snippets Groups Projects
operators.py 73.7 KiB
Newer Older
  • Learn to ignore specific revisions
  •     '''Add New Collection'''
        bl_label = "Add New Collection"
        bl_idname = "view3d.add_collection"
        bl_options = {'UNDO'}
    
        child: BoolProperty()
    
        def execute(self, context):
            global rto_history
    
            new_collection = bpy.data.collections.new('Collection')
    
            cm = context.scene.collection_manager
    
    
            # if there are collections
    
            if len(cm.cm_list_collection) > 0:
    
                # get selected collection
    
                laycol = layer_collections[cm.cm_list_collection[cm.cm_list_index].name]
    
                # add new collection
                if self.child:
                    laycol["ptr"].collection.children.link(new_collection)
                    expanded.append(laycol["name"])
    
                    # update tree view property
                    update_property_group(context)
    
                    cm.cm_list_index = layer_collections[new_collection.name]["row_index"]
    
                else:
                    laycol["parent"]["ptr"].collection.children.link(new_collection)
    
                    # update tree view property
                    update_property_group(context)
    
                    cm.cm_list_index = layer_collections[new_collection.name]["row_index"]
    
            # if no collections add top level collection and select it
            else:
    
                context.scene.collection.children.link(new_collection)
    
                # update tree view property
                update_property_group(context)
    
                cm.cm_list_index = 0
    
            global rename
            rename[0] = True
    
            # reset history
            for rto in rto_history.values():
                rto.clear()
    
            return {'FINISHED'}
    
    
    phantom_history = {"view_layer": "",
                       "initial_state": {},
    
                       "exclude_history": {},
                       "select_history": {},
                       "hide_history": {},
                       "disable_history": {},
                       "render_history": {},
    
                       "exclude_all_history": [],
                       "select_all_history": [],
                       "hide_all_history": [],
                       "disable_all_history": [],
                       "render_all_history": []
                       }
    
    
    class CMPhantomModeOperator(Operator):
    
        '''Toggle Phantom Mode'''
        bl_label = "Toggle Phantom Mode"
        bl_idname = "view3d.toggle_phantom_mode"
    
        def execute(self, context):
            global phantom_history
            global rto_history
    
            cm = context.scene.collection_manager
            view_layer = context.view_layer
    
            # enter Phantom Mode
    
            if not cm.in_phantom_mode:
    
                cm.in_phantom_mode = True
    
                # save current visibility state
    
                phantom_history["view_layer"] = view_layer.name
    
                laycol_iter_list = [view_layer.layer_collection.children]
    
                while len(laycol_iter_list) > 0:
                    new_laycol_iter_list = []
                    for laycol_iter in laycol_iter_list:
                        for layer_collection in laycol_iter:
                            phantom_history["initial_state"][layer_collection.name] = {
                                "exclude": layer_collection.exclude,
                                "select": layer_collection.collection.hide_select,
                                "hide": layer_collection.hide_viewport,
                                "disable": layer_collection.collection.hide_viewport,
                                "render": layer_collection.collection.hide_render,
                                    }
    
                            if len(layer_collection.children) > 0:
                                new_laycol_iter_list.append(layer_collection.children)
    
                    laycol_iter_list = new_laycol_iter_list
    
                # save current rto history
                for rto, history, in rto_history.items():
    
                    if history.get(view_layer.name, None):
                        phantom_history[rto+"_history"] = deepcopy(history[view_layer.name])
    
            # return to normal mode
            else:
    
                laycol_iter_list = [view_layer.layer_collection.children]
    
                while len(laycol_iter_list) > 0:
                    new_laycol_iter_list = []
                    for laycol_iter in laycol_iter_list:
                        for layer_collection in laycol_iter:
                            phantom_laycol = phantom_history["initial_state"][layer_collection.name]
    
                            layer_collection.exclude = \
                                phantom_laycol["exclude"]
    
                            layer_collection.collection.hide_select = \
                                phantom_laycol["select"]
    
                            layer_collection.hide_viewport = \
                                phantom_laycol["hide"]
    
                            layer_collection.collection.hide_viewport = \
                                phantom_laycol["disable"]
    
                            layer_collection.collection.hide_render = \
                                phantom_laycol["render"]
    
                            if len(layer_collection.children) > 0:
                                new_laycol_iter_list.append(layer_collection.children)
    
                    laycol_iter_list = new_laycol_iter_list
    
                # restore previous rto history
                for rto, history, in rto_history.items():
    
                    if view_layer.name in history:
                        del history[view_layer.name]
    
                    if phantom_history[rto+"_history"]:
    
                        history[view_layer.name] = deepcopy(phantom_history[rto+"_history"])
    
                    phantom_history[rto+"_history"].clear()
    
                cm.in_phantom_mode = False
    
            return {'FINISHED'}