How to execute a Dynamo graph inside a python script?

Hi! It’s my first post here!

I need to chain run a few different Dynamo graphs from a single python script or combine Dynamo graphs with Python code.

Something like this:

foo()
dynamo.run("script1.dyn")
bar()
dynamo.run("script2.dyn")
foo()

Since pyRevit allows running .dyn scripts from Ribbon I figured it is possible. I’ve tried reading pyRevit source and looking in Internet but without any luck.
Thanks for help!

@Andrzej ,

i recognized that running a dynamo script takes even more time when you have insteat a clean python code.

or follow this

KR

Andreas

So far I have come up with this:

import clr
clr.AddReference('PyRevitLoader')
from PyRevitLoader import ScriptExecutor
import pyrevit

py_executor = ScriptExecutor()
print(py_executor.ExecuteScript("d:\\test.py"))
# this succedes

dyn_executor = ScriptExecutor()
print(dyn_executor.ExecuteScript("d:\\test.dyn"))
# this does not

Result

Succeeded
Failed

Currently I’m stuck with setting up dynamo engine for ScriptExecutor.

1 Like

in my opinion, that amount of daisy chaining will more often than not cause Revit to crap on itself and crash. also, the execution time is going to be slower - since there are more wrappers between the script execution and Revit.
I would advise combining the 2 dynamo scripts into1 and then executing through a pushbutton

1 Like

I have a partial solution:

import clr
clr.AddReference('PyRevitLoader')
from PyRevitLoader import ScriptExecutor
from pyrevit.runtime.types import DynamoBIMEngine, ScriptData, \
    ScriptRuntime, ScriptRuntimeConfigs

scriptdata = ScriptData()
scriptdata.ScriptPath = "d:\\test.dyn"
config = ScriptRuntimeConfigs()
runtime = ScriptRuntime(scriptdata, config)
engine = DynamoBIMEngine()
engine.Execute(runtime)

This will execute a graph. They can be chained. Still, it requires open Dynamo window to work, because otherwise it throws this:

image

So, I still need my way around EngineManager.

1 Like

We have a policy to go with Dynamo whenever it is possible. The reason is that there aren’t that many Pythoners available at hand to hire and I am the only one currently in my company. Dynamo is usally easier for newcomers.

I still need to programatically chain run Dynamo graphs as it will speed up things greatly.

you could be the first to post in the Jobs category :smile: @Andrzej

1 Like

Let’s just say that I don’t care about the overhead. Secondly Revit craps when code is poorly written.

That’s not a solution for me. Our Dynamo graphs are complex enough already and adding more makes them unmanagable and almost impossible to imporve later on. I have been into Dynamospaghetti world once and I’m not coming back.

1 Like

I will remember to do that.

you can start Dynamo before and see if that helps using this piece of code

from pyrevit import HOST_APP,UI
uiapp = HOST_APP.uiapp

RevitCommandId commandId = UI.RevitCommandId.LookupPostableCommandId(UI.PostableCommand.Dynamo)
uiapp.PostCommand(commandId)

1 Like

@Andrzej thank you for posting this, did you solve the issue. I am facing the same issue of running dynamo using pyrevit.