Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Commit 44da3be

Browse files
committed
Import all assemblies for Unity
All assemblies in the `UnityEngine` and `UnityEditor` namespaces are now imported automatically when using `UnityPython.CreateEngine`. This fixes problems where not all types were available to import inside of a Python script, such as Rigidbody.
1 parent 5bbf33d commit 44da3be

2 files changed

Lines changed: 41 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
88

9+
### Fixed
10+
* Fix problem where not all types in the `UnityEngine` namespace could be
11+
imported
12+
913

1014
## [0.3.1]
1115

Scripts/UnityPython.cs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,53 @@
11
using IronPython.Hosting;
22
using Microsoft.Scripting.Hosting;
3+
using System;
34
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Reflection;
47

5-
public sealed class UnityPython
8+
/// <summary>
9+
/// Convenience class for creating a Python engine integrated with Unity.
10+
///
11+
/// All scripts executed by the ScriptEngine created by this class will:
12+
/// * Be able to import any class in a `UnityEngine*` namespace
13+
/// * Be able to import any class in a `UnityEditor*` namespace, if the script
14+
/// is running in the UnityEditor
15+
/// </summary>
16+
public static class UnityPython
617
{
7-
public static ScriptEngine CreateEngine()
18+
public static ScriptEngine CreateEngine(IDictionary<string, object> options = null)
819
{
9-
return CreateEngine(null);
10-
}
20+
var engine = Python.CreateEngine(options);
1121

12-
public static ScriptEngine CreateEngine(IDictionary<string, object> options)
13-
{
14-
ScriptEngine engine;
15-
if (options == null)
22+
// Load assemblies for the `UnityEngine*` namespaces
23+
foreach (var assembly in GetAssembliesInNamespace("UnityEngine"))
1624
{
17-
engine = Python.CreateEngine();
25+
engine.Runtime.LoadAssembly(assembly);
1826
}
19-
else
20-
{
21-
engine = Python.CreateEngine(options);
22-
}
23-
24-
var unityEngine = typeof(UnityEngine.GameObject).Assembly;
25-
engine.Runtime.LoadAssembly(unityEngine);
2627

2728
#if UNITY_EDITOR
28-
var unityEditor = typeof(UnityEditor.Editor).Assembly;
29-
engine.Runtime.LoadAssembly(unityEditor);
29+
// Load assemblies for the `UnityEditor*` namespaces
30+
foreach (var assembly in GetAssembliesInNamespace("UnityEditor"))
31+
{
32+
engine.Runtime.LoadAssembly(assembly);
33+
}
3034
#endif
3135

3236
return engine;
3337
}
38+
39+
/// <summary>
40+
/// Get a list of all loaded assemblies in the current AppDomain for a
41+
/// namespace beginning with the specified string.
42+
/// </summary>
43+
/// <param name="prefix">The beginning of the namespace.</param>
44+
/// <returns>All matching assemblies.</returns>
45+
private static IEnumerable<Assembly> GetAssembliesInNamespace(string prefix)
46+
{
47+
return AppDomain.CurrentDomain.GetAssemblies()
48+
.SelectMany(t => t.GetTypes())
49+
.Where(t => t.Namespace != null && t.Namespace.StartsWith(prefix))
50+
.Select(t => t.Assembly)
51+
.Distinct();
52+
}
3453
}

0 commit comments

Comments
 (0)