Forum

Author Topic: Run script on start  (Read 27484 times)

marchewa

  • Newbie
  • *
  • Posts: 1
    • View Profile
Run script on start
« on: December 23, 2013, 08:34:01 PM »
Hello

I've seen that kind of problem with no solution on this forum. I'm thinking on using PhotoScan Pro as a part of whole process of object creation that will be fully automatic. So there is a need to auto-run my own script on start of PhotoScan. From what I read I assume it was possible in the past. Is it still now?

Regards
Przemek

jmsuomal

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Run script on start
« Reply #1 on: January 07, 2014, 12:55:23 PM »
+1

I would also appreciate addition of this feature. Basic commandline syntax would be great:

>> photoscan.exe "c:\Temp\PythonScript.txt"

captured

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Run script on start
« Reply #2 on: January 22, 2014, 06:34:40 AM »
Hey guys. If you're on Windows, you can accomplish this by placing a script in "C:/users/<user name>/AppData/Local/AgiSoft/PhotoScan Pro/scripts" I guess that's not exactly the same as starting it from a command line, but is still pretty helpful.

I've been using that to create menu items on startup for employees to click rather than running scripts manually (another pretty useful feature).

Code: [Select]
app = PhotoScan.Application()
app.addMenuItem("Some Button", some_function)

jmsuomal

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Run script on start
« Reply #3 on: January 22, 2014, 11:35:54 AM »
Thanks captured!

That should be a workable solution.

Although I still must complain that it is not as elegant as the commandline option would be as I still need to take care of deleting the script file after it is run and if that fails for any reason the processing script remains disturbing the next "normal" starting of photoscan...

jmsuomal

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Run script on start
« Reply #4 on: January 23, 2014, 12:52:25 PM »
While we are waiting for a real commandline support, here is a workaround to run script at the start.

Contents of Run_PhotoScan_Script.bat:
Code: [Select]
:: Runs a Python script in PhotoScan from command line
::
:: SYNTAX: Run_PhotoScan_Script.bat "C:\Temp\ScriptFile.py"
::
:: Compatible (at least) with PhotoScan 1.0.0
::
:: If you want photoscan to close without user action, add these lines to the end of your Python script:
::
:: import PhotoScan
:: app = PhotoScan.Application()
:: app.quit()
::
:: YOU MAY NEED TO CHANGE THE PATHS TO MATCH YOUR PHOTOSCAN INSTALLATION

:: Dont show commands in cmd window
@echo off

echo ---RUNNING A PYTHON SCRIPT IN PHOTOSCAN---
echo .
echo DO NOT CLOSE THIS WINDOW!
echo .
echo If you do so before the batch finishes, the temporary script file will not be deleted and it will run again always when you start PhotoScan normally! If you do it by accident, delete manually the file:
echo %LOCALAPPDATA%\Agisoft\PhotoScan Pro\scripts\TempScript.py
echo .

:: Copy the input script file to PhotoScan scripts folder
echo Copying script file to PhotoScan Script folder...
copy %1 "%LOCALAPPDATA%\Agisoft\PhotoScan Pro\scripts\TempScript.py" /Y

:: Run PhotoScan and wait until it is closed
echo PhotoScan is started.
echo Waiting for PhotoScan to close... (you might need to do it manually)
call "c:\Program Files\Agisoft\PhotoScan Pro\photoscan.exe"

:: Delete the script file so that it is not run again when user runs next time photoscan normally
echo Deleting the script file in PhotoScan script folder...
del "%LOCALAPPDATA%\Agisoft\PhotoScan Pro\scripts\TempScript.py"

:: closes the cmd window
exit

And here is a simple TestScript.py:
Code: [Select]
import PhotoScan
app = PhotoScan.Application()

# Show a message to user
app.messageBox("Scripting works! PhotoScan will now close.")

# Close photoscan
app.quit()


Running example:

   > Run_PhotoScan_Script.bat "C:\Temp\TestScript.py"

It is even better to run this bat somehow hidden (without cmd window) so that  user cannot accidentally close it prematurely. I found a solution for that using an antique exe that still seems to work with Win7:

