Esempio n. 1
0
 static void PrintHelloWorldFromPython()
 {
     PythonRunner.RunString(@"
             import UnityEngine;
             UnityEngine.Debug.Log('hello console')
             ");
 }
Esempio n. 2
0
        public void TestUndoRedirectStdout()
        {
            // open python console
            PythonConsoleWindow.ShowWindow();

            var prevContents = PythonConsoleWindow.s_window.m_outputContents;

            PythonConsoleWindow.s_window.m_outputContents = "";

            var msg       = "hello world";
            var pythonCmd = string.Format("print ('{0}')", msg);

            PythonRunner.RunString(pythonCmd);
            var output = PythonConsoleWindow.s_window.m_outputContents;

            Assert.That(output, Is.EqualTo(msg + "\n"));

            PythonRunner.UndoRedirectStdout();


            PythonConsoleWindow.s_window.m_outputContents = "";

            // stdout should no longer be redirected
            PythonRunner.RunString(pythonCmd);
            output = PythonConsoleWindow.s_window.m_outputContents;

            Assert.That(output, Is.Null.Or.Empty);

            PythonConsoleWindow.s_window.m_outputContents = prevContents;
            PythonConsoleWindow.s_window.Close();

            // redo stdout redirection
            PythonRunner.RedirectStdout();
        }
Esempio n. 3
0
 static void OnHierarchyChanged()
 {
     // This is the simplest way to call Python code.
     PythonRunner.RunString(@"
             import PySideExample
             PySideExample.update_camera_list()
             ");
 }
Esempio n. 4
0
        // Helper for TestProjectAssemblyReferencesAdded.
        // Tries to import System and run a command with PythonRunner.RunString().
        // Called once before and after domain reload.
        private void TestImportSystemHelper()
        {
            var importSystem = "import System;System.IO.File.Exists('test.txt')";

            PythonRunner.RunString(importSystem);

            // System has an extra m, so this should throw an exception
            var importSystemInvalid = "import Systemm;System.IO.File.Exists('test.txt')";

            Assert.Throws <PythonException>(() =>
            {
                PythonRunner.RunString(importSystemInvalid);
            });
        }
Esempio n. 5
0
        static void CreateOrReinitialize()
        {
            // You can manually add the sample directory to your sys.path in
            // the Python Settings under site-packages. Or you can do it
            // programmatically like so.
            string dir = __DIR__();

            PythonRunner.EnsureInitialized();
            using (Py.GIL())
            {
                dynamic sys = PythonEngine.ImportModule("sys");
                if ((int)sys.path.count(dir) == 0)
                {
                    sys.path.append(dir);
                }
            }

            // Now that we've set up the path correctly, we can import the
            // Python side of this example as a module:
            PythonRunner.RunString(@"
                    import PySideExample

                    PySideExample.create_or_reinitialize()
                    ");

            // We can't register events in Python directly, so register them
            // here in C#:
            EditorApplication.hierarchyChanged += OnHierarchyChanged;
            EditorApplication.update           += OnUpdate;

            //
            // A domain reload happens when you change C# code or when you
            // launch into play mode (unless you selected the option not to
            // reload then).
            //
            // When it happens, your C# state is entirely reinitialized. The
            // Python state, however, remains as it was.
            //
            // To store information about what happened in the previous domain,
            // Unity provides the SessionState. Alternately we could have
            // stored the data in a variable in Python.
            //
            SessionState.SetBool(kStateName, true);
        }
Esempio n. 6
0
        public void TestRunString()
        {
            // check with null and empty string
            PythonRunner.RunString(null);
            PythonRunner.RunString("");

            // Something valid
            string goName = "Bob";

            PythonRunner.RunString($"import UnityEngine;obj = UnityEngine.GameObject();obj.name = '{goName}'");
            var obj = GameObject.Find(goName);

            Assert.That(obj, Is.Not.Null);

            // Same code, with obvious error
            Assert.Throws <PythonException>(() =>
            {
                PythonRunner.RunString($"import UnityEngineobj = UnityEngine.GameObject();obj.name = '{goName}'");
            });

            // Testing scopeName parameter
            string scopeName = "__main__";

            UnityEngine.TestTools.LogAssert.Expect(LogType.Log, scopeName);
            PythonRunner.RunString("import UnityEngine; UnityEngine.Debug.Log(__name__)", scopeName);

            scopeName = "unity_python";
            UnityEngine.TestTools.LogAssert.Expect(LogType.Log, scopeName);
            PythonRunner.RunString("import UnityEngine; UnityEngine.Debug.Log(__name__)", scopeName);

            // No NameError with list comprehension when setting __name__
            scopeName = "__main__";
            UnityEngine.TestTools.LogAssert.Expect(LogType.Log, scopeName);
            PythonRunner.RunString("import UnityEngine;items=[1,2,3];[x for x in items if isinstance(x, UnityEngine.GameObject)];UnityEngine.Debug.Log(__name__)",
                                   scopeName);
        }