I am now working on this. I think I've found where Dynamo runs the code, but it does not work when I replicate it. The portion I found in the Git is in the IronPythonEvaluator.cs:
public static object EvaluateIronPythonScript(
string code,
IList bindingNames,
[ArbitraryDimensionArrayImport] IList bindingValues)
{
// TODO - it would be nice if users could modify a preference
// setting enabling the ability to load additional paths
// Container for paths that will be imported in the PythonEngine
List<string> paths = new List<string>();
// Attempt to get the Standard Python Library
string stdLib = pythonStandardLibPath();
if (code != prev_code)
{
ScriptEngine PythonEngine = Python.CreateEngine();
if (!string.IsNullOrEmpty(stdLib))
{
code = "import sys" + System.Environment.NewLine + code;
paths = PythonEngine.GetSearchPaths().ToList();
paths.Add(stdLib);
}
// If any paths were successfully retrieved, append them
if (paths.Count > 0)
{
PythonEngine.SetSearchPaths(paths);
}
ScriptSource script = PythonEngine.CreateScriptSourceFromString(code);
script.Compile();
prev_script = script;
prev_code = code;
}
ScriptEngine engine = prev_script.Engine;
ScriptScope scope = engine.CreateScope();
int amt = Math.Min(bindingNames.Count, bindingValues.Count);
for (int i = 0; i < amt; i++)
{
scope.SetVariable((string)bindingNames[i], InputMarshaler.Marshal(bindingValues[i]));
}
try
{
OnEvaluationBegin(engine, scope, code, bindingValues);
prev_script.Execute(scope);
}
catch (Exception e)
{
OnEvaluationEnd(false, engine, scope, code, bindingValues);
var eo = engine.GetService<ExceptionOperations>();
string error = eo.FormatException(e);
throw new Exception(error);
}
OnEvaluationEnd(true, engine, scope, code, bindingValues);
var result = scope.ContainsVariable("OUT") ? scope.GetVariable("OUT") : null;
return OutputMarshaler.Marshal(result);
}
I'm hitting a roadblock, though. All namespaces loaded, no compile errors or warnings. I've simplified mine to:
[Transaction(TransactionMode.Manual)]
public class ModelCleanup : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
string code = @"|filepath|.dyn";
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
ScriptSource script = engine.CreateScriptSourceFromFile(code);
script.Compile();
script.Execute(scope);
return Result.Succeeded;
}
I end up with the following error:
![Capture.JPG Capture.JPG](https://forums.autodesk.com/t5/image/serverpage/image-id/642418i71829C115AFE0304/image-size/large?v=v2&px=999)
Does anyone have any insight? I am already using Dyno Browser, though I'm not 100% happy with it. I cannot decompile his DLL's to a far-enough degree to see how he's doing it, but he proves it's possible. I do not like Dynamo Player at all. I'm simply looking for a way to have C# call some .dyn files so I don't have to waste time rewriting them all.