I am trying to use the etabs api from grasshopper so I can bring in building structures, amend them in grasshopper and then export back to etabs. When using python 3.5, I use the below code but when I run this in grasshopper it says that there is no module named comtypes. How can I fix this.
Thanks
import os
import comtypes.client
AttachToInstance=True
if not os.path.exists(modelDir):
try:
os.makedirs(modelDir)
except OSError:
pass
modelPath=modelDir+os.sep+fileName
if AttachToInstance:
try:
EtabsObject=comtypes.client.GetActiveObject(“CSI.ETABS.API.ETABSObject”)
except (OSError,comtypes.COMError):
print(“No running instance of the program found or failed to attach.”)
sys.exit(-1)
SapModel=EtabsObject.SapModel
In Grasshopper you are using IronPython which is the C# port of Python. You cannot use it like normal python. Many common modules are not part of it.
You can use the CPython plugin instead (but then you cannot use Rhino functionality), or better you access the com apis with methods provided in DotNet. It may be much easier to write it in c# then, since it can be weird in IronPython. Check stack overflow for it. There are tons of examples.
Which API is this? You’ll need to provide more information to aid in troubleshooting. This also includes how you have installed it and set up your Rhino/GHPython/Python paths etc. etc.
As far as I can tell: this is NOT a standard Python module, you may therefore simply need to install it (i.e. ensure that all the etabs dependencies are installed correctly). Note that it depends on ctypes, which is in fact usable from IronPython, and IS part of the (Iron)Python Standard Library. That said, you may be looking at quite some dependency hell here, but I wouldn’t give up.
The Standard Library for CPython and IronPython are actually more or less identical. I assume you are referring to things like numpy, which is also not part of CPython. Maybe be more specific about things like this, prevent them fake python news from spreading.
I’d suggest trying the same if installing comtypes fails, since it is already working in your external CPython interpreter. There are these two options currently (that I know of):
I assumed that comtypes is part of an standard python install but it wasn’t ported to Ironpython. But I’m not having time nor motivation to prove that. Besides that I wouldn’t do com accessing in script components. I have successfully worked with ctypes in gh, p/invoking my own c library, but I never called a library exposed in com with it. I only did that with catia in a custom plugin done in c# . Com accessing isn’t easy at all and shouldn’t be done without a good debugger in my opinion.
I installed comtypes from pypi using pip install. I might try using c# and see if I can get it to work that way. Thanks for your suggestions and links.
hi, I have the same, issue,
and still can not solve,
if you get the solution, would you share it, please?
actually, now it is passing from this point, and now can not get the “ETABSObject”
IronPython is part of the .NET framework and already includes clr and ctypes which can be used to import similar functionality. You should not add a CPython library from PyPI.
e.g.
import clr
clr.AddReference("xxx.dll")
If you want to interact with COM objects you may find the System.Runtime.InteropServices namespace useful. It provides a wide range of types that you can use to interact with COM objects from .NET code.
Thanks Andrew, I think I was confused by using Python.NET with clr import DLL file and Python.COM with using comtypes. Now I can link ETABS API by using GH Python 3, time for more exploring.