Hello James,
I have found the script, that allows to save information about patches to shape attributes and read them:
import Metashape
def get_camera_by_key(key, chunk):
for camera in chunk.cameras:
if camera.key == key:
return camera
return None #camera not found
def patch_to_attr():
doc = Metashape.app.document
chunk = doc.chunk #active chunk
if not chunk:
print("Empty project, script aborted")
return 0
if not len([camera for camera in chunk.cameras if camera.type == Metashape.Camera.Type.Regular]):
print("No cameras, script aborted")
return 0
if not chunk.orthomosaic:
print("No orthomosaic for the active chunk, script aborted")
return 0
if not chunk.shapes:
print ("No shapes, script aborted")
return 0
ortho = chunk.orthomosaic
shapes = chunk.shapes
patches = ortho.patches.keys()
if not len(patches):
print ("No patches, script aborted")
return 0
count = 0
for patch in patches:
assigned_image = get_camera_by_key(ortho.patches[patch].image_keys[0], chunk)
if assigned_image == None:
print("Skipping patch", patch)
continue
patch.attributes["PATCH_CAMERA_LABEL"] = assigned_image.label
if assigned_image.photo.path:
patch.attributes["PATCH_CAMERA_PATH"] = assigned_image.photo.path
count += 1
print(str(count) + " patches saved to shape attributes. Script finished.")
return 1
def attr_to_patch():
doc = Metashape.app.document
chunk = doc.chunk #active chunk
if not chunk:
print("Empty project, script aborted")
return 0
if not len([camera for camera in chunk.cameras if camera.type == Metashape.Camera.Type.Regular]):
print("No cameras, script aborted")
return 0
if not chunk.orthomosaic:
print("No orthomosaic for the active chunk, script aborted")
return 0
if not chunk.shapes:
print ("No shapes, script aborted")
return 0
ortho = chunk.orthomosaic
patches = [shape for shape in chunk.shapes if (shape.geometry.type == Metashape.Geometry.Type.PolygonType) and ("PATCH_CAMERA_LABEL" in shape.attributes.keys())] #list of patches
ortho_patches = dict()
count = 0
for shape in patches:
found_cameras = []
for camera in chunk.cameras:
if camera.label.upper() == shape.attributes["PATCH_CAMERA_LABEL"].upper():
found_cameras.append(camera)
if not len(found_cameras):
print("Skipping " + str(shape) + " patch, no related cameras found")
continue
elif len(found_cameras) > 1:
if "PATCH_CAMERA_PATH" not in shape.attributes.keys():
print("Using first found camera with corresponding label for " + str(shape) + " patch")
camera = found_cameras[0]
else:
for camera in found_cameras:
if camera.photo.path.upper() == shape.attributes["PATCH_CAMERA_PATH"].upper():
break
print("Using first found camera with corresponding label for " + str(shape) + " patch")
camera = found_cameras[0]
else:
camera = found_cameras[0]
###camera - image to be assigned
patch = Metashape.Orthomosaic.Patch()
patch.image_keys = [camera.key]
ortho.patches[shape] = patch
count += 1
print("Assigned patch " + str(shape) + " for camera " + camera.label)
print(str(count) + " patches loaded from shape attributes.")
return 1
Metashape.app.addMenuItem("Scripts/Save existing patches to shape attributes", patch_to_attr)
Metashape.app.addMenuItem("Scripts/Load patches from shape attributes", attr_to_patch)