Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: m on December 11, 2015, 05:49:31 PM

Title: Update view and bounding box for each chunk
Post by: m on December 11, 2015, 05:49:31 PM
Hi


I want to process many chunks in loop with a python script, but I have an issue with reset view and bounding box.

The predefined bounding box is not always big enough to cover the point cloud and I must manually rotate and resize bounding box before dense cloud processing is started. This is time consuming and batch processing is splitted in two parts.

I work with a coordinate system and have markers.
Bounding box must be aligned to the coordinate system.

I would also like to reset view for each chunk in a loop before exporting an pdf.

How can I work this out with Python scripting?


m
Title: Re: Update view and bounding box for each chunk
Post by: Alexey Pasumansky on December 11, 2015, 05:51:27 PM
Hello m,

What parameters do you wish to use for bounding box size (like center and width, height, depth or coordinates of the corners)? And is there any specific view point that you wish to switch to for the PDF export?
Title: Re: Update view and bounding box for each chunk
Post by: m on December 11, 2015, 05:55:30 PM
Hi


Impressive fast reply to this post.


I would like to use coordinates of the corners and view point from the top.


m
Title: Re: Update view and bounding box for each chunk
Post by: Alexey Pasumansky on December 23, 2015, 02:56:04 PM
Hello m,

Here's the code that automatically orients the viewpoint to display the bounding box fully in the Model view frame and exports the model to PDF format.

Code: [Select]
import PhotoScan
from PySide import QtGui

chunk = PhotoScan.app.document.chunk
T = chunk.transform.matrix
viewpoint = PhotoScan.app.viewpoint

main =  QtGui.qApp
for wdg in list(main.allWidgets()):
    if wdg.inherits('QGLWidget'):
        cy = wdg.height()
        cx = wdg.width()
        break

region = chunk.region
r_center = region.center
r_rotate = region.rot
r_size = region.size
r_vert = list()

for i in range(8):   #bounding box corners
r_vert.append(PhotoScan.Vector([0.5 * r_size[0] * ((i & 2) - 1), r_size[1] * ((i & 1) - 0.5), 0.25 * r_size[2] * ((i & 4) - 2)]))
r_vert[i] = r_center + r_rotate * r_vert[i]

height =  T.mulv(r_vert[1] - r_vert[0]).norm()
width  = T.mulv(r_vert[2] - r_vert[0]).norm()

if width / cx > height /cy:
scale = cx / width
else:
scale = cy / height

PhotoScan.app.viewpoint.coo = T.mulp(chunk.region.center)
PhotoScan.app.viewpoint.mag = scale
PhotoScan.app.viewpoint.rot = chunk.transform.rotation * r_rotate

crs = PhotoScan.CoordinateSystem("LOCAL")
chunk.exportModel(path, format="pdf", projection=crs)

As for the script that should use the coordinates of the bounding box corners, are you planning to specify all eight points or four laying in the plane?
Title: Re: Update view and bounding box for each chunk
Post by: m on December 23, 2015, 04:37:52 PM
Hi


The best solution will be to specify all eight corners as the height/depth will vary a lot from chunk to chunk.

Thank you for the code, I will test it after the holiday.


m
Title: Re: Update view and bounding box for each chunk
Post by: pablorov on March 04, 2017, 08:27:33 AM
Hello m,

Here's the code that automatically orients the viewpoint to display the bounding box fully in the Model view frame and exports the model to PDF format.

Code: [Select]
import PhotoScan
from PySide import QtGui

chunk = PhotoScan.app.document.chunk
T = chunk.transform.matrix
viewpoint = PhotoScan.app.viewpoint

main =  QtGui.qApp
for wdg in list(main.allWidgets()):
    if wdg.inherits('QGLWidget'):
        cy = wdg.height()
        cx = wdg.width()
        break

region = chunk.region
r_center = region.center
r_rotate = region.rot
r_size = region.size
r_vert = list()

