1
Python and Java API / Marquee select and remove
« on: June 06, 2023, 08:16:37 AM »
Hi i was wondering if there was a script to define a marquee selection across multiple chunks to remove?
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
import Metashape, math
doc = Metashape.app.document
chunk = doc.chunk
region = chunk.region
T = chunk.transform.matrix
m = Metashape.Vector([1E+1, 1E+1, 1E+1])
M = -m
for point in chunk.point_cloud.points:
if not point.valid:
continue
coord = T * point.coord
for i in range(3):
m[i] = min(m[i], coord[i])
M[i] = max(M[i], coord[i])
center = (M + m) / 2
size = M - m
region.center = T.inv().mulp(center)
region.size = size * (2 / T.scale())
region.rot = T.rotation().t()
chunk.region = region
print("Script finished.")
import Metashape
chunk = Metashape.app.document.chunk
index = 0
for chunk in Metashape.app.document.chunks:
for camera in chunk.cameras:
cameraCount = (len(chunk.cameras))
camera.label = "_".join([format(index)])
index += 1
if index == cameraCount:
index = 0
print("Script finished")
Hey chitty1989,
you can always insert a loop that loops inside all chunks in document:Code: [Select]for chunk in Metashape.app.document.chunks:
for camera in chunk.cameras:
.....
be careful the camera.photo.path should not be changed as this is the path to actual image file name on disk irrespective of change in camera.label
import PhotoScan
chunk = PhotoScan.app.document.chunk
string = PhotoScan.app.getString("Input prefix to be added to the selected camera labels:")
index = PhotoScan.app.getInt("Input starting index:")
for chunk in Metashape.app.document.chunks:
for camera in chunk.cameras:
if camera.label:
if camera.label[-3] == ".":
camera.label = "_".join([string,"{:04d}".format(index)]) + camera.label[-4:]
else:
camera.label = "_".join([string,"{:04d}".format(index)])
camera.photo.path = camera.photo.path.rsplit("/",1)[0] + "/" + camera.label
index += 1
print("Script finished")
Hi chitty1989,
for a given camera, its label can be stripped of its first 4 characters by:Code: [Select]camera.label = camera.label[4:]
and for renaming chunks to folder where images are stored, you can use following to extract folder name from a given camera.photo.path:Code: [Select]camera.photo.path
Out[5]: 2022-03-17 21:59:06 'C:/Users/paul.pelletier/Documents/Merida_190718/Vuelo2/img/IMG_6133.JPG'
os.path.dirname(camera.photo.path)
Out[6]: 2022-03-17 22:00:21 'C:/Users/paul.pelletier/Documents/Merida_190718/Vuelo2/img'
os.path.dirname(camera.photo.path).rsplit('/',1)
Out[7]: 2022-03-17 22:01:25 ['C:/Users/paul.pelletier/Documents/Merida_190718/Vuelo2', 'img']
os.path.dirname(camera.photo.path).rsplit('/',1)[-1]
Out[8]: 2022-03-17 22:02:05 'img'
for a base script to rename all chunks in a workspace look at https://github.com/agisoft-llc/metashape-scripts/blob/master/src/contrib/change_chunk_names.py
Hope this gets you started!
import PhotoScan
chunk = PhotoScan.app.document.chunk
string = PhotoScan.app.getString("Input prefix to be added to the selected camera labels:")
index = PhotoScan.app.getInt("Input starting index:")
for camera in chunk.cameras:
if camera.selected:
if camera.label[-4] == ".":
camera.label = "_".join([string,"{:04d}".format(index)]) + camera.label[-4:]
else:
camera.label = "_".join([string,"{:04d}".format(index)])
camera.photo.path = camera.photo.path.rsplit("/",1)[0] + "/" + camera.label
index += 1
print("Script finished")
However the slightly annoying thing is that I have to select all the photos within each chunk and run the script. and then repeat for the next chunk. Is there a way i could do this for all chunks? Maybe if i pre- specified the prefix and starting index in the script and apply it to all photos within each chunk?