http://www.joeware.net/freetools/tools/quiet/index.htm

Place the quiet.exe in the same folder as the bat and you can run:

   > quiet "Run_PhotoScan_Script.bat" "C:\Temp\TestScript.py"

r0xx

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Run script on start
« Reply #5 on: November 28, 2014, 04:53:27 PM »
Awesome thanks!
Will it ever be possible to run PhotoScan with a script from a shortcut like:
Photoscan.exe -python MyScript.py?
« Last Edit: December 17, 2014, 06:53:14 PM by r0xx »

nickc

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Run script on start
« Reply #6 on: January 09, 2015, 06:44:21 PM »
anyone try putting scripts in the start folder? I am able to run a script from console but running the same script from start folder seems to drop some functions. more specifically;

chunk = PhotoScan.app.document.addChunk() gets the error;

attributeError: 'NoneType' object has no attribute 'addchunk'

I am using 1.1 2004.
« Last Edit: January 09, 2015, 06:52:11 PM by nickc »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14971
    • View Profile
Re: Run script on start
« Reply #7 on: January 09, 2015, 08:19:38 PM »
Hello nickc,

Access to Document class is blocked for autorun scripts.
Best regards,
Alexey Pasumansky,
Agisoft LLC

nickc

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Run script on start
« Reply #8 on: January 09, 2015, 08:49:10 PM »
thanks alexey

is the intent for autorun scripts to be used solely for loading custom ui ?

nickc

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Run script on start
« Reply #9 on: January 09, 2015, 10:08:41 PM »
For example...you could do something like this? prompt the user to run a specific script where the project resides on launch.

Code: [Select]
import PhotoScan
from PySide import QtCore, QtGui
import os

path = os.getcwd()
print (path)

app = QtGui.QApplication.instance()
parent = app.activeWindow()

msg = "Would you like to run data for the current project location?"
reply = QtGui.QMessageBox.question(parent, 'Message', msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

if reply == QtGui.QMessageBox.Yes:
print("yes")
PhotoScan.ConsolePane("AgisoftRun.py")
else:
print("no")

im not sure what the proper way to run a script from agisoft console is??
« Last Edit: January 09, 2015, 10:32:47 PM by nickc »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14971
    • View Profile
Re: Run script on start
« Reply #10 on: January 11, 2015, 03:03:39 PM »
Hello Nick,

Using autorun scripts you can create custom menus and dialogs that can start customized processing workflow.

So basically, you can define your external script as Python function (via def) and link the custom menu to this function, then put such script to the autorun folder.
Here's short example:
Code: [Select]
import PhotoScan

def test():

print("Testing successfull. Chunk added.")
doc = PhotoScan.app.document
chunk = PhotoScan.Chunk()
chunk.label = "Test chunk"
doc.addChunk(chunk)

return 1

def funct():
doc = PhotoScan.app.document
chunk = doc.chunk

chunk.matchPhotos()
chunk.alignCameras()

print("Done")


label = "New Menu/Test script"
PhotoScan.app.addMenuItem(label, test)

label = "New Menu/Processing script"
PhotoScan.app.addMenuItem(label, funct)


print("New menu added")
Best regards,
Alexey Pasumansky,
Agisoft LLC

nickc

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Run script on start
« Reply #11 on: January 12, 2015, 05:29:28 PM »
well thats pretty awesome. thanks!

nickc

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Run script on start
« Reply #12 on: January 12, 2015, 05:58:36 PM »
one more question... is it possible to associate a new hot key for custom menu items?

edit - nevermind found it :)
« Last Edit: January 12, 2015, 06:09:55 PM by nickc »

r0xx

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Run script on start
« Reply #13 on: August 19, 2015, 05:51:35 PM »
Is the document access still blocked from the startscrip?
I have a batch file which copies a script into the scripts folder and then starts up PhotoScan.
Now I want to load images into this new document, compute some stuff and the save and exit. Running the script from inside is perfect but I would need it to run without any user interaction...

r0xx

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Run script on start
« Reply #14 on: August 26, 2015, 11:29:49 AM »
Anyone?