Jump to content

Can't Find File by Relative Path in Add-on Structure


---
 Share

Recommended Posts

Hello everyone,

I'm working on a Python project structured as shown in the attached image, and I'm having trouble accessing some files using a relative path.

The file is located inside the scripts subdirectory of my project's root folder named My Add-on Hello World. My script that needs to access this file is also located within the same scripts directory.
image.png.4adbf1b5a7d97f1271bc78bb8190b89d.png

Here's the snippet of code where I attempt to open the file:

1.
creds = service_account.Credentials.from_service_account_file("ratc-python-cloud-01e34e139bdc.json", scopes=SCOPES)

2.
with open('version.txt', 'r') as file:
  version_content = file.read()
    

image.thumb.png.cf3a1f9917e5591caabdf790d59d380d.png

In addition to the above issue, I would also like to know if there's a way to obtain the actual "path" to the files within the add-on package, rather than just reading the content via resources.

Any guidance or suggestions would be greatly appreciated!

Thank you in advance.

Link to comment
Share on other sites

Please sign in to view this quote.

Just adding that I would also be interested to know where the script .py files are stored in the 2023 software. Prior to ZEISS 2023 I have used an overnight backup task that copies our scripts to a network folder for safekeeping in the event of a hard drive or computer failure, however it doesn't appear that ZEISS 2023 uses the C:\Users\user\AppData\Roaming\gom\2023\gom_scripts folder anymore? The only place I have found the scripts on the hard drive so far is within the .addon file containing them, but since this file doesn't seem to update unless the user clicks the "Finish Editing" button for them in the Add-on Editor, it isn't a surefire source to copy from for scheduled backups. Surely the .py files must exist somewhere outside of the add-ons?

Thanks,
Michael Henson

Link to comment
Share on other sites

Please sign in to view this quote.

Hello,

Accessing files within an Add-on depends on whether the Add-on is in editing mode or not.

Here is a version which works for the upcoming ZEISS INSPECT 2023 Service Pack 1:

import gom

addon = gom.api.addons.get_current_addon ()

print('All files in current Add-on:')
if addon.is_edited():
	for root, dirs, files in os.walk(addon.get_file ()):
		for file in files:
			print(os.path.join(root, file))
else:
	for file in addon.get_file_list():
		print (file)

path = addon.get_file()
print('Current path:', path)

if addon.is_edited():
	with open(os.path.join(path, 'scripts', 'version.txt'), 'r') as file:
		version_content = file.read()
else:
	version_content = addon.read('scripts/version.txt')
	version_content = version_content.decode("utf-8")
	
print('Version:', version_content)

As far as I remember, `is_edited()` is new and `get_file` has been modified recently.

Link to comment
Share on other sites

Please sign in to view this quote.

Hello,

The Add-ons in editing mode are located in 

gom.app.user_edited_addon_directory

which is currently C:\Users\<USERID>\AppData\Roaming\gom\2023\gom_edited_addons. Each Add-on has a directory named by its ID.

Best regards,

Matthias Prinke

Link to comment
Share on other sites

Please sign in to view this quote.

Dear Matthias,

Thank you for your helpful explanation on file access in ZEISS INSPECT 2023. It works perfectly.

Best regards,
Sean Tsai

Link to comment
Share on other sites

 Share

×
×
  • Create New...