Files
blender-portable-repo/scripts/addons/atomic_data_manager/ops/main_ops.py
T
2026-03-17 14:30:01 -06:00

445 lines
13 KiB
Python

"""
Copyright (C) 2019 Remington Creative
This file is part of Atomic Data Manager.
Atomic Data Manager 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 3 of the
License, or (at your option) any later version.
Atomic Data Manager 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 Atomic Data Manager. If not, see <https://www.gnu.org/licenses/>.
---
This file contains the main operators found in the main panel of the
Atomic Data Manager interface. This includes nuke, clean, undo, and the
various selection operations.
"""
import bpy
from bpy.utils import register_class
from bpy.utils import unregister_class
from atomic_data_manager.stats import unused
from atomic_data_manager.ops.utils import clean
from atomic_data_manager.ops.utils import nuke
from atomic_data_manager.ui.utils import ui_layouts
# Atomic Data Manager Nuke Operator
class ATOMIC_OT_nuke(bpy.types.Operator):
"""Remove all data-blocks from the selected categories"""
bl_idname = "atomic.nuke"
bl_label = "CAUTION!"
def draw(self, context):
atom = bpy.context.scene.atomic
layout = self.layout
col = layout.column()
col.label(text="Remove the following data-blocks?")
# No Data Section
if not (atom.collections or atom.images or atom.lights or
atom.materials or atom.node_groups or atom.particles or
atom.textures or atom.worlds):
ui_layouts.box_list(
layout=layout,
)
# display when the main panel collections property is toggled
if atom.collections:
collections = sorted(bpy.data.collections.keys())
ui_layouts.box_list(
layout=layout,
title="Collections",
items=collections,
icon="OUTLINER_OB_GROUP_INSTANCE"
)
# display when the main panel images property is toggled
if atom.images:
images = sorted(bpy.data.images.keys())
ui_layouts.box_list(
layout=layout,
title="Images",
items=images,
icon="IMAGE_DATA"
)
# display when the main panel lights property is toggled
if atom.lights:
lights = sorted(bpy.data.lights.keys())
ui_layouts.box_list(
layout=layout,
title="Lights",
items=lights,
icon="OUTLINER_OB_LIGHT"
)
# display when the main panel materials property is toggled
if atom.materials:
materials = sorted(bpy.data.materials.keys())
ui_layouts.box_list(
layout=layout,
title="Materials",
items=materials,
icon="MATERIAL"
)
# display when the main panel node groups property is toggled
if atom.node_groups:
node_groups = sorted(bpy.data.node_groups.keys())
ui_layouts.box_list(
layout=layout,
title="Node Groups",
items=node_groups,
icon="NODETREE"
)
# display when the main panel particle systems property is toggled
if atom.particles:
particles = sorted(bpy.data.particles.keys())
ui_layouts.box_list(
layout=layout,
title="Particle Systems",
items=particles,
icon="PARTICLES"
)
# display when the main panel textures property is toggled
if atom.textures:
textures = sorted(bpy.data.textures.keys())
ui_layouts.box_list(
layout=layout,
title="Textures",
items=textures,
icon="TEXTURE"
)
# display when the main panel worlds property is toggled
if atom.worlds:
worlds = sorted(bpy.data.worlds.keys())
ui_layouts.box_list(
layout=layout,
title="Worlds",
items=worlds,
icon="WORLD"
)
row = layout.row() # extra spacing
def execute(self, context):
atom = bpy.context.scene.atomic
if atom.collections:
nuke.collections()
if atom.images:
nuke.images()
if atom.lights:
nuke.lights()
if atom.materials:
nuke.materials()
if atom.node_groups:
nuke.node_groups()
if atom.particles:
nuke.particles()
if atom.textures:
nuke.textures()
if atom.worlds:
nuke.worlds()
bpy.ops.atomic.deselect_all()
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
# Atomic Data Manager Clean Operator
class ATOMIC_OT_clean(bpy.types.Operator):
"""Remove all unused data-blocks from the selected categories"""
bl_idname = "atomic.clean"
bl_label = "Clean"
unused_collections = []
unused_images = []
unused_lights = []
unused_materials = []
unused_node_groups = []
unused_particles = []
unused_textures = []
unused_worlds = []
def draw(self, context):
atom = bpy.context.scene.atomic
layout = self.layout
col = layout.column()
col.label(text="Remove the following data-blocks?")
# display if no main panel properties are toggled
if not (atom.collections or atom.images or atom.lights or
atom.materials or atom.node_groups or atom.particles
or atom.textures or atom.worlds):
ui_layouts.box_list(
layout=layout,
)
# display when the main panel collections property is toggled
if atom.collections:
ui_layouts.box_list(
layout=layout,
title="Collections",
items=self.unused_collections,
icon="OUTLINER_OB_GROUP_INSTANCE"
)
# display when the main panel images property is toggled
if atom.images:
ui_layouts.box_list(
layout=layout,
title="Images",
items=self.unused_images,
icon="IMAGE_DATA"
)
# display when the main panel lights property is toggled
if atom.lights:
ui_layouts.box_list(
layout=layout,
title="Lights",
items=self.unused_lights,
icon="OUTLINER_OB_LIGHT"
)
# display when the main panel materials property is toggled
if atom.materials:
ui_layouts.box_list(
layout=layout,
title="Materials",
items=self.unused_materials,
icon="MATERIAL"
)
# display when the main panel node groups property is toggled
if atom.node_groups:
ui_layouts.box_list(
layout=layout,
title="Node Groups",
items=self.unused_node_groups,
icon="NODETREE"
)
# display when the main panel particle systems property is toggled
if atom.particles:
ui_layouts.box_list(
layout=layout,
title="Particle Systems",
items=self.unused_particles,
icon="PARTICLES"
)
# display when the main panel textures property is toggled
if atom.textures:
textures = sorted(unused.textures_deep())
ui_layouts.box_list(
layout=layout,
title="Textures",
items=textures,
icon="TEXTURE"
)
# display when the main panel worlds property is toggled
if atom.worlds:
ui_layouts.box_list(
layout=layout,
title="Worlds",
items=self.unused_worlds,
icon="WORLD"
)
row = layout.row() # extra spacing
def execute(self, context):
atom = bpy.context.scene.atomic
if atom.collections:
clean.collections()
if atom.images:
clean.images()
if atom.lights:
clean.lights()
if atom.materials:
clean.materials()
if atom.node_groups:
clean.node_groups()
if atom.particles:
clean.particles()
if atom.textures:
clean.textures()
if atom.worlds:
clean.worlds()
bpy.ops.atomic.deselect_all()
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
atom = bpy.context.scene.atomic
if atom.collections:
self.unused_collections = unused.collections_deep()
if atom.images:
self.unused_images = unused.images_deep()
if atom.lights:
self.unused_lights = unused.lights_deep()
if atom.materials:
self.unused_materials = unused.materials_deep()
if atom.node_groups:
self.unused_node_groups = unused.node_groups_deep()
if atom.particles:
self.unused_particles = unused.particles_deep()
if atom.textures:
self.unused_textures = unused.textures_deep()
if atom.worlds:
self.unused_worlds = unused.worlds()
return wm.invoke_props_dialog(self)
# Atomic Data Manager Undo Operator
class ATOMIC_OT_undo(bpy.types.Operator):
"""Undo the previous action"""
bl_idname = "atomic.undo"
bl_label = "Undo"
def execute(self, context):
bpy.ops.ed.undo()
return {'FINISHED'}
# Atomic Data Manager Smart Select Operator
class ATOMIC_OT_smart_select(bpy.types.Operator):
"""Auto-select categories with unused data"""
bl_idname = "atomic.smart_select"
bl_label = "Smart Select"
def execute(self, context):
bpy.context.scene.atomic.collections = \
any(unused.collections_deep())
bpy.context.scene.atomic.images = \
any(unused.images_deep())
bpy.context.scene.atomic.lights = \
any(unused.lights_deep())
bpy.context.scene.atomic.materials = \
any(unused.materials_deep())
bpy.context.scene.atomic.node_groups = \
any(unused.node_groups_deep())
bpy.context.scene.atomic.particles = \
any(unused.particles_deep())
bpy.context.scene.atomic.textures = \
any(unused.textures_deep())
bpy.context.scene.atomic.worlds = \
any(unused.worlds())
return {'FINISHED'}
# Atomic Data Manager Select All Operator
class ATOMIC_OT_select_all(bpy.types.Operator):
"""Select all categories"""
bl_idname = "atomic.select_all"
bl_label = "Select All"
def execute(self, context):
bpy.context.scene.atomic.collections = True
bpy.context.scene.atomic.images = True
bpy.context.scene.atomic.lights = True
bpy.context.scene.atomic.materials = True
bpy.context.scene.atomic.node_groups = True
bpy.context.scene.atomic.particles = True
bpy.context.scene.atomic.textures = True
bpy.context.scene.atomic.worlds = True
return {'FINISHED'}
# Atomic Data Manager Deselect All Operator
class ATOMIC_OT_deselect_all(bpy.types.Operator):
"""Deselect all categories"""
bl_idname = "atomic.deselect_all"
bl_label = "Deselect All"
def execute(self, context):
bpy.context.scene.atomic.collections = False
bpy.context.scene.atomic.images = False
bpy.context.scene.atomic.lights = False
bpy.context.scene.atomic.materials = False
bpy.context.scene.atomic.node_groups = False
bpy.context.scene.atomic.particles = False
bpy.context.scene.atomic.textures = False
bpy.context.scene.atomic.worlds = False
return {'FINISHED'}
reg_list = [
ATOMIC_OT_nuke,
ATOMIC_OT_clean,
ATOMIC_OT_undo,
ATOMIC_OT_smart_select,
ATOMIC_OT_select_all,
ATOMIC_OT_deselect_all
]
def register():
for item in reg_list:
register_class(item)
def unregister():
for item in reg_list:
unregister_class(item)