Forum

Author Topic: Custom Menu using addMenuItem  (Read 2570 times)

geri

  • Newbie
  • *
  • Posts: 11
    • View Profile
Custom Menu using addMenuItem
« on: May 19, 2020, 11:28:41 PM »
Hello,

I've created a few python scripts for special cases of import and export (right position and order of colums, delimiter,...), have created a custom menu with "addMenuItem" and have added the file to the script folder, as described in another post. Now my custom is created at each startup, which is great.

My question is: How do I use icons for my respective commands/definitions? According to the Agisoft Python Reference (https://www.agisoft.com/pdf/metashape_python_api_1_6_0.pdf) it is possible, but I don't seem to understand, how it is done. Is there a way to use existing icons of Agisoft, or can I use *.ico-files?

And is it possible to create "line separators" in a custom menu to group specific commands together? It would really help a lot

Thanks.

Seboon

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Re: Custom Menu using addMenuItem
« Reply #1 on: May 20, 2020, 10:10:31 AM »
Hello Geri,

You have to put your icon in the same folder as your script :

   your_scripts_folder/
   
       your_script.py
   
       your_icon.png (or.svg)



You need then to create the resources file for your icon and compile it.

Create that file :

<RCC>
    <qresource prefix="/ your_scripts_folder" >
        <file>your_icon.png</file>
    </qresource>
</RCC>


and save it in  your_scripts_folder as "resources.qrc"

Make sure you have "Pyside2-rcc.exe" script in your Python Scripts folder.

What I do is to get the same Python version (https://www.python.org/downloads/release/python-352/) as that of Metashape in another folder and download inside the Pyside2 module ( https://pypi.org/project/PySide2/).

Don't forget to add Pyside2 module to your PATH .

Then open a command prompt from  your_scripts_folder and type:

"pyside2-rcc  -o resource.py resources.qrc"

This will compile your file in python format.

Now you should have:

       your_scripts_folder/
   
       your_script.py
   
       your_icon.png (or.svg)

       resources.qrc

       resources.py


Add "from PySide2.QtGui import QIcon" at the beginning of  your_script.py and
ico = ":/your_icon.png"
Metashape.app.addMenuItem("yout script name", your function,"!",icon=ico) where "!" is a shortcut if need
.

Take a look here : https://github.com/Seboon/Metashape_ScriptReloader/tree/master/ReloaderFiles for an example.

You should now have a icon in your toolbar.

Best regards!
S.Poudroux
Archaeologist - Topographer - Drone remote pilot

geri

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Custom Menu using addMenuItem
« Reply #2 on: May 25, 2020, 09:35:55 AM »
Wow, thank you for your fast and helpful reply. i will have to take some time to understand and try it. :-)