[De...] Posted October 15 Share Posted October 15 Hi, I was wondering if I could import a .gelement/.zelement /.gxml as an add-on resource, then import that resource into my project (clipboard). I tried reading https://zeissiqs.github.io/zeiss-inspect-addon-api/2023/howtos/python_api_introduction/using_script_resources.html but I'm not having much luck... Any advice? Link to comment Share on other sites More sharing options...
[Ma...] Posted November 4 Share Posted November 4 Hi, import_gom_inspection_exchange needs a regular file name, neither a file handle nor a resource object works instead. You can use the following workaround: import gom import tempfile import os # Copy the resource to a regular (temporary) file with gom.Resource('Points.gxml').open() as gxml_file, tempfile.NamedTemporaryFile(delete=False) as temp_file: data = gxml_file.read() temp_file.write(data) # Import from temporary file gom.script.sys.import_gom_inspection_exchange (files = [temp_file.name], import_mode = 'clipboard') # Remove temporary file try: os.remove(temp_file.name) except: pass Best regards, Matthias Link to comment Share on other sites More sharing options...
[De...] Posted November 26 Author Share Posted November 26 Thank you Matthias, that worked perfectly!! I initially tried to reference https://zeissiqs.github.io/zeiss-inspect-addon-api/2023/python_api/python_api.html#example & load it directly from the add-on filepath, but that only worked when the add-on was in edit mode. upon further reading it looks like the .addon is just a zip file, so I used zipfile to unzip it into an "extracted" folder. (tempfile and unzipping was giving me problems for some reason) Both methods seem to work, thank you for your help!! # -*- coding: utf-8 -*- import gom import os import tempfile import zipfile Method = 1 #Method = 2 if Method == 1: # Copy the resource to a regular (temporary) file with gom.Resource('Large_Fixture_CSYs.zelements').open() as zelements, tempfile.NamedTemporaryFile(delete=False, mode = 'wb') as temp_file: data = zelements.read() temp_file.write(data) # Import from temporary file gom.script.sys.import_project( file=temp_file.name, import_mode='clipboard') # gom.script.sys.import_gom_inspection_exchange (files = [temp_file.name], import_mode = 'clipboard') # Remove temporary file try: os.remove(temp_file.name) except: pass if Method == 2: # Resource paths Large_Fixture = 'scripts/Resources/Large_Fixture_CSYs.zelements' # Full relative path inside the .addon archive # Get the current add-on current_addon = gom.api.addons.get_current_addon() # Check if the add-on is in edit mode if current_addon.is_edited(): # If the add-on is in edit mode, combine the resource path with the add-on's file path Resource_fpath = os.path.normpath(os.path.join(current_addon.get_file(), Large_Fixture)) print(f"Add-on is in Edit Mode. Using:\n{Resource_fpath}") else: # If the add-on is not in edit mode, extract the specific file from the .addon archive addon_zip_path = os.path.normpath(os.path.join(gom.app.public_addon_directory, f"{current_addon.get_name()}.addon")) print(f"Add-on is NOT in Edit Mode. Extracting .addon file:\n{addon_zip_path}") # Define the extraction directory (same as .addon file location, in a subfolder named 'Extracted') extraction_dir = os.path.join(os.path.dirname(addon_zip_path), "Extracted") os.makedirs(extraction_dir, exist_ok=True) # Extract only the specific file with zipfile.ZipFile(addon_zip_path, 'r') as addon_zip: if Large_Fixture in addon_zip.namelist(): # Extract the file extracted_path = os.path.join(extraction_dir, os.path.basename(Large_Fixture)) with open(extracted_path, 'wb') as extracted_file: extracted_file.write(addon_zip.read(Large_Fixture)) print(f"Specific file extracted to:\n{extracted_path}") Resource_fpath = extracted_path else: raise FileNotFoundError(f"The file '{Large_Fixture}' does not exist in the .addon archive.") # Import the resource from the resolved path gom.script.sys.import_project( file=Resource_fpath, import_mode='clipboard') Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in