Forum

Author Topic: Python scripts fails on launch but works in console?  (Read 9397 times)

nickponline

  • Newbie
  • *
  • Posts: 38
    • View Profile
Python scripts fails on launch but works in console?
« on: May 14, 2014, 02:08:57 AM »
Hi there,

When I type the following commands in the console, it works as expected:

import PhotoScan
doc = PhotoScan.app.document
chunk = doc.chunks.add()
chunk.label = 'My Chunk'

Also if I put in in a script and execute it from Tools->Run script it works.

BUT, if I put those command in a script in ~/Library/Application\ Support/Agisoft/PhotoScan\ Pro/scripts and launch PhotoScan it fails with:

https://www.dropbox.com/s/4x3b9692kp8w5fo/Screenshot%202014-05-13%2016.08.09.png

nickponline

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #1 on: May 14, 2014, 02:12:45 AM »
I upgraded to Pro because I thought it would work :( what is the difference? How can I get the behavior I want from the script to run automatically.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #2 on: May 14, 2014, 07:09:01 AM »
Hello nickponline,

Access to PhotoScan.app.document is blocked for scripts put in autostart folder. So to start processing script it is necessary to run it from GUI via Run Script dialog or custom menu item created using autorun scripts.
Best regards,
Alexey Pasumansky,
Agisoft LLC

nickponline

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #3 on: May 29, 2014, 11:59:21 PM »
Thanks. Ok I've finally changed my script to be run from the GUI instead of the autostart folder. Now how can I get photoscan to execute Run Script when it start automatically?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #4 on: May 31, 2014, 02:31:42 PM »
Hello nickponline,

Processing script should be started by the operator.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Dmitry Bezrukov

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #5 on: November 11, 2016, 05:07:42 PM »
Hello nickponline,

Processing script should be started by the operator.

Hello,

Is there any way to run PhotoScan Python scripts in headless mode, without needing to click "Tools->Run Script"?
I'd like to setup PhotoScan on a linux server, that doesn't have Desktop installed.
It would be nice to run it from console with command like this:
xvfb-run ./photoscan -script /path/to/the/script.

best,
Dmitry

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #6 on: November 11, 2016, 05:27:14 PM »
Hello Dmitry,

In the next version of PhotoScan Pro (v. 1.3) it would be possible to run the script from the command line using -r key and path to the script as an argument. You can download the version 1.3 pre-release from the corresponding forum thread:
http://www.agisoft.com/forum/index.php?topic=5976.0

So it should work like:
Code: [Select]
./photoscan.sh -r script.py
Best regards,
Alexey Pasumansky,
Agisoft LLC

Dmitry Bezrukov

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #7 on: November 14, 2016, 07:47:59 AM »
Hello Dmitry,

In the next version of PhotoScan Pro (v. 1.3) it would be possible to run the script from the command line using -r key and path to the script as an argument. You can download the version 1.3 pre-release from the corresponding forum thread:
http://www.agisoft.com/forum/index.php?topic=5976.0

So it should work like:
Code: [Select]
./photoscan.sh -r script.py

Thanks, that's great!

venkatg

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #8 on: November 19, 2016, 12:33:20 AM »
Hi Alexey Pasumansky,

What is the expected date for stable release of v1.3?  Thanks!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #9 on: November 19, 2016, 11:54:19 AM »
Hello venkatg,

I think it would be released in the end of this year, so might be late December.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Dmitry Bezrukov

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #10 on: December 21, 2016, 05:45:17 PM »
Hello Alexey,

The '-r' key works well in PhotoScan Pro v1.3, but I've noticed an error in its log:
'Can’t save dense project file'.

In spite of the error the project file being saved successfully.
So should I pay attention to that error or it's just a bug with console output?

---Build dense cloud...
---Save dense project: /path/to/denseprojfile.psx
  SaveProject
  saved project in 1.029 sec
  LoadProject
  loaded project in 0.004735 sec
  ---Can’t save dense project file
  LoadProject
  loaded project in 0.003707 sec
---Build digital elevation model...
---Build orthomosaic...

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #11 on: December 21, 2016, 06:10:06 PM »
Hello Dmitry,

Can you post a part of code that is related to the following output:
Code: [Select]
---Save dense project: /path/to/denseprojfile.psx
  SaveProject
  saved project in 1.029 sec
  LoadProject
  loaded project in 0.004735 sec
  ---Can’t save dense project file
  LoadProject
  loaded project in 0.003707 sec

I see that the project is saved, but due to some additional custom messages in the console, I assume that some additional operations may be performed. Or the printing function related to the "  ---Can’t save dense project file" is not properly implemented.
The difference, as I assume, may be related to the different handling of the errors in the version 1.2 and 1.3: now processing and saving operations do not return Boolean value (maybe in your script you are checking that), now if the operation fails it results in RuntimeError, for example:
Code: [Select]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-47-0b71e7b52316> in <module>()
----> 1 doc.save(__some_invalid_path__)

RuntimeError: Can't save project
So you need to catch such operations errors with try&except approach:
Code: [Select]
try:
      doc.save()
except RuntimeError:
      print("Oops, can't save project.")

Hope it will solve the problem.
« Last Edit: December 21, 2016, 06:11:40 PM by Alexey Pasumansky »
Best regards,
Alexey Pasumansky,
Agisoft LLC

Dmitry Bezrukov

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #12 on: December 21, 2016, 08:49:36 PM »
@Alexey

Exactly, we just needed to replace the old Boolean code with new Try/Catch one.
Thank you!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Python scripts fails on launch but works in console?
« Reply #13 on: December 21, 2016, 08:55:18 PM »
Hello Dmitry,

Nice to hear that it works. If you were using some additional checks for other processing options in a similar way (like matchPhotos(), buildDenseCloud() and etc.), then you also need to replace it accordingly.
Best regards,
Alexey Pasumansky,
Agisoft LLC