framework/cg/blender/utils/remove_collections.py

37 lines
1.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Copyright (C) 2023 Ilia Kurochkin <brothermechanic@gmail.com>
#
# 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 3 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.
__version__ = '0.1'
import logging
import bpy
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def remove_collections(collection_name=None):
'''Removes all all collection or collection_name only'''
if collection_name:
collection = bpy.data.collections.get(collection_name)
try:
for obj in collection.objects:
bpy.data.objects.remove(obj, do_unlink=True)
bpy.data.collections.remove(collection)
except Exception:
logger.info(f'There is no collection {collection_name!r} in scene')
else:
for collection in bpy.data.collections:
for obj in collection.objects:
bpy.data.objects.remove(obj, do_unlink=True)
bpy.data.collections.remove(collection)