Functions in Python

Let’s say I’ve got a dozen functions I want to use…

I could stick them all in my Python node but it’s a bit full :person_shrugging:

What’s the best way of using multiple functions in a script do you think?

What do you mean exactly? What’s the issue you’re trying to avoid?

If I’ve got 200 lines of functions… Can I stick them in another block(s) and read from them?

I mean… suppose I could make external modules or something too.

I’m wondering what people do if they’ve got walls of code in one Python block.

Off the top of my head:

  1. Save a .py somewhere with your functions and load it as an external module. You’ll have to distribute that .py and put it in place for others to use it by adding to their environment or loading it from a configurable path though.
  2. Leave it all in the python node
  3. Build the definitions into one .txt file, and the actual code that calls them in another text file, and load that into the Dynamo environment using the ‘from string’ Python node and a File.ReadText and some string joining

Personally, if I’m going to go with Python it’s all going into one node with no external dependencies. Otherwise you have issues with stuff like getting dependencies out to end users (option 1), or paths changing (option 1 and 3). If I find it’s too much to reasonably work with in that environment (a personal preference - I cap out around 800 lines, less if I only have my laptop to work on) I am ditching Python for C# and a zero touch node.

2 Likes

You have a couple of options:

Ignore it and work through but make sure you comment and make names reasonable to what it is. I have had a python node that had to be 1000+ lines of code with a class object and sub functions totalling 700 lines, it is all about managing it as best as possible for your use case.

At least 5% of this 1000 line code was commenting/documentation.

Or

You could split things into separate python nodes and run the data through as well.

2 Likes

If the only thing you’re trying to avoid is extra lines of code, I’d say that’s a welcome compromise for stable and independent functionality. You can add in header comments to break up your python code into distinct sections to help. I also appreciate being able to reference the function definitions when writing or modifying my working code.

Personally, for big scripts, I use Python nodes as modules (which are actually classes).

Example

5 Likes

Definitely agree with Jacob, breaking your code into separate .py’s is a good move and is how a lot of Python developers work (see pyRevit’s code for a good example). This will also prepare you for C# development where breaking code up into separate files/areas and leaning into the ‘using’ keyword will be the norm.

From then you can use the syntax:


from yourPyName import yourFunctionName, yourOtherFunctionName, etc.

Or you can use * to just import the lot.

I recommend giving the .py file a name that sits in front of the functions to ensure they stay unique, but that’s just a quirk of mine. I also tend to get each library to reference in the current document and pass it as a default argument to any function using document (same with app, uidoc etc.), that way if you forget to specify it in a function, it will still round it up anyway.

I’ve got a video on this topic here including the method to append in the path. You can dynamically append in a .py in a custom package I think, check out Bumblebee’s code which does this.

Here’s a screengrab showing my approach to bundling my functions into .py files that I use in pyRevit, but could apply in Dynamo also:

image

1 Like

Perfect, this is nice. Thanks :slight_smile:

2 Likes