Python Automation

Hi All, @stevebaer
is there a python sample for external automation?
Like this: rhinocommon/examples/AutomationSample at master · mcneel/rhinocommon · GitHub

That way it would be possible to use python libraries which do not work from IronPython.

Thanks,
Jess

We don’t currently have any automation samples for this. Are you trying to run Rhino from IronPython?

No, I want to automate Rhino from an external python environment.
Do you think it is possible to access the ScriptObject via some COM stuff?

Which external python environment? CPython or IronPython

I think it is possible, just haven’t written anything like this in quite a while.

cpython (right now 2.7.11)

Looks like this old forum thread can help

Hi Steve, OK I’ve installed the comtypes and adjusted the path to the Rhino folder.
Now I have the following code, but it looks like I’m missing something:

from comtypes.client import GetModule
from comtypes.client import CreateObject

name="Rhino5x64.Interface"
tlb = "C:\Program Files\Rhinoceros 5 (64-bit)\System\Rhino5.tlb"
tls = "C:\Program Files\Rhinoceros 5 (64-bit)\Plug-ins\RhinoScript.tlb"
IRhino = GetModule(tlb)
IRhinoScript = GetModule(tls)
Rhino = CreateObject(name)
RS = Rhino.QueryInterface(IRhino.IRhino5x64Interface).GetScriptObject()
rs = RS.QueryInterface(IRhinoScript.IRhinoScript)
rs.Print("Hello Rhino!")

Any hints what’s going on?
Thanks, Jess

I believe @bar was the person who figured this out. Maybe he knows what is going wrong.

Thanks Steve! Today it works :wink:
Edit: Also big thanks to @bar

@dan this may be a start for the python section once you add something about automation to the developer docs.

I wonder if “external access” is also possible on Mac.

Jess

The python section of that new dev docs site is really thin right now. Only my list to cleanup/consolidate. But feel free to work on it too! :wink:

Hi All!

Have been trying to call Rhino5 by Cpython 2.7.10 COM calls to automate some items. Have tried to implement the following code from discussion above but get the following error:

AttributeError: ‘NoneType’ object has no attribute ‘QueryInterface’ upon execution at the line: rs = RS.QueryInterface(IRhinoScript.IRhinoScript)

Has anyone encountered a similar problem? Seems like I must be missing something simple.

import comtypes.client

from comtypes.client import GetModule
from comtypes.client import CreateObject
name="Rhino5x64.Interface"
tlb = "C:\Program Files\Rhinoceros 5.0 (64-bit)\System\Rhino5.tlb"
tls = "C:\Program Files\Rhinoceros 5.0 (64-bit)\Plug-ins\RhinoScript.tlb"
IRhino = GetModule(tlb)
IRhinoScript = GetModule(tls)
from comtypes.client import CreateObject
Rhino = CreateObject(name)
RS = Rhino.QueryInterface(IRhino.IRhino5x64Interface).GetScriptObject()
rs = RS.QueryInterface(IRhinoScript.IRhinoScript)
rs.Print(“Hello Rhino”)

@Jess
Did you ever try to update your code to Rhino 6? I am currently trying to connect a collection of old .py files with Rhino6. The Python files correspond to python version 2.7

I adapted your code as follows after reading some of what @dale had posted on this matter in various threads.

from comtypes.client import GetModule
from comtypes.client import CreateObject

name=“Rhino.Interface.6”
tlb = “C:\Program Files\Rhino 6\System\Rhino.tlb”
tls = “C:\Program Files\Rhino 6\Plug-ins\RhinoScript.tlb”
IRhino = GetModule(tlb)
IRhinoScript = GetModule(tls)
Rhino = CreateObject(name)
RS = Rhino.QueryInterface(IRhino.IRhinoInterface6).GetScriptObject()
rs = RS.QueryInterface(IRhinoScript.IRhinoScript)
rs.Print(“Hello Rhino!”)

The error I get:
RS = Rhino.QueryInterface(IRhino.IRhinoInterface6).GetScriptObject()
AttributeError: ‘module’ object has no attribute ‘IRhinoInterface6’
Any advice is much appreciated!
Thank you.

Hi @ChrisH2a ,

No, I have not updated that to Rhino 6. I seem to remember that there were some post about V6 Automation here on discourse…

If these are IronPython compatible then you may use it directly with Rhino’s _EditPythonScript interface.

Recently I tested some CPython scripts in Rhino V8. It works some how via the new RhinoCode interface, but it is still very early “Work In Progress”.

Jess

1 Like

Hi, thank you so much for that encouragement! Had tried this before and did not work, but went back to it and got it running.