Extensions - Programmatically add node to current workspace

Dear All,

What is the proper way to programmatically add nodes to the currently open workspace using an extension?

Let’s say I want to add a PythonScriptNode and a custom generated script inside it programmatically - how would I do that?

I looked at WorkspaceViewModel + WorkspaceModel but the Nodes collection is read only and RegisterNode (no idea if this is used for this purpose internally) is protected and hence not accessible.

Any tips @Michael_Kirschner2?

Thank you very much
Best regards, Eckart

@Eckart-S you should use a CreateNodeCommand and use the command executive to execute this command.



Ah that’s how it works. I will try using this for my extension.
Thank you very much Michael!

I know it has been a year but right now I have this exact same problem: from an extension, I need to make a custom node appear in Dynamo, but I don’t know how to access the DynamoModel from there in order to call for that CreateNodeCommand. Did you manage to make it work like this? Thank you!

Use the commandExecutive object in the params sent to your extension.

1 Like

Hi @clarabonillo,
Yes, it works like mentioned above.

When your extension is loaded it calls your function

public void Loaded(ViewLoadedParams p) { …

Inside it you can use

CustomNode MyNode = new CustomNode(); — using your custom node model based class
p.CommandExecutive.ExecuteCommand(new DynamoModel.CreateNodeCommand(MyNode, 0, 0, true, false));

If you want to do this later or in a different part of your extension you can save the ViewLoadedParams in a parameter for later.

Best, Eckart


Hi @Eckart-S

Thank you for answering! I have managed to make my node appear if I invoke that function in the Loaded function like this (I use two added parameters because the ExecuteCommand needs the Id and Name of the extension…):

p.CommandExecutive.ExecuteCommand(new DynamoModel.CreateNodeCommand(MyNode, 0, 0, true, false), UniqueId, Name);

However, even if I store the ViewLoadedParameters, when i try to invoke that same funcion from another function of my extension, the node is not created. Do you know why this can be? Thank you!

If you want to change the python code from a python node then the following should hopefully help you because this is how i did it for my DynaStandard Extension(https://github.com/brencass/DynaStandard_public)

The below is a way i got all Python Nodes within a graph then change all the python nodes to a given input text(python code)

At the end I managed to solve it calling the intial function of ExecuteCommand inside:

DispatcherPriority.Background, new Action(() =>
readyParams.CommandExecutive.ExecuteCommand(new DynamoModel.CreateNodeCommand(MyNode, 0, 0, true, false), “”, “”);

I don’t know if it is a very clean solution but for now at least it allows me to do what I need. Thank you very much for the replies!

this is an okay solution - what it means is that you have delayed your function call a tiny bit - the reason is that the workspace was probably not fully constructed at the time you tried to call your command.

In the future Extensions should likely have startup methods that are triggered slightly later.

You might like to use a task with a delay instead to be more explicit.

1 Like