Hi,
I would like to request a new parameter for the Python API, such as document.save(silent=True) or an equivalent option.
Currently, calling doc.save() from a script triggers the same progress popup as a manual save, which interrupts the workflow when using autosave scripts. A silent save mode (no popup, no UI interruption) would be extremely useful for background autosave routines.
Because if the user has a polyline and polygon when opening the popup, sometimes it closes and what was clicked with the active command is lost when the popup appears, or a native command that activates an autosave as soon as the user finishes creating a shape. Below is the autosave code we use.
# -*- coding: utf-8 -*-
import Metashape
try:
from PySide2 import QtCore
except ImportError as e:
Metashape.app.messageBox(
"Error importing PySide2 (QtCore).\n"
"Autosave requires Qt Timer.\n\n"
"Detail: %s" % str(e)
)
raise
autosave_timer = None
autosave_interval_min = 5
autosave_enabled = False
def autosave_log(msg):
print("[AUTOSAVE] " + msg)
def autosave_tick():
doc = Metashape.app.document
if doc is None:
autosave_log("No document open, skipping autosave.")
return
if not doc.path:
autosave_log("Project has not been saved yet (no path).")
return
try:
doc.save()
autosave_log("Project saved: %s" % doc.path)
except Exception as e:
autosave_log("Error saving project: %s" % str(e))
def autosave_enable():
global autosave_timer, autosave_enabled, autosave_interval_min
if autosave_enabled:
Metashape.app.messageBox("Autosave is already enabled.")
return
if autosave_timer is None:
autosave_timer = QtCore.QTimer()
autosave_timer.timeout.connect(autosave_tick)
autosave_timer.start(int(autosave_interval_min * 60 * 1000))
autosave_enabled = True
autosave_log("Autosave ENABLED – interval: %d min" % autosave_interval_min)
Metashape.app.messageBox(
"Autosave enabled every %d minute(s)." % autosave_interval_min
)
def autosave_disable():
global autosave_timer, autosave_enabled
if not autosave_enabled:
Metashape.app.messageBox("Autosave is already disabled.")
return
if autosave_timer is not None:
autosave_timer.stop()
autosave_enabled = False
autosave_log("Autosave DISABLED.")
Metashape.app.messageBox("Autosave disabled.")
def autosave_set_interval():
global autosave_interval_min, autosave_enabled, autosave_timer
new_value = Metashape.app.getInt(
"Autosave interval (minutes):",
autosave_interval_min
)
if new_value <= 0:
Metashape.app.messageBox("Invalid value. Insert minutes > 0.")
return
autosave_interval_min = new_value
autosave_log("New interval: %d min" % autosave_interval_min)
if autosave_enabled and autosave_timer is not None:
autosave_timer.stop()
autosave_timer.start(int(autosave_interval_min * 60 * 1000))
autosave_log("Timer restarted with new interval.")
Metashape.app.messageBox(
"Interval updated to %d minute(s)." % autosave_interval_min
)
else:
Metashape.app.messageBox(
"Interval set to %d minute(s), but autosave is OFF.\n\nUse:\nAutoSave → Enable"
% autosave_interval_min
)
label_enable = "AutoSave/Enable"
label_disable = "AutoSave/Disable"
label_interval = "AutoSave/Interval (min)"
Metashape.app.addMenuItem(label_enable, autosave_enable)
Metashape.app.addMenuItem(label_disable, autosave_disable)
Metashape.app.addMenuItem(label_interval, autosave_set_interval)
print("AutoSave loaded. Use:")
print(" %s" % label_enable)
print(" %s" % label_disable)
print(" %s" % label_interval)
autosave_log("Autosave script loaded.")