Hello Garfields_Lasagna,
Please check, if the following script works as expected for your needs:
#input file format:
#camera name, x-Position in Pixel, Y-position in Pixel
#
#output file format:
#marker label, x-world, y-world, z-world
import Metashape, os
def read_2D_export_3D(chunk, path):
if not path:
print("Invalid path, script aborted")
return 0
if not os.path.isfile(path):
print("Invalid path, script aborted")
return 0
if not chunk:
print("Empty document, script aborted")
return 0
cameras = [camera for camera in chunk.cameras if camera.transform and camera.type == Metashape.Camera.Type.Regular] #list of aligned cameras
if not cameras:
print("Empty chunk, script aborted")
return 0
if not chunk.dense_cloud:
print("Dense cloud is missing, script aborted")
return 0
surface = chunk.dense_cloud
crs = chunk.crs
T = chunk.transform.matrix
input = open(path, "rt")
if os.path.splitext(path)[1]:
out_path = os.path.splitext(path)[0] + "_out.txt"
else:
out_path = path + "_out.txt"
output = open(out_path, "wt")
lines = input.readlines()
for line in lines:
if len(line) < 4:
continue
label, x_coord, y_coord = line.strip().split(",",3)
x_coord = float(x_coord)
y_coord = float(y_coord)
for camera in cameras:
if camera.label == label:
marker = chunk.addMarker()
ray_origin = camera.unproject(Metashape.Vector([x_coord, y_coord, 0]))
ray_target = camera.unproject(Metashape.Vector([x_coord, y_coord, 1]))
coord = crs.project(T.mulp(surface.pickPoint(ray_origin, ray_target)))
marker.label = label
marker.projections[camera] = Metashape.Marker.Projection(Metashape.Vector([x_coord,y_coord]), True)
marker.reference.location = coord
marker.reference.enabled = True
output.write("{:s},{:.6f},{:.6f},{:.6f}\n".format(marker.label, coord.x, coord.y, coord.z))
break
output.flush()
output.close()
input.close()
print("Script finished")
return 1
chunk = Metashape.app.document.chunk
path = Metashape.app.getOpenFileName("Select the file with 2D marker coordinates:", filter="(*.txt) Text files;; (*.*) All files")
read_2D_export_3D(chunk, path)
The input text file is expected to be in the following format:
DSC01578.JPG,100.12,200.223
DSC02579.JPG,234.13,300.245
DSC02580.JPG,300.14,456.289
DSC03581.JPG,500.15,600.267
The output file is created in the same directory where the input file is located with "_out" suffix.