for i in range(8):   #bounding box corners
r_vert.append(PhotoScan.Vector([0.5 * r_size[0] * ((i & 2) - 1), r_size[1] * ((i & 1) - 0.5), 0.25 * r_size[2] * ((i & 4) - 2)]))
r_vert[i] = r_center + r_rotate * r_vert[i]

height =  T.mulv(r_vert[1] - r_vert[0]).norm()
width  = T.mulv(r_vert[2] - r_vert[0]).norm()

if width / cx > height /cy:
scale = cx / width
else:
scale = cy / height

PhotoScan.app.viewpoint.coo = T.mulp(chunk.region.center)
PhotoScan.app.viewpoint.mag = scale
PhotoScan.app.viewpoint.rot = chunk.transform.rotation * r_rotate

crs = PhotoScan.CoordinateSystem("LOCAL")
chunk.exportModel(path, format="pdf", projection=crs)

As for the script that should use the coordinates of the bounding box corners, are you planning to specify all eight points or four laying in the plane?

What modifications should I make in this code to be compatible with version 1.3.0?
Title: Re: Update view and bounding box for each chunk
Post by: Alexey Pasumansky on March 09, 2017, 11:26:59 AM
Hello pablorov,

In the version 1.3, you can use the following code:
Code: [Select]
import PhotoScan

chunk = PhotoScan.app.document.chunk
T = chunk.transform.matrix
viewpoint = PhotoScan.app.viewpoint
cx = viewpoint.width
cy = viewpoint.height

region = chunk.region
r_center = region.center
r_rotate = region.rot
r_size = region.size
r_vert = list()

for i in range(8):   #bounding box corners
r_vert.append(PhotoScan.Vector([0.5 * r_size[0] * ((i & 2) - 1), r_size[1] * ((i & 1) - 0.5), 0.25 * r_size[2] * ((i & 4) - 2)]))
r_vert[i] = r_center + r_rotate * r_vert[i]

height =  T.mulv(r_vert[1] - r_vert[0]).norm()
width  = T.mulv(r_vert[2] - r_vert[0]).norm()

if width / cx > height /cy:
scale = cx / width
else:
scale = cy / height

PhotoScan.app.viewpoint.coo = T.mulp(chunk.region.center)
PhotoScan.app.viewpoint.mag = scale
PhotoScan.app.viewpoint.rot = chunk.transform.rotation * r_rotate

crs = PhotoScan.CoordinateSystem("LOCAL")
chunk.exportModel(path, format="pdf", projection=crs)
Title: Re: Update view and bounding box for each chunk
Post by: janalbertschenk on January 30, 2019, 04:56:43 PM
Hello,


I tried this in Metashape 1.5.0, but it doesn't work. Replaced PhotoScan with MetaShape (doesn't make any difference luckely) but i gives an error on line 6

>>>cx = vp.width

Error:
2019-01-30 14:48:26 Traceback (most recent call last):
2019-01-30 14:48:26   File "C:/test2ps.py", line 6, in <module>
2019-01-30 14:48:26     cx = viewpoint.width
2019-01-30 14:48:26 AttributeError: 'int' object has no attribute 'width'
2019-01-30 14:48:26 Error: 'int' object has no attribute 'width'
>>>

I tried different things but no luck. Can you help?
I am a noob in Python, so if its clear what the error is > sorry
Title: Re: Update view and bounding box for each chunk
Post by: Alexey Pasumansky on January 30, 2019, 05:22:46 PM
Hello janalbertschenk,

Can you paste here the first six lines from your script here?

I've tried to reproduce the issue on our side, but the assignment worked properly in 1.5.0 version.
Title: Re: Update view and bounding box for each chunk
Post by: janalbertschenk on January 30, 2019, 05:34:14 PM
That is fast  :), I used your code as in reaction to Pablorov
But here it is (after a doublecheck if the error is still there):


import PhotoScan

chunk = PhotoScan.app.document.chunk
T = chunk.transform.matrix
viewpoint = PhotoScan.app.viewpoint
cx = viewpoint.width
cy = viewpoint.height

