Forum

Author Topic: Set distances of scalebars via Python API  (Read 2108 times)

justinT

  • Newbie
  • *
  • Posts: 2
    • View Profile
Set distances of scalebars via Python API
« on: May 26, 2022, 03:59:25 AM »
Hi all,

I am trying to update the distances of scalebars via the Python API of Metashape. However, it turned out that my code does not work. Please kindly find my code as follows:
Code: [Select]
    chunk.addScalebar(chunk.markers[0], chunk.markers[1])
    chunk.addScalebar(chunk.markers[0], chunk.markers[2])
    chunk.addScalebar(chunk.markers[0], chunk.markers[3])
    chunk.addScalebar(chunk.markers[1], chunk.markers[2])
    chunk.addScalebar(chunk.markers[1], chunk.markers[3])
    chunk.addScalebar(chunk.markers[2], chunk.markers[3])

    chunk.sortScalebars()
    #Input distance of scalebars.
    chunk.scalebars[0].Reference.distance = 0.03
    chunk.scalebars[1].Reference.distance = 0.04
    chunk.scalebars[2].Reference.distance = 0.05
    chunk.scalebars[3].Reference.distance = 0.05
    chunk.scalebars[4].Reference.distance = 0.04
    chunk.scalebars[5].Reference.distance = 0.03

This code does not give me any error or warning. However, the corresponding distances will not change accordingly.

However, I found it is possible for me to rename the scalebars via the code:

Code: [Select]
chunk.scalebars[0].label = "example_scalebar"

Thus, I was wondering if I might be looking at the wrong functions/objects to change the distance.

Thank you so much for your attention.
« Last Edit: May 26, 2022, 04:02:02 AM by justinT »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15074
    • View Profile
Re: Set distances of scalebars via Python API
« Reply #1 on: May 30, 2022, 03:54:23 PM »
Hello Justin,

You should use the following (error in your code is related to the capital letter in Reference):

Code: [Select]
chunk.scalebars[0].reference.distance = 0.03
 chunk.scalebars[0].reference.enabled = True
 #...
 chunk.updateTransform()
Best regards,
Alexey Pasumansky,
Agisoft LLC

justinT

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Set distances of scalebars via Python API
« Reply #2 on: June 02, 2022, 08:25:38 PM »
Thank you so much Alexey! It works fine now!