Agisoft Metashape Scripts

repository·master·Indexed 19 days ago

https://github.com/agisoft-llc/metashape-scripts

A collection of Python scripts for Agisoft Metashape Pro to automate and extend photogrammetry workflows. Includes tools for automatic sky masking using ONNX, aligning models or dense point clouds via ICP, filtering point clouds by confidence level, filtering tie points and cameras by selection, adding reference altitude, and removing small mesh components. Compatible with Metashape Pro version 2.3 and older versions via specific branches.

Tokens
6.4K
Snippets
15
Records
28
Agent score
67%

What's inside metashape-scripts

  1. Check Metashape and PhotoScan version compatibility

    master

    Scripts in this repository include version checks to ensure they are used with a compatible version of Metashape.

    • Latest Metashape versions: Use the main branch of this repository.
    • Older Metashape or PhotoScan versions: Switch to the specific branch corresponding to your software version to find compatible scripts.
  2. Run Python scripts automatically on Metashape Pro launch

    master
    These scripts are designed for use within Metashape Pro. You can configure Metashape to execute Python scripts automatically whenever the application starts. For detailed instructions on how to set up this automatic execution, refer to the official Agisoft support documentation.
  3. Filter tie points and cameras by selection

    master

    This script allows you to define a Region of Interest (ROI) by selecting specific tie points in the 3D view. Once run, the tool performs the following actions:

    1. Updates the Bounding Region: Dynamically recalculates and updates the chunk's bounding region to tightly fit the selected tie points.
    2. Filters Tie Points:
      • Invalidates tie points located outside the newly defined region.
      • If Preserve Unselected Tie Points Within Region is unchecked, it removes all unselected tie points globally.
      • If Preserve Unselected Tie Points Within Region is checked, it only invalidates points outside the region, keeping unselected points that fall within the bounds.
    3. Manages Cameras:
      • Disables cameras that do not have any valid tie point projections within the new region.
      • If Remove Disabled Cameras is checked, it physically removes the disabled cameras from the chunk.

    Compatibility Note: This script is designed for Metashape version 2.3.

  4. Use the Tie Points Region Filter Tool

    master

    To use this tool within Metashape:

    1. Open your project and navigate to the 3D View.
    2. Select the tie points you wish to use as the basis for your region of interest.
    3. Run the script from the Metashape menu: Scripts > Filter tie points and cameras by points selection.
    4. In the dialog that appears:
      • Check Preserve Unselected Tie Points Within Region if you want to keep unselected points that are inside the bounds.
      • Check Remove Disabled Cameras if you want to delete cameras that no longer contribute to the ROI.
    5. Click Filter Tie Points.
  5. Install the Remove Unpinned Projections script into the Metashape menu

    master

    To use this script via the Metashape GUI, follow these steps:

    1. Run the script within Metashape.
    2. The script will automatically add a custom menu item labeled Scripts/Remove Unpinned Projections to the Metashape application menu.
    3. To execute the logic later, navigate to: Workflow -> Batch Process -> Add -> Run script to add to menu (or simply use the newly created menu item).
    4. You can then trigger the removal by selecting Scripts/Remove Unpinned Projections from the menu.
  6. Create a footprint shape layer in Metashape

    master

    The footprints_to_shapes.py script automates the creation of a footprint shape layer in the active Metashape chunk. For every aligned regular camera, it calculates a four-vertex polygon (footprint) based on the sensor corners and projects them onto the available surface (Elevation, Model, Point Cloud, or Tie Points).

    Key Features:

    • Automatic Layer Creation: Creates a new shape group labeled Footprints with a specific color (30, 239, 30).
    • Surface Detection: Automatically selects the best available surface for projection: chunk.elevation > chunk.model > chunk.point_cloud > chunk.tie_points.
    • Calibration Validation: Checks sensor calibration stability at the corners. If calibration is unstable, it crops the footprint to a stable region.
    • Metadata: Each generated shape is labeled with the camera's label and includes a Photo attribute containing the camera label.

    Compatibility: This script requires Metashape Pro version 2.3.

    # To use this script, run it within Metashape Pro.
    # It adds a menu item to the Metashape interface:
    # Scripts/Create footprint shape layer
  7. Split project into chunks using the Split in chunks dialog

    master

    The split_in_chunks script provides a GUI dialog to divide a Metashape project into a grid of smaller chunks. This is useful for processing large datasets by breaking them into manageable spatial segments.

    Key Features:

    • Grid Configuration: Define the number of divisions in X and Y directions.
    • Overlap Control: Set an overlap percentage to ensure continuity between chunks.
    • Automated Processing:
      • Build Point Cloud: Automatically generates point clouds for each chunk.
      • Build Mesh: Automatically generates meshes for each chunk.
    • Mesh Options: Choose between Mesh From Depth Maps or other surface types, and select face count density (High, Medium, Low, or Custom).
    • Point Cloud Quality: Select density levels (Ultra, High, Medium, Low, Lowest).
    • Merge Back: Optionally merges the processed products from all individual chunks back into a single unified chunk.
    • Autosave: Automatically saves the project after each major operation.
    # To run the script, execute it within the Metashape Python environment.
    # Once executed, it adds a menu item to the Metashape interface:
    # 'Scripts/Split in chunks'
  8. Filter point clouds by confidence level

    master

    This script allows you to filter a Metashape point cloud by removing points that fall below a specified confidence threshold. It works by setting a confidence filter to select low-confidence points, removing those selected points, and then resetting the filter to restore the remaining high-confidence points.

    Usage in Metashape Pro

    1. Open Metashape Pro.
    2. Go to Tools -> Run Script.
    3. Select this script.
    4. In the Arguments field, enter an integer representing the maximum confidence level you want to remove (e.g., 3).
      • If you enter 3, the script will remove all points with a confidence level in the range [0, 3].
      • If you leave the Arguments field blank, the script uses a default maximum confidence value of 3.
    Arguments: 3
  9. Use the Remove Low Quality Photos script

    master

    This script automates the process of estimating image quality within a Metashape chunk and removing images that fall below a specific quality threshold.

    Workflow:

    1. Open Metashape Pro.
    2. Navigate to Workflow -> Batch Process -> Add.
    3. Run the script to add it to the menu.
    4. Select the new menu item: Scripts/Remove low quality photos.

    Behavior:

    • It calls chunk.analyzeImages(chunk.cameras) to estimate quality.
    • It checks the Image/Quality metadata field for each camera.
    • Images with a quality score less than 0.7 are removed from the chunk.
    • It automatically adds a custom menu item to Metashape labeled Scripts/Remove low quality photos for easy subsequent access.
  10. Remove disabled photos from a Metashape project

    master

    This script automates the cleanup of disabled photos in a Metashape project. It identifies all cameras marked as 'Disabled', moves their corresponding image files into a new subdirectory named Disabled within the original photo directory, and then removes the disabled camera entries from the Metashape project.

    Key behaviors:

    • Directory Creation: It creates a Disabled/ folder in the same directory as the photos if it doesn't already exist.
    • Safety: It skips cameras that are not of type Metashape.Camera.Type.Regular (e.g., camera tracks) and skips cameras that are currently enabled.
    • Error Handling: If it cannot create the destination directory or encounters an OSError during the move, it skips that specific photo to prevent data loss.
    • Integration: The script adds a menu item to the Metashape interface under Scripts/Remove disabled photos.

    Compatibility: This specific version of the script is designed for Metashape version 2.3.

    # To use this script, run it within the Metashape Python environment.
    # It will add a menu item to the Metashape UI:
    # Scripts -> Remove disabled photos
  11. Use the Render image GUI

    master

    The script provides a graphical user interface (GUI) via render_image_gui() to configure and execute renders. When run, it adds a menu item to Metashape Pro under Scripts/Render image.

    GUI Options

    • Source data: Select between Model, Tiled model, Point cloud, or Tie points.
    • Sensor type: Select between Frame, Spherical, or Fisheye.
    • Width/Height: Set the output resolution using spin boxes.

    Upon clicking Ok, the script prompts for a save location (supporting .tif, .jpg, and .png) and renders the view from the current viewpoint.

    # To trigger the GUI from within Metashape's Python console:
    render_image_gui()
  12. Remove small mesh components using gradual selection

    master

    This script automates the removal of small mesh components (debris) from a Metashape model. It iteratively reduces the number of components in a chunk until only one component remains. It works by calculating the average number of faces per component and using that value as a threshold to remove all components smaller than the average, repeating this process until the model is consolidated.

    Compatibility:

    • Requires Metashape Pro version 2.3.

    How to run:

    1. Open Metashape Pro.
    2. Navigate to Workflow -> Batch Process.
    3. Click Add and select Run script.
    4. Load this script and execute.
    # Usage in Metashape Batch Process:
    # Workflow -> Batch Process -> Add -> Run script