How to get entire code inside Python Script

I’m trying to get the code inside a component as output.
For example, in Grasshopper, it’d be using ghenv.Component.Code. What would be the equivalent way of doing it in Dynamo?

Also, are there hidden variables in Dynamo similar to grasshopper’s ghenv, ghdoc, etc., Dynamo API Docujmentation?

I’ve seen this (Dynamo API documentation), but how do I use this? Neither direct import nor clr.AddReference would work.


What I’m trying to do is something similar to the Ladybug’s Export Ladybug component (https://github.com/mostaphaRoudsari/ladybug/blob/master/src/Ladybug_Export%20Ladybug.py) so i could place my python scripts under VCS.

Hi erfajo,
Thanks for the reply.

I’m already familiar with the RevitAPI so I don’t think there’s anything in it that might help me extract Dynamo Python Code.

As for using python files and the Python Script From String node or sys.path.append or placing modules inside C:\Users\<>\AppData\Roaming\Dynamo\Dynamo Revit\1.3\packages , I already have those as some of my options (I think it was from one of your posts that I got the idea) In case Dynamo doesn’t have any library that allows me to do something similar to ghenv.Component.Code.

I still prefer the ladybug-tools approach of having the src and dyf files separated and under version control but I’ll settle with having the files outside if it isn’t possible yet with Dynamo.

You can parse the Dynamo file which is an XML file before Dynamo 2.0 and a JSON file for Dynamo 2.0 and after that. The python code is included inside the file. If you know the path to the Dynamo file then all you need is a parser.

Also you might be interested in dyfpy library which I wrote to convert Grasshopper GHPython components to Dynamo custom nodes. You can use them to create a Dynamo custom node from a JSON file which includes inputs, outputs and the code.

4 Likes

what should really be done here is to use the DynamoCore apis to construct an entirely new dynamoModel, use it to open workspaces, and then query the nodes for things like code. A python or zero touch node today cannot get access to the running instance of dynamo.

Does it mean that it will be possible at some point?

I’m not sure, likely not in the way you want. I know we’ve talked about this a bunch before :slight_smile:

I would advise these ways forward.

  • If you want to interact with the currently running instance of Dynamo - write an extension. You can access the workspace, nodes, etc. We’ll be looking soon into how devs can distribute packages without building their own installers.
  • write a hacky node model node that goes and gets the dynamoViewModel from the NodeViewModel and then go crazy. This is hard to find because it’s bad and likely we should remove this reference.
  • I would love to make a metaDynamo zerotouch library which would call the dynamoCore.dlls from within dynamo… this would let you start a new DynamoModel, open graphs, save graphs, inspect graphs, modify them, etc. Just isolated from the dynamo you’re currently executing in, think of this like a python engine node but for graphs and design script.
3 Likes

:rofl:

I kinda want to do this just to see what ‘bad’ means…

Thanks for all the input guys!
I’ll try out @Mostapha 's repo. I might also look into parsing XMLs.


On another note, does anyone know how to use these?

Thanks again.

the API you linked is what I alluded to in option 3 (write a meta dynamo library) which calls those APIs - to see how they are used look at how dynamo starts up.

or here for the command line runner which is a simpler use case:

@Michael_Kirschner2

Not sure what you meant by meta library but does that mean they’re only accessible using ZeroTouch nodes, i.e. they’re not available inside python nodes using methods like clr.AddReference?
Although I do have some minor experience using C#, I haven’t tried ZeroTouch yet so I’m clueless.

@Mostapha

Btw, what did you mean by XML, JSON and 2.0?
By XML, were you referring to viewing the dyf files like this: ?

<Workspace Version="1.2.0.2690" X="109.287814" Y="209.2361" zoom="0.752660" ....">
  <NamespaceResolutionMap /> 

And by json, you mean the json file inside packages: pkg.json?
What were you referring to as 2.0 though?

Thanks again.

I think @Mostapha is referring to the fact that Dynamo 2.0 and up uses json under the hood instead of xml, which is what previous versions used. I believe this was worth bringing up in more detail here as code you write today that disassembles xml data from dynamo core nodes may need to be rewritten for 2.0 and later in the future.

Check the github for info.

1 Like

ironPython emits IL just like c# as far as I know - you can import these dlls into ironpython or c#.

@jacob.small
I see, I guess that’d be more convenient.

@Michael_Kirschner2
Thanks.