Refering to a relative path

Hi there,

I would like to externalise my python scripts from my dynamo script, so I can work on the scripts seperately (via git).
Doing this is not a problem, what is a problem however is that I want to refer to the python scripts relatively to the dynamo script.

Is there a way to extract the path of the currently opened dynamo script?

I tried two options that kind of work (but really don’t):

  1. I use the dynamo log files to extract the path. However, this requires the dynamo file to be saved before it can be run.
  2. By using the “os.getcwd()” command. This only works if the open revit file is in the same directory as the dynamo file.

I would like to have a command that just returns the current working directory of the cynamo script.

Kind regards,

try the Graph.FilePath node from the SpringsUI package:

6 Likes

Thanks for your reply!
This works, however, I would like to do it without custom nodes and packages.
The idea is that when other people open the dynamo file, it always works and they don’t need any packages installed.
Is the code used in SpringsUI.Graph.FilePath open-source?

It will be when I get the chance to clean it up a bit and post it on GitHub. However you won’t be able to use it from python, because you need a hook to Dynamo’s view model. I don’t think there’s a straight-forward enough way to do that from python :frowning:

Alright, thanks a lot anyway!
Stubborn as I am, I’ll still have a look, but you’re probably right :wink:

Thanks!

Hi Dimitar! I need this function a lot, but the SpringsUI.Graph.FilePath isn´t working for me. Seems nothing happens when i choose the node in the dynamo UI (the node doesn´t appear on the workspace). Is it some bug? Im using Dynamo 2.0.1. I know that is an older post. Thanks in advance!

Hi, I haven’t ported the package to 2.0 but you can find all the source code here:

I want to use this node. But as soon as I load it onto the canvas my Dynamo crashes. Do others have this too? Im using Revit 2023 and SpringsUI 132.1.1 .

Error message :

Method not found: 'System.String Dynamo.Graph.Nodes.PortModel.get_PortName()'.

   at SpringsUI.HomeSpacePath..ctor()
   at lambda_method(Closure )
   at Dynamo.Wpf.ViewModels.NodeSearchElementViewModel.OnClicked()
   at Dynamo.UI.Controls.InCanvasSearchControl.ExecuteSearchElement(ListBoxItem listBoxItem)
   at Dynamo.UI.Controls.InCanvasSearchControl.OnMouseLeftButtonUp(Object sender, MouseButtonEventArgs e)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.OnPreviewMouseUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.ContentElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

Thinking ahead about a backup solution : is there no other way to achieve this yet anno 2024 ?

Edit 1 : just noticed the reference links underneath Dimitar Venkovs solution. Will check those out
Edit 2 : this code worked :

import clr
clr.AddReference('DynamoRevitDS')
import Dynamo

dynamoRevit = Dynamo.Applications.DynamoRevit()
currentWorkspace = dynamoRevit.RevitDynamoModel.CurrentWorkspace
OUT = currentWorkspace.FileName

This might be a better solution, as it will work with Dynamo core as opposed to just Dynamo for Revit: dynamoPython/dynamoAPI/dynamoAPICurrentWorkspaceInfo.py at master · Amoursol/dynamoPython · GitHub

Should be completely functional in all currently supported Dynamo versions as well (2.10 to 3.4).

1 Like