Forum

Author Topic: add folder like the gui  (Read 1174 times)

fran.garcia

  • Newbie
  • *
  • Posts: 7
    • View Profile
add folder like the gui
« on: January 26, 2023, 10:29:51 PM »
hi, i tried this code to add a folder with 11 subfolders, i need to mantain the same structure but it doesn´t work well , the script fills the folder in a random way. Any help??
Code: [Select]
import Metashape
import os
doc = Metashape.app.document
chunk = doc.chunk
def find_files(folder, types):
    files = []
    for root, dirs, filenames in os.walk(folder):
        for filename in filenames:
            if os.path.splitext(filename)[1].lower() in types:
                files.append(os.path.join(root, filename))
    return files

def import_subfolders(folder):
    # Crear un nuevo proyecto en Metashape
    doc = Metashape.app.document
    for root, dirs, files in os.walk(folder):
        for dir in dirs:
            # Crear un nuevo grupo para cada subdirectorio
            group=chunk.addCameraGroup()
            group.label = dir
           
            # Encontrar todas las fotos en el subdirectorio
            photos = find_files(os.path.join(root, dir), [".jpg", ".jpeg", ".tif", ".tiff"])
            # Añadir las fotos al chunk
            chunk.addPhotos(photos,group=group.label)
    # Guardar el proyecto

# Usa la funcion anterior para importar todos los subdirectorios en una carpeta dada
image_folder = Metashape.app.getExistingDirectory("Selecciona la carpeta de imagenes:")
import_subfolders(image_folder)