Forum

Author Topic: Replicate Update Transform using Python API  (Read 314 times)

sepehr500

  • Newbie
  • *
  • Posts: 13
    • View Profile
Replicate Update Transform using Python API
« on: May 20, 2022, 05:37:25 PM »
Hello. I am running a script where I detect markers, add scale bars, then try to scale the model to those scale bars. Unfortunately,  when I call chunk.updateTransform() after these scalebars are added programatically, nothing happens. But if I open the software, my scalebars and markers look great. All I need to do is click the update transform button in the GUI and the model scales. Is replicating this behavior in the API more complex than simply calling chunk.updateTransform()?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 13729
    • View Profile
Re: Replicate Update Transform using Python API
« Reply #1 on: May 23, 2022, 04:43:24 PM »
Hello sepehr500,

When you open the project created by the script in GUI, are the markers and scale bars checked on in the Reference pane?
Best regards,
Alexey Pasumansky,
Agisoft LLC

sepehr500

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Replicate Update Transform using Python API
« Reply #2 on: May 23, 2022, 07:14:37 PM »
What do you mean by checked?
« Last Edit: May 23, 2022, 07:33:52 PM by sepehr500 »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 13729
    • View Profile
Re: Replicate Update Transform using Python API
« Reply #3 on: May 24, 2022, 04:35:41 PM »
Hello sepehr500,

I mean the checkboxes next to cameras, markers, scale bars labels in the Reference pane.
Best regards,
Alexey Pasumansky,
Agisoft LLC

sepehr500

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Replicate Update Transform using Python API
« Reply #4 on: May 24, 2022, 07:41:14 PM »
Thanks Alexy. I figured out that i'm actually having a different scaling issue. updateTransform works well after adding scalebars programmatically, aftwords, when I go to align the 3d model to the location of the first camera, the scale gets reset.

Code: [Select]
    if not chunk:
        print("Empty project, script aborted")
        return

    T = chunk.transform.matrix
    origin = (-1) * camera.center
    R = Metashape.Matrix().Rotation(camera.transform.rotation()
                                    * Metashape.Matrix().Diag([1, -1, 1]))
    origin = R.inv().mulp(origin)
    chunk.transform.matrix = Metashape.Matrix([[1, 0, 0, 0], [0, 0, 1, 0], [
                                              0, 1, 0, 0], [0, 0, 0, 1]]) * Metashape.Matrix().Translation(origin) * R.inv()

If I go and scale model alignment after running the above script, then 0,0,0 is no longer then location of the first camera. Do you have any solutions for this?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 13729
    • View Profile
Re: Replicate Update Transform using Python API
« Reply #5 on: May 25, 2022, 02:31:14 PM »
Hello sepehr500,

Can you please check if the following modification of your script solves the required task:

Code: [Select]
chunk = Metashape.app.document.chunk

#create scalebars here

chunk.updateTransform()
camera = chunk.cameras[-1]
s = chunk.transform.scale

origin = (-1) * camera.center
R = Metashape.Matrix().Rotation(camera.transform.rotation()
* Metashape.Matrix().Diag([1, -1, 1]))
origin = R.inv().mulp(origin)
chunk.transform.matrix = Metashape.Matrix([[s, 0, 0, 0], [0, 0, s, 0], [
  0, s, 0, 0], [0, 0, 0, 1]]) * Metashape.Matrix().Translation(origin) * R.inv()

It applies the scale from the scale bards first, then re-calculates coordinate system origin and orientation (based on the first camera), but preserves the applied scale.
Best regards,
Alexey Pasumansky,
Agisoft LLC

sepehr500

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Replicate Update Transform using Python API
« Reply #6 on: May 27, 2022, 07:07:40 PM »
Works perfectly! Thank you.