Forum

Author Topic: NameError: name 'Metashape' is not defined (Python)  (Read 495 times)

UsmanJam

  • Newbie
  • *
  • Posts: 6
    • View Profile
NameError: name 'Metashape' is not defined (Python)
« on: June 09, 2024, 10:34:25 AM »
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()
 

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14965
    • View Profile
Re: NameError: name 'Metashape' is not defined (Python)
« Reply #1 on: June 09, 2024, 03:19:37 PM »
Hello UsmanJam,

Are you able to run the script using the following call?
Code: [Select]
metashape.exe -r test1.py
Best regards,
Alexey Pasumansky,
Agisoft LLC

UsmanJam

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: NameError: name 'Metashape' is not defined (Python)
« Reply #2 on: June 09, 2024, 03:32:04 PM »
Hi Alexey, yes i can.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14965
    • View Profile
Re: NameError: name 'Metashape' is not defined (Python)
« Reply #3 on: June 10, 2024, 01:18:59 PM »
Hello UsmanJam,

Then if you need to run the script in headless mode though the system Python, you should install Metashape module to the system Python following the related instruction:
https://agisoft.freshdesk.com/support/solutions/articles/31000148930-how-to-install-metashape-stand-alone-python-module
Best regards,
Alexey Pasumansky,
Agisoft LLC