Title: Re: Update view and bounding box for each chunk
Post by: Alexey Pasumansky on January 30, 2019, 05:47:48 PM
Hello janalbertschenk,

Can you do it line by line in Metashape Pro 1.5.0 GUI?

Code: [Select]
import Metashape as PhotoScan
chunk = PhotoScan.app.document.chunk
T = chunk.transform.matrix
viewpoint = PhotoScan.app.viewpoint
print(viewpoint)
cx = viewpoint.width
cy = viewpoint.height
Title: Re: Update view and bounding box for each chunk
Post by: janalbertschenk on January 30, 2019, 05:57:51 PM
Hello Alexey

Ok i did line for line in the console, the result of the print was 5, the error messages stays the same

>>> import Metashape as PhotoScan
>>> chunk = PhotoScan.app.document.chunk
>>> T = chunk.transform.matrix
>>> viewpoint = PhotoScan.app.viewpoint
>>> print(viewpoint)
2019-01-30 15:52:15 5
>>> cx = viewpoint.width
2019-01-30 15:52:29 Traceback (most recent call last):
2019-01-30 15:52:29   File "<console>", line 1, in <module>
2019-01-30 15:52:29 AttributeError: 'int' object has no attribute 'width'
>>> cy = viewpoint.height
2019-01-30 15:52:51 Traceback (most recent call last):
2019-01-30 15:52:51   File "<console>", line 1, in <module>
2019-01-30 15:52:51 AttributeError: 'int' object has no attribute 'height'
>>>
Title: Re: Update view and bounding box for each chunk
Post by: janalbertschenk on January 30, 2019, 07:20:13 PM
He Alexey

It is solved, after restarting the computer and the program it worked. Sorry I didn't try this first but i was a little too focused on the problem.

Thanks for the fast support
Title: Re: Update view and bounding box for each chunk
Post by: Alexey Pasumansky on January 30, 2019, 07:25:41 PM
Hello janalbertschenk,

OK. I have only idea that previously something could be broken due to some invalid calls/assignments that affected the core API functionality.
Title: Re: Update view and bounding box for each chunk
Post by: kiran on March 22, 2021, 07:28:46 PM
Hello,

Code: [Select]
        chunk = doc.chunks[len(doc.chunks) - 1]

        T = chunk.transform.matrix
        viewpoint = Metashape.app.ModelView.viewpoint
        cx = viewpoint.width
        cy = viewpoint.height


I am trying to get the 'width' and 'height' of viewpoint, but I am getting the following error. I am using 1.7.2

2021-03-22 12:20:46 Traceback (most recent call last):
2021-03-22 12:20:46   File "/Users/photogrammetry/main.py", line 33, in <module>
2021-03-22 12:20:46     init_photogrammetry.autoCorrectBoundingBox()
2021-03-22 12:20:46   File "/Users/photogrammetry/photogrammetry.py", line 334, in autoCorrectBoundingBox
2021-03-22 12:20:46     cx = Metashape.app.ModelView.viewpoint.width
2021-03-22 12:20:46 AttributeError: 'getset_descriptor' object has no attribute 'width'
2021-03-22 12:20:46 Error: 'getset_descriptor' object has no attribute 'width'

any suggestions? I am thinking theres been changes to 1.7.2 version for the viewpoint. How do I access 'width' and 'height'?

Thank you

Kiran
Title: Re: Update view and bounding box for each chunk
Post by: Alexey Pasumansky on March 22, 2021, 09:58:02 PM
Hello Kiran,

Please try to use Metashape.app.model_view.viewpoint.width and Metashape.app.model_view.viewpoint.height
Title: Re: Update view and bounding box for each chunk
Post by: kiran on March 23, 2021, 06:01:14 AM
Thank you Alex,

I am able to get the width and height using Metashape.app.model_view.viewpoint.width and Metashape.app.model_view.viewpoint.height.

Kiran
Title: Re: Update view and bounding box for each chunk
Post by: mcstieg on July 25, 2021, 03:40:35 PM
Is it possible to change the viewpoints center?