Hello Vaidutis,
Please check the following script. The script requires the polygonal shapes for patching to be grouped to "Patches" shape layer (other layers will be ignored). Each shape should have an attribute with "Patch" name and this attribute value should be set in accordance to the camera label to be used for patching.
import Metashape
def auto_patch():
doc = Metashape.app.document
chunk = doc.chunk #active chunk
if not chunk:
print("No active chunk, script aborted")
return False
if not chunk.orthomosaic:
print("No orthomosaic for the active chunk, script aborted")
return False
if not chunk.shapes:
chunk.shapes = Metashape.Shapes()
print ("No shapes, script aborted")
return False
ortho = chunk.orthomosaic
patches_list = [shape for shape in chunk.shapes if (shape.type == Metashape.Shape.Type.Polygon) and (shape.group.label.upper() == "PATCHES")] #list of polygonal shapes for patching
patches = dict()
for shape in patches_list:
if "Patch" not in shape.attributes.keys():
continue #no attribute
for camera in chunk.cameras:
if not camera.transform:
continue
if camera.label.upper() == shape.attributes["Patch"].upper():
patches[shape] = camera
patch = Metashape.Orthomosaic.Patch()
patch.image_keys = [patches[shape].key]
ortho.patches[shape] = patch
print("Assigned patch to shape " + shape.label + " for camera " + camera.label)
continue
ortho.update() #update orthomosaic patches
print("Script finished.")
Metashape.app.addMenuItem("Custom menu/Automatic orthomosaic patching", auto_patch)
I suggest to check, if the script works as expected. If any modifications are required, please let me know.