Hi All, i have been trying to create a very basic script using python to create a project and add photos from a folder and align them. However i am getting this error "NameError: name 'Metashape' is not defined". i have set the PYTHONPATH (screenshot attached) in the system varialbles, but the error presists. I have tried running the script standalone and through the python module in the Metashape installation directory, but result is the same. Can any one here help me with this please. Code is given below
import sys
import os
# Add the path to the Metashape Python module
# Replace this path with the actual path where Metashape is installed
metashape_path = "C:/Program Files/Agisoft/Metashape Pro/python" # Modify this path as needed
sys.path.append(metashape_path)
import Metashape
import tkinter as tk
from tkinter import filedialog
# Function to select Metashape project file
def select_project_file():
project_path = filedialog.askopenfilename(filetypes=[("Metashape Projects", "*.psx")])
if project_path:
project_label.config(text=project_path)
global project_file_path
project_file_path = project_path
# Function to select folder containing images
def select_folder():
folder_path = filedialog.askdirectory()
if folder_path:
folder_label.config(text=folder_path)
add_images_button.config(state="normal")
global images_folder_path
images_folder_path = folder_path
# Function to add images to Metashape project
def add_images_to_metashape():
# Open Metashape project
doc = Metashape.Document()
doc.open(project_file_path)
# Get the first chunk or create a new one if none exists
if not doc.chunks:
chunk = doc.addChunk()
else:
chunk = doc.chunks[0]
# Add images to the chunk
image_list = sorted(os.listdir(images_folder_path))[:100]
image_paths = [os.path.join(images_folder_path, image) for image in image_list if image.lower().endswith(('jpg', 'jpeg', 'png', 'tif', 'tiff'))]
chunk.addPhotos(image_paths)
# Save the project
doc.save()
status_label.config(text="Images added successfully!")
# Create GUI
root = tk.Tk()
root.title("Add Images to Metashape Project")
frame = tk.Frame(root, padx=20, pady=20)
frame.pack(padx=10, pady=10)
project_label = tk.Label(frame, text="Select a Metashape project file")
project_label.pack(pady=5)
project_button = tk.Button(frame, text="Select Project File", command=select_project_file)
project_button.pack(pady=5)
folder_label = tk.Label(frame, text="Select a folder containing images")
folder_label.pack(pady=5)
folder_button = tk.Button(frame, text="Select Folder", command=select_folder)
folder_button.pack(pady=5)
add_images_button = tk.Button(frame, text="Add Images to Project", command=add_images_to_metashape, state="disabled")
add_images_button.pack(pady=20)
status_label = tk.Label(frame, text="")
status_label.pack(pady=5)
root.mainloop()