Hello geo_enth,
Please see an example of script below (via GUI) that allows to generate the orthomosaic in local coordinates using Planar mode with predefined view options.
3x3 projection matrix defines the projection plane.
import Metashape
from PySide2 import QtCore, QtGui, QtWidgets
class BuilOrthoLocal(QtWidgets.QDialog):
def __init__ (self, parent):
QtWidgets.QDialog.__init__(self, parent)
self.setWindowTitle("Build Ortho&DEM in Local Coordinates")
self.txtView = QtWidgets.QLabel()
self.txtView.setText("Projection plane:")
self.boxView = QtWidgets.QComboBox()
for element in ["FrontXZ", "BackXZ", "LeftYZ", "RightYZ", "TopXY", "BottomXY", "CurrentView"]:
self.boxView.addItem(element)
self.btnQuit = QtWidgets.QPushButton("Close")
#self.btnQuit.setFixedSize(90, 50)
self.btnP1 = QtWidgets.QPushButton("Build Ortho&&DEM")
#self.btnP1.setFixedSize(90, 50)
layout = QtWidgets.QGridLayout()
layout.addWidget(self.txtView, 0, 0, QtCore.Qt.AlignRight)
layout.addWidget(self.boxView, 0, 1, QtCore.Qt.AlignLeft)
layout.addWidget(self.btnP1, 1, 0)
layout.addWidget(self.btnQuit, 1, 1)
self.setLayout(layout)
proc_build_ortho = lambda: self.build_ortho()
QtCore.QObject.connect(self.btnP1, QtCore.SIGNAL("clicked()"), proc_build_ortho)
QtCore.QObject.connect(self.btnQuit, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("reject()"))
self.exec()
def build_ortho(self):
doc = Metashape.app.document
chunk = doc.chunk
if not chunk:
print("Empty project. Script aborted")
return 0
if not chunk.model:
print("No mesh model, can't build orthomosaic. Script aborted.")
return 0
if not len([camera for camera in chunk.cameras if (camera.transform and camera.type == Metashape.Camera.Type.Regular)]):
print("No aligned cameras, can't build orthomosaic. Script aborted.")
return 0
print("Script started...")
PROJECTIONS = {"TopXY": Metashape.Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
"BottomXY": Metashape.Matrix([[1, 0, 0], [0, -1, 0], [0, 0, -1]]),
"FrontXZ": Metashape.Matrix([[1, 0, 0], [0, 0, 1], [0, -1, 0]]),
"BackXZ": Metashape.Matrix([[-1, 0, 0], [0, 0, 1], [0, 1, 0]]),
"LeftYZ": Metashape.Matrix([[0, -1, 0], [0, 0, 1], [-1, 0, 0]]),
"RightYZ": Metashape.Matrix([[0, 1, 0], [0, 0, 1], [1, 0, 0]]),
"CurrentView": Metashape.app.model_view.viewpoint.rot.t()
}
Rot = Metashape.Matrix.Rotation(PROJECTIONS[self.boxView.currentText()])
T = chunk.transform.matrix
lf = chunk.crs.localframe(T.mulp(Metashape.Vector([-1000000, -1000000, 0])))
proj = Metashape.OrthoProjection()
proj.crs = chunk.crs
proj.type = Metashape.OrthoProjection.Type.Planar
proj.matrix = Rot * Metashape.Matrix.Rotation(lf.rotation())
chunk.orthomosaic = None
chunk.buildOrthomosaic(surface_data = Metashape.DataSource.ModelData, blending_mode = Metashape.BlendingMode.MosaicBlending, cull_faces = True, projection = proj)
chunk.elevation = None
chunk.buildDem(source_data = Metashape.DataSource.ModelData, interpolation = Metashape.EnabledInterpolation, projection = proj)
chunk.orthomosaic.label = "Orthomosaic " + self.boxView.currentText()
chunk.elevation.label = "DEM " + self.boxView.currentText()
print("Script finished")
return 1
def build_ortho_local():
global app
app = QtWidgets.QApplication.instance()
parent = app.activeWindow()
dlg = BuilOrthoLocal(parent)
Metashape.app.addMenuItem("Custom menu/Build Ortho&&DEM in Local Coordinates", build_ortho_local)