Skip to content

Commit 3b13781

Browse files
authored
subscribe to dynamo reset event (#4)
1 parent c459c01 commit 3b13781

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

DSPythonNet3/DSPythonNet3Evaluator.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private static readonly Lazy<DSPythonNet3Evaluator>
168168
static DSPythonNet3Evaluator()
169169
{
170170
InitializeEncoders();
171+
SubscribeToDynamoResetEvent();
171172

172173
var dynamoRevitAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name == "DynamoRevitDS");
173174

@@ -177,14 +178,38 @@ static DSPythonNet3Evaluator()
177178
}
178179
}
179180

181+
/// <summary>
182+
/// Subscribes our handler to DynamoModel.RequestPythonReset (internal static event) using reflection.
183+
/// </summary>
184+
private static void SubscribeToDynamoResetEvent()
185+
{
186+
try
187+
{
188+
var dmType = typeof(Dynamo.Models.DynamoModel);
189+
190+
// internal static event Action<string> RequestPythonReset;
191+
var evt = dmType.GetEvent("RequestPythonReset",
192+
BindingFlags.Static | BindingFlags.NonPublic);
193+
if (evt == null) return;
194+
195+
// get the non-public "add" method and hook our Action<string>
196+
var addMethod = evt.GetAddMethod(true);
197+
if (addMethod == null) return;
198+
199+
Action<string> handler = RequestPythonResetHandler;
200+
addMethod.Invoke(null, new object[] { handler });
201+
}
202+
catch { }
203+
}
204+
180205
public override string Name => "PythonNet3";
181206

182207
internal static void RequestPythonResetHandler(string pythonEngine)
183208
{
184209
//check if python engine request is for this engine, and engine is currently started
185210
if (PythonEngine.IsInitialized && pythonEngine == Instance.Name)
186211
{
187-
DynamoLogger?.Log("attempting reload of cpython3 modules", LogLevel.Console);
212+
DynamoLogger?.Log("attempting reload of pythonnet3 modules", LogLevel.Console);
188213
using (Py.GIL())
189214
{
190215
//the following is inspired by:
@@ -232,7 +257,7 @@ def should_reload(module):
232257
Analytics.TrackEvent(
233258
Dynamo.Logging.Actions.Start,
234259
Dynamo.Logging.Categories.PythonOperations,
235-
"CPythonReset");
260+
"PythonNet3Reset");
236261
}
237262
}
238263

0 commit comments

Comments
 (0)