Forum

Author Topic: Creating an animation track from a polyline  (Read 2677 times)

Michel

  • Newbie
  • *
  • Posts: 14
    • View Profile
Creating an animation track from a polyline
« on: June 02, 2022, 02:12:17 PM »
Hello everyone,

I would like to know if it is possible to create a camera track from a polyline via a Python script ?

While searching, I found the following topic (https://www.agisoft.com/forum/index.php?topic=11313.0) which gives a script allowing to create a path passing by all the cameras of a project. I imagine the basis of the script would be similar, but I'm having trouble getting from a line, to points and then keyframes.

The aim of the script would be to create n keyframes per line (by distance or % criteria) with an orientation either by default or directed to the next point. Adjustments will be made manually before rendering.

Thanks in advance for your help,

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15086
    • View Profile
Re: Creating an animation track from a polyline
« Reply #1 on: June 02, 2022, 04:07:31 PM »
Hello Michel,

Please see the sample script that creates the animation camera track, based on selected shapes (polylines and polygons).


There are some limitations, which could be fixed, if necessary:
- script only works with polygonal and polyline shapes, which are defined in 3D and do not have attached markers,
- script assumes that the project is georeferenced (let me know, if you are planning to apply the script to unreferenced projects or projects in local coordinates),
- each polyline/polygon segment would contain N camera track keyframes with equal spacing (N is hardcoded in the script body).



Code: [Select]
import Metashape

def flatten_list(input_list):
if not input_list:
return []
output_list = []
for i in input_list:
if not isinstance(i, list):
output_list.append(i)
else:
output_list += flatten_list(i)
return output_list

def shape_to_camera_track(chunk, shape):

N = 10 #keyframes per polyline segment

if shape.geometry.type not in [Metashape.Geometry.Type.LineStringType, Metashape.Geometry.Type.PolygonType]:
print("Only polygonal and polyline shapes are supported, skipping shape " + shape.label)
return 0
if not shape.geometry.is_3d:
print("Only 3D shapes are supported, skipping shape " + shape.label)
if shape.is_attached:
print("Shapes with attached markers not yet supported, skipping shape " + shape.label)
#TODO, if necessary
return 0

animation = chunk.addCameraTrack()
chunk.camera_track = animation
animation.label = "({:d}) {:s}".format(shape.key, shape.label)
frames = []

vertices = flatten_list(shape.geometry.coordinates)
T = chunk.transform.matrix
shapes_crs = chunk.shapes.crs
if not shapes_crs:
shapes_crs = chunk.crs

for i in range(0, len(vertices) - 1):
start = Metashape.CoordinateSystem.transform(vertices[i], shapes_crs, chunk.crs.geoccs)
finish = Metashape.CoordinateSystem.transform(vertices[i+1], shapes_crs, chunk.crs.geoccs)

for j in range(N):
coord = start + j * (finish - start) / N
coord = T.inv().mulp(coord)
rot = Metashape.Matrix().Diag([1,1,1])

frame = chunk.addCamera()
frame.type = Metashape.Camera.Type.Keyframe
frame.transform = Metashape.Matrix.Translation(coord) * Metashape.Matrix.Rotation(rot)
frames.append(frame)
animation.keyframes = frames

print("Created camera track from shape " + shape.label)
return 1


doc = Metashape.app.document
chunk = doc.chunk
shapes = [s for s in chunk.shapes if s.geometry.type in [Metashape.Geometry.Type.LineStringType, Metashape.Geometry.Type.PolygonType]]

if len(shapes):
for shape in [s for s in shapes if s.selected]:
shape_to_camera_track(chunk, shape)
else:
print("No polygonal or polyline shapes, script aborted")
Best regards,
Alexey Pasumansky,
Agisoft LLC

Michel

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Creating an animation track from a polyline
« Reply #2 on: June 02, 2022, 06:12:19 PM »
Hello Alexey,

Thank you for sharing this python script which answers my problem perfectly. I should never exceed the limitations, because I use a 3D polyline and my project is georeferenced. Nevertheless, I will come back to you if my problem evolves.

I just have a question about the orientation of the cameras. Indeed, in the track obtained, the cameras are oriented in a way that the 3D model is seen from below, whereas they are directed towards a point above (see image). Could this be due to a problem in the transformation? Is it possible to define the desired orientation within the script? For information, my model and polyline are georeferenced in the same coordinate system as the project.

--
Michel

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15086
    • View Profile
Re: Creating an animation track from a polyline
« Reply #3 on: June 02, 2022, 06:52:49 PM »
Hello Michel,

What should be a desired camera orientation? The options that should be quite easy for implementation are center of the chunk region or, for example, centroid of the related shape.

If the orientation angles should be the same for all the key frames, I could add the possibility of setting them as yaw, pitch, roll angles.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Michel

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Creating an animation track from a polyline
« Reply #4 on: June 02, 2022, 07:16:40 PM »
Hello Alexey,

I would like each camera to fix a particular point in space. For example, I create a point layer with one viewpoint per camera in the track and the script automatically orients the viewpoint.

If this is not possible, I would be interested in the solution where all or some of the cameras fix the centre of a polygon so that I can more easily orientate them manually afterwards.

I would also be interested in the second solution that you propose with configurable yaw, pitch and roll angles

Thanking you in advance,
--
Michel