Run Dynamo script from Revit API

I am looking for a way to run dynamo script with Revit API programmatically.

Following code execute simple dynamo scripts successfully but unable to run dynamo script which requires user interaction (Select from dwg or input some values)

//Get application and document objects and start transaction
            UIApplication uiapp = commandData.Application;
            Document doc = uiapp.ActiveUIDocument.Document;

            // Run dynamo script without dynamo player..
            string Journal_Dynamo_Path = @"<dynamo_script_path>";
            var dynamoRevit = new DynamoRevit();

            var dynamoRevitCommandData = new DynamoRevitCommandData
            {
                Application = uiapp
            };

            IDictionary<string, string> journalData = new Dictionary<string, string>
            {
                { Dynamo.Applications.JournalKeys.ShowUiKey, false.ToString() }, // don't show DynamoUI at runtime
                { Dynamo.Applications.JournalKeys.AutomationModeKey, true.ToString() }, //run journal automatically
                { Dynamo.Applications.JournalKeys.DynPathKey, Journal_Dynamo_Path }, //run node at this file path
                { Dynamo.Applications.JournalKeys.DynPathExecuteKey, true.ToString() }, // The journal file can specify if the Dynamo workspace opened from DynPathKey will be executed or not. If we are in automation mode the workspace will be executed regardless of this key.
                { Dynamo.Applications.JournalKeys.ForceManualRunKey, false.ToString() }, // don't run in manual mode
                { Dynamo.Applications.JournalKeys.ModelShutDownKey, true.ToString() },
                { Dynamo.Applications.JournalKeys.ModelNodesInfo, false.ToString() }

            };

            dynamoRevitCommandData.JournalData = journalData;
            Result externalCommandResult = dynamoRevit.ExecuteCommand(dynamoRevitCommandData);

            DynamoRevit.RevitDynamoModel.OpenFileFromPath(Journal_Dynamo_Path, true);
            DynamoRevit.RevitDynamoModel.ForceRun();

Tried to run script with Dynamo player itself, able to open dynamo player programmatically, but not able to run script using player.

The key aspect here is to edit the inputs prior to execution; You have to edit the JSON string that makes up the .dyn file directly to do so.

I have a few examples of this recently in a coming share out / package consisting of nodes I showed off with @dana.defilippi at BILT a few weeks back. Still picking up a few changes, but I hope to have it out this week.

2 Likes

I’ve used data shapes before to prompt for selection.

2 Likes

I don’t need Dynamo script, I have script which requires user interaction. I want to run that script programmatically using revit API.

Data-Shapes supports user interaction in the form of sliders, string input, select model elements, dropdowns and more.

Your question wasn’t very clear then, I’ll sit this thread out now. Good luck with your task.

1 Like

I would say that you need to re-check your dynamo script then if it will not work in Dynamo Player, because if it wont run in dynamo player it is unlikely to run with the code you have indicated above.

Dynamo Player or Dynamo CLI(the code you have shown) run dynamo without all the UI/ViewModel. Therefore maybe some code or package your using may utilise either of these areas of dynamo and therefore will not be able run/load.

Dynamo script works fine with DynamoPlayer. I am using script available in the sample folder C:\ProgramData\Autodesk\RVT 2023\Dynamo\samples\en-US\Revit\DynamoPlayer-5\Calculate Longest Exit Distance.dyn

Thinking run script with Dynamo player programmatically. Able to open dynamo player using following code, but not sure how to play script (programmatically with Revit API)

            UIApplication uiapp = commandData.Application;
            Document doc = uiapp.ActiveUIDocument.Document;
            RevitCommandId commandId = RevitCommandId.LookupPostableCommandId(PostableCommand.DynamoPlayer);
            uiapp.PostCommand(commandId);
1 Like

Hey Jacob, where can I find these examples?