Execute Python script from .NET (within document context)

Execute Python script from .NET (within document context)

Anonymous
Not applicable
1,820 Views
3 Replies
Message 1 of 4

Execute Python script from .NET (within document context)

Anonymous
Not applicable

Looking for help on executing Python script from my Revit .NET Add-In. The script should execute within Revit document context.

 

I've downloaded the RevitPythonShell source code and am digging through it to try and find my answer, but am struggling to find the critical parts.

 

My basic goal is this... Given I already know the path of a Python script, how do I simply execute it from my .NET Add-In within document context.

 

Any help is much appreciated!

Jon

0 Likes
1,821 Views
3 Replies
Replies (3)
Message 2 of 4

stever66
Advisor
Advisor

The Revit Python Shell was great, and its how I got my start programming in Revit.   But are you aware that with the more recent versions of Revit, you can write Macros in Python using the integrated Sharp Develop Editor? It makes me wonder if the Revit Python Shell is still necessary.  You can look at the Revit help macros topic for more info:

 

http://help.autodesk.com/view/RVT/2019/ENU/?guid=GUID-4DFDA8CD-B0FD-492E-8EDE-A28C29B1E316

 

But regarding your question about starting one script from another:  I'm still pretty much an amateur with this programming stuff, or maybe I don't understand the question.  But I always assumed you could start one script from another by adding a reference to the second script, and then calling the command by Namespace. Command.

 

For example, say you have a dll called PyDLL, with a namespace called PyName, and a command called PyCommand.  

 

Can't you just add PyDLL as a reference (just like you add the RevitAPI.DLL and RevitAPIUI.DLL references)? And then just call PyName.PyCommand?

 

Maybe its more complicated if one program is C# and one is Python?

 

 

 

 

0 Likes
Message 3 of 4

Anonymous
Not applicable

@stever66, thanks for your response. Ironically my attention was drawn to the Macro Manager yesterday that as you mention allows Python programming among other languages. I will likely lean towards this as part of my solution since the .NET Revit API has straight forward methods for executing these Macros.

 

To clarify my goals, my Revit Add-In should have functionality that will allow user to dynamically define any number of scripts/macros they choose to execute at certain points during other processes of my Add-In. Basically allowing them to customize what my Add-In accomplishes. That said, hard coding a reference to particular script/macros in my source code is not an option as you mentioned.

 

I'm still quite interested in how to execute a Python script that is NOT part of a Macro, but I think using the Macros may be my best option as it allows users to customize using their language of choice.

 

Thanks again @stever66 !

 

Jon

0 Likes
Message 4 of 4

stever66
Advisor
Advisor

I don't know why I didn't think of this yesterday, but the recent versions of Visual Studio also support Python.

 

0 Likes