Skip to content

Commit

Permalink
Python: Switch to loading Image3dAPI types from Image3dAPI.tlb instea…
Browse files Browse the repository at this point in the history
…d of the loader.

This depends on Image3dAPI.tlb being found in the system PATH.
  • Loading branch information
forderud authored and Fredrik Orderud committed Jul 19, 2022
1 parent 67cdb05 commit fda5761
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 25 deletions.
4 changes: 1 addition & 3 deletions TestPython/TestPython.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
## Sample code to demonstrate how to access Image3dAPI from a python script
import platform
import comtypes
import comtypes.client
import numpy as np
from utils import SafeArrayToNumpy
from utils import FrameTo3dArray
from utils import TypeLibFromObject


if __name__=="__main__":
# create loader object
loader = comtypes.client.CreateObject("DummyLoader.Image3dFileLoader")
# cast to IImage3dFileLoader interface
Image3dAPI = TypeLibFromObject(loader)
Image3dAPI = comtypes.client.GetModule("Image3dAPI.tlb")
loader = loader.QueryInterface(Image3dAPI.IImage3dFileLoader)

# load file
Expand Down
1 change: 1 addition & 0 deletions TestPython/TestPython.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<OutputPath>.</OutputPath>
<Name>TestPython</Name>
<RootNamespace>TestPython</RootNamespace>
<Environment>PATH=$(SolutionDir)x64</Environment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
22 changes: 0 additions & 22 deletions TestPython/utils.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
## Sample code to demonstrate how to access Image3dAPI from a python script
import platform
import comtypes
import comtypes.client
import numpy as np


def TypeLibFromObject (object):
"""Loads the type library associated with a COM class instance"""
import winreg

with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, "CLSID\\"+object.__clsid+"\\TypeLib", 0, winreg.KEY_READ) as key:
typelib = winreg.EnumValue(key, 0)[1]
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, "CLSID\\"+object.__clsid+"\\Version", 0, winreg.KEY_READ) as key:
version = winreg.EnumValue(key, 0)[1]

try:
major_ver, minor_ver = version.split(".")
return comtypes.client.GetModule([typelib, int(major_ver), int(minor_ver)])
except OSError as err:
# API 1.2-only compatibility fallback to avoid breaking existing loaders
if (version != "1.2") or (err.winerror != -2147319779): # Library not registered
raise # rethrow
# Fallback to TypeLib version 1.0
return comtypes.client.GetModule([typelib, 1, 0])


def SafeArrayToNumpy (safearr_ptr, copy=True):
"""Convert a SAFEARRAY buffer to its numpy equivalent"""
import ctypes
Expand Down

0 comments on commit fda5761

Please sign in to comment.