Writing dynamo node as python code

Hi,

I was trying to write nodes as a Python Code to learn how to write.

To create these nodes as a Python code I wrote these lines below. Could you help me write it?
Please excuse my major errors.

image

# Load the Python Standard and DesignScript Libraries
import sys
import clr
import Autodesk
clr.AddReference('ProtoGeometry')
clr.AddReference('RevitNodes')
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.DesignScript.Geometry import *
import Revit
from Revit import *

# The inputs to this node will be stored as a list in the IN variables.
selectModelElement = IN[0]
observerViewHeight= IN[1]

# Place your code below this line

def observerLocation (selectModelElement, observerViewHeight):
   Geom = Autodesk.Revit.DB.FamilyInstance.Location(selectModelElement)
   Direct= Vector.ZAxis
   Distance = observerviewheight
   Point = Geometry.Translate(Geom,Direct,Distance)
   return Point
              

# Assign your output to the OUT variable.
OUT = observerLocation(selectModelElement, observerViewHeight)

I understand that you’re just doing this to learn Python (awesome!), but it’s rarely worth using Dynamo nodes in Python (in my opinion). Dynamo nodes will automatically address list structure, which is incredibly helpful. Python allows you to specify the exact iteration you want to use across any number of inputs and data structures. There’s rarely a time when you benefit from mixing the two, save for a few very specific functions (in which case, just do them with nodes). If you want to learn Python, you’re better off learning it with the Revit API as that’s where the real advantage is.

Dynamo also has DesignScript, which is just the textual context you see in a code block. That can be a good option for something more direct than nodes but not as advanced as Python. The other benefit is that DesignScript will match the same node syntax in Python, so it’s a good way to learn syntax without dealing with Python issues at first.

4 Likes

Hi, here is a direction even if I agree with Mr. Nick and his comments

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitAPI")
import Autodesk 
from Autodesk.Revit.DB import Element
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

elt=UnwrapElement(IN[0]) #Because Element Revit
Point_Revit=elt.Location.Point #Class Element use From Autodesk.Revit.DB
Point_Dynamo=Point_Revit.ToPoint() #Revit geometry conversion

ht=IN[1]
v=Vector.ZAxis()
new_point=Point_Dynamo.Translate(v,ht)

OUT = Point_Dynamo,v,new_point

Sincerely
christian.stan

1 Like

You mixed in the API though. :wink:

2 Likes

Dynamo node are API calls… just a matter of how you speak it.

For what it’s worth I find Dynamo geometry and many custom packages (VASA is my jam of late) in Python quite useful.

2 Likes

Ok the Revit API. :laughing:

I agree there are times for custom packages (I’ve been looking at Topologic in Python myself) but, in a general sense, I don’t typically see the need for Core (and certainly not Revit) nodes in Python. Sometimes as a last resort for specific functions, but personally I think it’s more efficient to just separate the two in most cases.

1 Like

Thank you for your explanations and what you mention about iteration is a summary of my exact necessity and I am happy that you made it clear without even telling you.

The iteration struggle made me write these lines. Cause I was dealing with a situation in which I needed to calculate 30 different points of view calculations and each of them had 180 separate calculations. In the end, I need to calculate 5400 separate situations.

That is why I was trying to learn python versions. Thank you again for a clear explanation.

1 Like

I am grateful for your solution which enlightened me. Thank you very much! @christian.stan I am thankful for your additions @jacob.small.

1 Like

Everyone’s role is different, but I’ll take a bite.

Here’s 6 examples I had to touch in the last 24 hours.

  1. I want to fillet polycurves from a given list of radii.
  2. I want to find the shortest path from each point in a set of points to an existing curve network, and append the path to the network before checking the next point.
  3. I want to use Face.Loops (hidden in the library) to get the curve loops defining a face of a Dynamo topology.
  4. I want to assign values to objects without reusing the same value twice.
  5. I want to iterate until a condition is met.
  6. I want to work with a mesh.

And yes, all of those can be done with Design Script or C# via Zero Touch node. The beauty of Dynamo is that we can use ALL THE THINGS. The struggle of Dynamo is that we have to learn and support ALL THE THINGS.

4 Likes

I agree 100%. My response was mainly in the context of learning. “You have to know the rules in order to know when to break the rules.” I think it’s important to learn and understand each context on its own before you start mixing methods. It can get really confusing when you’re not familiar with the specifics of each method already.

2 Likes

it is fascinating and a huge convenience to create different solutions on the same program. Also, knowing experts could guide users like me when we are stuck on our problems is what I love about Dynamo. and yes, different approaches make me confused as seen in my first script above.

2 Likes