69 lines
2.9 KiB
Python
69 lines
2.9 KiB
Python
import os
|
|
import glob
|
|
from django.core.management.base import BaseCommand
|
|
from django.core.files import File
|
|
import logging
|
|
from object.models import Scene3D, Element3D
|
|
|
|
logger = logging.getLogger(__name__) # Инициализация логгера
|
|
|
|
class Command(BaseCommand):
|
|
help = "Import GLB files into the database and associate them with a specific Scene3D object."
|
|
|
|
def handle(self, *args, **options):
|
|
logger.info("Starting the import process...")
|
|
|
|
# Определение корневой директории
|
|
base_dir = os.path.dirname(os.path.abspath(__file__)) # Получаем путь к текущему файлу команды
|
|
root_directory = os.path.join(base_dir, "../../data") # Переходим на уровень выше и указываем папку data
|
|
root_directory = os.path.normpath(root_directory) # Нормализуем путь для кроссплатформенности
|
|
|
|
if not os.path.exists(root_directory):
|
|
logger.error(f"The directory {root_directory} does not exist.")
|
|
return
|
|
|
|
logger.info(f"Using root directory: {root_directory}")
|
|
|
|
# Поиск файлов .glb
|
|
try:
|
|
files = glob.glob("*.glb", recursive=True, root_dir=root_directory)
|
|
if not files:
|
|
logger.warning("No .glb files found in the specified directory.")
|
|
return
|
|
|
|
logger.info(f"Found {len(files)} .glb files: {', '.join(files)}")
|
|
except Exception as e:
|
|
logger.error(f"Error while searching for .glb files: {e}")
|
|
return
|
|
|
|
# Получение объекта Scene3D
|
|
try:
|
|
hv = Scene3D.objects.get(id=56)
|
|
logger.info(f"Retrieved Scene3D object with ID 56: {hv}")
|
|
except Scene3D.DoesNotExist:
|
|
logger.error("Scene3D object with ID 56 does not exist.")
|
|
return
|
|
except Exception as e:
|
|
logger.error(f"Error while retrieving Scene3D object: {e}")
|
|
return
|
|
|
|
# Обработка каждого файла
|
|
for f in files:
|
|
file_path = os.path.join(root_directory, f)
|
|
logger.info(f"Processing file: {file_path}")
|
|
|
|
try:
|
|
with open(file_path, 'rb') as file:
|
|
el = Element3D(name=f)
|
|
el.model_file = File(file, f)
|
|
el.save()
|
|
logger.info(f"Successfully saved Element3D object: {el}")
|
|
|
|
hv.elements.add(el)
|
|
logger.info(f"Added Element3D {el} to Scene3D {hv}. Total elements count: {hv.elements.count()}")
|
|
except FileNotFoundError:
|
|
logger.error(f"File not found: {file_path}")
|
|
except Exception as e:
|
|
logger.error(f"Error while processing file {f}: {e}")
|
|
|
|
logger.info("Import process completed successfully.") |