Example #1
0
        void Analyze(string path)
        {
            var data = File.ReadAllText(path);
            var name = Path.GetFileNameWithoutExtension(path);

            using (PyScope scope = Py.CreateScope())
            {
                scope.Import("sys");
                scope.Import("os");
                var dir = Path.GetDirectoryName(path);
                var com = $"sys.path.append({dir})";
                scope.Exec("p = os.getcwd()");
                var p = scope.Get("p");
                //scope.Exec(com);
                scope.Exec(data);
                var v       = scope.Get(name);
                var mapping = v.GetAttr("mapping");
                var items   = mapping.GetAttr("keys");

                foreach (var item in mapping)
                {
                    var command = item.ToPython();
                    var target  = mapping.GetItem(command);
                    commands[command.ToString()] = target;
                }
            }
        }
Example #2
0
        public void RunFunction(string sFuncName, params string[] args)
        {
            using (Py.GIL())
                using (PyScope scope = Py.CreateScope())
                {
                    Scope = scope;

                    foreach (var kvp in Locals)
                    {
                        var      pyName = kvp.Key;
                        PyObject pyObj  = kvp.Value.ToPython();

                        Scope.Set(pyName, pyObj);
                    }

                    Scope.Exec(Script);

                    var functionBuilder = new StringBuilder();
                    functionBuilder.Append(sFuncName);

                    functionBuilder.Append("(");
                    foreach (var arg in args)
                    {
                        functionBuilder.Append(arg);
                    }
                    functionBuilder.Append(")");

                    Scope.Exec(functionBuilder.ToString());
                }
        }
Example #3
0
        static void Main(string[] args)
        {
            string preamble =
                "import clr\n" +
                "clr.AddReference('MyModule')\n" +
                "from MyModule import MyClass\n" +
                "import sys\n" +
                "print(sys.version)";

            string callWithFloatArgs = "result = MyClass.Add(1.0, 2.0)";
            string callWithIntArgs   = "result = MyClass.Add(1, 2)";
            string callWithMixedArgs = "result = MyClass.Add(1.0, 2)";

            PythonEngine.Initialize();
            using (Py.GIL())
            {
                using (PyScope scope = Py.CreateScope())
                {
                    scope.Exec(preamble);

                    scope.Exec(callWithFloatArgs);
                    System.Console.WriteLine(scope.Get("result")); // prints "3.0"

                    scope.Exec(callWithIntArgs);
                    System.Console.WriteLine(scope.Get("result")); // prints "3"

                    scope.Exec(callWithMixedArgs);                 // Python.Runtime.PythonException: 'TypeError : No method matches given arguments for Add'
                    System.Console.WriteLine(scope.Get("result"));
                }
            }
        }
        public bool Execute(string content, string directory)
        {
            if (!CanExecute)
            {
                return(false);
            }

            int ProfileID = Utilities.BeginProfileSession();

            Executing = true;
            bool result = true;

            try
            {
                using (Py.GIL())
                {
                    Scope.Exec("sys.settrace(isExecutionThreadAlive)\n");

                    if (!string.IsNullOrEmpty(directory))
                    {
                        Scope.Exec("sys.path.append(r\"" + directory + "\")\n");
                    }

                    Scope.Exec(content);
                }

                IOManager?.GetOutput()?.WriteLine("Execution Completed. Runtime of {0:N2}s", Utilities.GetTimeIntervalSeconds(ProfileID));
            }
            catch (Exception ex)
            {
                result = false;

                Type exType = ex.GetType();
                if (exType.Namespace.Contains(PythonNETNamespace))
                {
                    IOManager?.GetError()?.WriteLine(ex.Message);
                    IOManager?.GetError()?.WriteLine(ex.StackTrace);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                }
            }

            IOManager?.GetOutput()?.Flush();
            Utilities.EndProfileSession(ProfileID);
            Executing = false;

            return(result);
        }
Example #5
0
        public void Run()
        {
            Person person = new Person("Bob", "Davies");

            using (PyScope scope = Py.CreateScope())
            {
                PyObject pyPerson = person.ToPython();
                scope.Set("person", pyPerson);
                string code = "Fullname = person.FirstName + ' ' + person.LastName";
                scope.Exec(code);
                string printit = "print (person.FirstName)";
                scope.Exec(printit);
            }
        }
Example #6
0
 public void TestExec()
 {
     using (Py.GIL())
     {
         ps.Set("bb", 100); //declare a global variable
         ps.Set("cc", 10);  //declare a local variable
         ps.Exec("aa = bb + cc + 3");
         var result = ps.Get <int>("aa");
         Assert.AreEqual(113, result);
     }
 }
Example #7
0
        public void TestImportScopeFunction()
        {
            using (Py.GIL())
            {
                ps.Set("bb", 100);
                ps.Set("cc", 10);
                ps.Exec(
                    "def func1():\n" +
                    "    return cc + bb\n");

                using (PyScope scope = ps.NewScope())
                {
                    //'func1' is imported from the origion scope
                    scope.Exec(
                        "def func2():\n" +
                        "    return func1() - cc - bb\n");
                    dynamic func2 = scope.Get("func2");

                    var result1 = func2().As <int>();
                    Assert.AreEqual(0, result1);

                    scope.Set("cc", 20);//it has no effect on the globals of 'func1'
                    var result2 = func2().As <int>();
                    Assert.AreEqual(-10, result2);
                    scope.Set("cc", 10); //rollback

                    ps.Set("cc", 20);
                    var result3 = func2().As <int>();
                    Assert.AreEqual(10, result3);
                    ps.Set("cc", 10); //rollback
                }
            }
        }
Example #8
0
        /// <summary>
        /// Migrates Python 2 code to Python 3 using Pythons 2to3 library.
        /// </summary>
        /// <param name="code">Python 2 code that needs to be migrated</param>
        /// <returns></returns>
        internal static string MigrateCode(string code)
        {
            Installer.SetupPython().Wait();

            if (!PythonEngine.IsInitialized)
            {
                PythonEngine.Initialize();
                PythonEngine.BeginAllowThreads();
            }

            IntPtr gs = PythonEngine.AcquireLock();

            try
            {
                using (Py.GIL())
                {
                    using (PyScope scope = Py.CreateScope())
                    {
                        scope.Set(INPUT_NAME, code.ToPython());
                        scope.Exec(GetPythonMigrationScript());

                        var result = scope.Contains(RETURN_NAME) ? scope.Get(RETURN_NAME) : null;
                        return(result.ToString());
                    }
                }
            }

            finally
            {
                PythonEngine.ReleaseLock(gs);
            }
        }
Example #9
0
        /// <summary>
        /// Runs Python code in the Unity process.
        /// </summary>
        /// <param name="pythonCodeToExecute">The code to execute.</param>
        /// <param name="scopeName">Value to write to Python special variable `__name__`</param>
        public static void RunString(string pythonCodeToExecute, string scopeName = null)
        {
            if (string.IsNullOrEmpty(pythonCodeToExecute))
            {
                return;
            }

            EnsureInitialized();
            using (Py.GIL())
            {
                // Clean up the string.
                dynamic inspect = PythonEngine.ImportModule("inspect");
                string  code    = inspect.cleandoc(pythonCodeToExecute);

                if (string.IsNullOrEmpty(scopeName))
                {
                    PythonEngine.Exec(code);
                }
                else
                {
                    using (PyScope scope = Py.CreateScope())
                    {
                        scope.Set("__name__", scopeName);
                        scope.Exec(code);
                    }
                }
            }
        }
Example #10
0
        /// <summary>
        /// Runs a Python script in the Unity process.
        /// </summary>
        /// <param name="pythonFileToExecute">The script to execute.</param>
        /// <param name="scopeName">Value to write to Python special variable `__name__`</param>
        public static void RunFile(string pythonFileToExecute, string scopeName = null)
        {
            EnsureInitialized();
            if (null == pythonFileToExecute)
            {
                throw new System.ArgumentNullException("pythonFileToExecute", "Invalid (null) file path");
            }

            // Ensure we are getting the full path.
            pythonFileToExecute = Path.GetFullPath(pythonFileToExecute);

            // Forward slashes please
            pythonFileToExecute = pythonFileToExecute.Replace("\\", "/");
            if (!File.Exists(pythonFileToExecute))
            {
                throw new System.IO.FileNotFoundException("No Python file found at " + pythonFileToExecute, pythonFileToExecute);
            }

            using (Py.GIL())
            {
                if (string.IsNullOrEmpty(scopeName))
                {
                    PythonEngine.Exec(string.Format("exec(open('{0}').read())", pythonFileToExecute));
                }
                else
                {
                    using (PyScope scope = Py.CreateScope())
                    {
                        scope.Set("__name__", scopeName);
                        scope.Set("__file__", pythonFileToExecute);
                        scope.Exec(string.Format("exec(open('{0}').read())", pythonFileToExecute));
                    }
                }
            }
        }
Example #11
0
 public void Run(string cmd)
 {
     using (PyScope scope = Py.CreateScope())
     {
         scope.Exec(cmd);
     }
 }
Example #12
0
        public int LoadProgram(string path)
        {
            string content = File.ReadAllText(path);

            m_scope.Exec(content);
            return(0);
        }
Example #13
0
        /// <summary>
        /// Processes additional bindings that are not actual inputs.
        /// Currently, only the node name is received in this way.
        /// </summary>
        /// <param name="scope">Python scope where execution will occur</param>
        /// <param name="bindingNames">List of binding names received for evaluation</param>
        /// <param name="bindingValues">List of binding values received for evaluation</param>
        private static void ProcessAdditionalBindings(PyScope scope, IList bindingNames, IList bindingValues)
        {
            const string NodeNameInput = "Name";
            string       nodeName;

            if (bindingNames.Count == 0 || !bindingNames[0].Equals(NodeNameInput))
            {
                // Defensive code to fallback in case the additional binding is not there, like
                // when the evaluator is called directly in unit tests.
                nodeName = "USER";
            }
            else
            {
                bindingNames.RemoveAt(0);
                nodeName = (string)bindingValues[0];
                bindingValues.RemoveAt(0);
            }

            // Session is null when running unit tests.
            if (ExecutionEvents.ActiveSession != null)
            {
                dynamic         logger      = ExecutionEvents.ActiveSession.GetParameterValue(ParameterKeys.Logger);
                Action <string> logFunction = msg => logger.Log($"{nodeName}: {msg}", LogLevel.ConsoleOnly);
                scope.Set(DynamoPrintFuncName, logFunction.ToPython());
                scope.Exec(RedirectPrint());
            }
        }
Example #14
0
        /// <summary>
        ///     Executes a Python script with custom variable names. Script may be a string
        ///     read from a file, for example. Pass a list of names (matching the variable
        ///     names in the script) to bindingNames and pass a corresponding list of values
        ///     to bindingValues.
        /// </summary>
        /// <param name="code">Python script as a string.</param>
        /// <param name="bindingNames">Names of values referenced in Python script.</param>
        /// <param name="bindingValues">Values referenced in Python script.</param>
        public static object EvaluatePythonScript(
            string code,
            IList bindingNames,
            [ArbitraryDimensionArrayImport] IList bindingValues)
        {
            if (code != prev_code)
            {
                Python.Included.Installer.SetupPython().Wait();

                if (!PythonEngine.IsInitialized)
                {
                    PythonEngine.Initialize();
                    PythonEngine.BeginAllowThreads();
                }

                IntPtr gs = PythonEngine.AcquireLock();
                try
                {
                    using (Py.GIL())
                    {
                        using (PyScope scope = Py.CreateScope())
                        {
                            int amt = Math.Min(bindingNames.Count, bindingValues.Count);

                            for (int i = 0; i < amt; i++)
                            {
                                scope.Set((string)bindingNames[i], InputMarshaler.Marshal(bindingValues[i]).ToPython());
                            }

                            try
                            {
                                OnEvaluationBegin(scope, code, bindingValues);
                                scope.Exec(code);
                                OnEvaluationEnd(false, scope, code, bindingValues);

                                var result = scope.Contains("OUT") ? scope.Get("OUT") : null;

                                return(OutputMarshaler.Marshal(result));
                            }
                            catch (Exception e)
                            {
                                OnEvaluationEnd(false, scope, code, bindingValues);
                                throw e;
                            }
                        }
                    }
                }
                catch (PythonException pe)
                {
                    throw pe;
                }
                finally
                {
                    PythonEngine.ReleaseLock(gs);
                }
            }
            return(null);
        }
Example #15
0
 public CPythonRuntime()
 {
     PythonEngine.PythonPath += ";" + Path.GetDirectoryName(typeof(CPythonRuntime).Assembly.Location);
     Log.WriteLine("PythonPath = {0}", PythonEngine.PythonPath);
     m_GIL   = Py.GIL();
     m_scope = Py.CreateScope();
     //TODO make sure the GraphEngine wheel is installed
     m_scope.Exec(@"import GraphEngine");
 }
Example #16
0
 public void Run(OpenCvSharp.Mat src, string cmd)
 {
     using (PyScope scope = Py.CreateScope())
     {
         PyObject pySrc = src.ToPython();
         scope.Set("src", pySrc);
         scope.Exec(cmd);
     }
 }
Example #17
0
 public static void ExecResource(this PyScope scope, string name)
 {
     using (Py.GIL()) {
         var asm = Assembly.GetCallingAssembly();
         using var stream       = asm.GetManifestResourceStream(asm.GetManifestResourceNames().Single(fn => fn.EndsWith(name, StringComparison.OrdinalIgnoreCase)));
         using var streamReader = new StreamReader(stream, Encoding.UTF8);
         scope.Exec(streamReader.ReadToEnd());
     }
 }
Example #18
0
        /// <summary>
        /// Migrates Python 2 code to Python 3 using Pythons 2to3 library.
        /// </summary>
        /// <param name="code">Python 2 code that needs to be migrated</param>
        /// <returns></returns>
        internal static string MigrateCode(string code)
        {
            InstallPython();

            if (!PythonEngine.IsInitialized)
            {
                PythonEngine.Initialize();
                PythonEngine.BeginAllowThreads();
            }

            IntPtr gs = PythonEngine.AcquireLock();

            try
            {
                using (Py.GIL())
                {
                    string output;
                    var    asm = Assembly.GetExecutingAssembly();

                    using (PyScope scope = Py.CreateScope())
                    {
                        scope.Set(INPUT_NAME, code.ToPython());

                        var path = Path.GetDirectoryName(asm.Location);

                        scope.Set(PATH_NAME, path.ToPython());
                        scope.Exec(Get2To3MigrationScript(asm));

                        output = scope.Contains(RETURN_NAME) ? scope.Get(RETURN_NAME).ToString() : string.Empty;
                    }

                    // If the code contains tabs, normalize the whitespaces. This is a Python 3 requirement
                    // that's not addressed by 2to3.
                    if (output.Contains("\t"))
                    {
                        using (PyScope scope = Py.CreateScope())
                        {
                            scope.Set(INPUT_NAME, output.ToPython());
                            scope.Exec(GetReindentationScript(asm));
                            output = scope.Contains(RETURN_NAME) ? scope.Get(RETURN_NAME).ToString() : string.Empty;
                        }
                    }

                    return(output);
                }
            }

            finally
            {
                PythonEngine.ReleaseLock(gs);
            }
        }
Example #19
0
        public static void LoadScript(string name, string file)
        {
            PyScope scriptScope = Py.CreateScope();

            scriptScope.Exec(File.ReadAllText(Path.Combine("PyScripts", file)));
            if (ScriptScopes.ContainsKey(name))
            {
                ScriptScopes[name].Dispose();
                ScriptScopes.Remove(name);
            }
            ScriptScopes.Add(name, scriptScope);
            scriptScope.Set("debuglog", (Action <string>)Debug.Log);
            scriptScope.Set("debugmsgbox", (Action <string>)Debug.MsgBox);
        }
Example #20
0
        private PyScope CreateUniquePythonScope()
        {
            PyScopeManager.Global.TryGet(uniquePythonScopeName, out PyScope Scope);

            if (Scope == null)
            {
                Scope = Py.CreateScope(uniquePythonScopeName);
                Scope.Exec(@"
import clr
import sys
");
            }

            return(Scope);
        }
Example #21
0
        public CPythonRuntime()
        {
            //string path = PythonEngine.PythonPath;
            //if (path == "") path = AssemblyUtility.MyAssemblyPath;
            //else path = path + ";" + AssemblyUtility.MyAssemblyPath;
            //PythonEngine.PythonPath = path;

            PythonEngine.PythonPath += ";" + Path.GetDirectoryName(typeof(CPythonRuntime).Assembly.Location);
            Log.WriteLine("PythonPath = {0}", PythonEngine.PythonPath);
            m_GIL   = Py.GIL();
            m_scope = Py.CreateScope();

            m_scope.Exec(@"
import GraphEngine as ge
ge.Init()
");
        }
Example #22
0
        public void Run()
        {
            using (Py.GIL())
                using (PyScope scope = Py.CreateScope())
                {
                    Scope = scope;

                    foreach (var kvp in Locals)
                    {
                        var      pyName = kvp.Key;
                        PyObject pyObj  = kvp.Value.ToPython();

                        Scope.Set(pyName, pyObj);
                    }

                    Scope.Exec(Script);
                }
        }
Example #23
0
        public Sudoku Solve(Sudoku s)
        {
            using (Py.GIL())
            {
                using (PyScope scope = Py.CreateScope())
                {
                    // convert the sudoku object to a PyObject
                    PyObject pySudoku = s.ToPython();

                    // create a Python variable "sudoku"
                    scope.Set("sudoku", pySudoku);

                    // the sudoku object may now be used in Pythonn
                    string code = "fullName = person.FirstName + ' ' + person.LastName";
                    scope.Exec(code);
                    s = scope.Get <Sudoku>("sudoku");
                }


                return(s);
                // PythonEngine.Exec("doStuff()");
            }
        }
Example #24
0
        /// <summary>
        ///     Executes a Python script with custom variable names. Script may be a string
        ///     read from a file, for example. Pass a list of names (matching the variable
        ///     names in the script) to bindingNames and pass a corresponding list of values
        ///     to bindingValues.
        /// </summary>
        /// <param name="code">Python script as a string.</param>
        /// <param name="bindingNames">Names of values referenced in Python script.</param>
        /// <param name="bindingValues">Values referenced in Python script.</param>
        public static object EvaluatePythonScript(
            string code,
            IList bindingNames,
            [ArbitraryDimensionArrayImport] IList bindingValues)
        {
            var evaluationSuccess = true;

            if (code == null)
            {
                return(null);
            }

            InstallPython();

            if (!PythonEngine.IsInitialized)
            {
                PythonEngine.Initialize();
                PythonEngine.BeginAllowThreads();
            }

            IntPtr gs = PythonEngine.AcquireLock();

            try
            {
                using (Py.GIL())
                {
                    if (globalScope == null)
                    {
                        globalScope = CreateGlobalScope();
                    }

                    using (PyScope scope = Py.CreateScope())
                    {
                        // Reset the 'sys.path' value to the default python paths on node evaluation.
                        var pythonNodeSetupCode = "import sys" + Environment.NewLine + "sys.path = sys.path[0:3]";
                        scope.Exec(pythonNodeSetupCode);

                        ProcessAdditionalBindings(scope, bindingNames, bindingValues);

                        int amt = Math.Min(bindingNames.Count, bindingValues.Count);

                        for (int i = 0; i < amt; i++)
                        {
                            scope.Set((string)bindingNames[i], InputMarshaler.Marshal(bindingValues[i]).ToPython());
                        }

                        try
                        {
                            OnEvaluationBegin(scope, code, bindingValues);
                            scope.Exec(code);
                            var result = scope.Contains("OUT") ? scope.Get("OUT") : null;

                            return(OutputMarshaler.Marshal(result));
                        }
                        catch (Exception e)
                        {
                            evaluationSuccess = false;
                            var traceBack = GetTraceBack(e);
                            if (!string.IsNullOrEmpty(traceBack))
                            {
                                // Throw a new error including trace back info added to the message
                                throw new InvalidOperationException($"{e.Message} {traceBack}", e);
                            }
                            else
                            {
                                throw e;
                            }
                        }
                        finally
                        {
                            OnEvaluationEnd(evaluationSuccess, scope, code, bindingValues);
                        }
                    }
                }
            }
            finally
            {
                PythonEngine.ReleaseLock(gs);
            }
        }
Example #25
0
        public void TestPythonInterop()
        {
            var jsonSerializerOptions = new JsonSerializerOptions
            {
                WriteIndented = true,
                Converters    =
                {
                    new JsonStringEnumConverter(),
                    new SimpleHypergridJsonConverter(),
                    new DimensionJsonConverter(),
                },
            };

            string csContinuousJsonString      = JsonSerializer.Serialize(continuous, jsonSerializerOptions);
            string csDiscreteJsonString        = JsonSerializer.Serialize(discrete, jsonSerializerOptions);
            string csOrdinalJsonString         = JsonSerializer.Serialize(ordinal, jsonSerializerOptions);
            string csCategoricalJsonString     = JsonSerializer.Serialize(categorical, jsonSerializerOptions);
            string csSimpleHypergridJsonString = JsonSerializer.Serialize(allKindsOfDimensions, jsonSerializerOptions);

            using (Py.GIL())
            {
                using PyScope pythonScope = Py.CreateScope();

                pythonScope.Set("cs_continuous_dimension_json_string", csContinuousJsonString);
                pythonScope.Set("cs_discrete_dimension_json_string", csDiscreteJsonString);
                pythonScope.Set("cs_ordinal_dimension_json_string", csOrdinalJsonString);
                pythonScope.Set("cs_categorical_dimension_json_string", csCategoricalJsonString);
                pythonScope.Set("cs_simple_hypergrid_json_string", csSimpleHypergridJsonString);

                pythonScope.Exec(PythonScriptsAndJsons.CreateDimensionsAndSpacesScript);
                pythonScope.Exec(PythonScriptsAndJsons.DeserializeDimensionsScript);

                bool   successfullyDeserializedDimensions = pythonScope.Get("success").As <bool>();
                string exceptionMessage = string.Empty;
                if (!successfullyDeserializedDimensions)
                {
                    exceptionMessage = pythonScope.Get("exception_message").As <string>();
                }

                Assert.True(successfullyDeserializedDimensions, exceptionMessage);

                pythonScope.Exec(PythonScriptsAndJsons.DeserializeSimpleHypergridScript);

                bool successfullyDeserializedSimpleHypergrid = pythonScope.Get("success").As <bool>();
                if (!successfullyDeserializedSimpleHypergrid)
                {
                    exceptionMessage = pythonScope.Get("exception_message").As <string>();
                }

                Assert.True(successfullyDeserializedSimpleHypergrid, exceptionMessage);

                string          pySimpleHypergridJsonString           = pythonScope.Get("py_simple_hypergrid_json_string").As <string>();
                SimpleHypergrid simpleHypergridDeserializedFromPython = JsonSerializer.Deserialize <SimpleHypergrid>(pySimpleHypergridJsonString, jsonSerializerOptions);

                Assert.True(simpleHypergridDeserializedFromPython.Name == "all_kinds_of_dimensions");
                Assert.True(simpleHypergridDeserializedFromPython.Dimensions.Count == 4);

                string reserializedHypergrid = JsonSerializer.Serialize(simpleHypergridDeserializedFromPython, jsonSerializerOptions);

                pythonScope.Set("cs_reserialized_hypergrid_json_string", reserializedHypergrid);
                pythonScope.Exec(PythonScriptsAndJsons.ValidateReserializedHypergridScript);

                bool successfullyValidatedReserializedHypergrid = pythonScope.Get("success").As <bool>();
                if (!successfullyValidatedReserializedHypergrid)
                {
                    exceptionMessage = pythonScope.Get("exception_message").As <string>();
                }

                Assert.True(successfullyValidatedReserializedHypergrid, exceptionMessage);
            }

            string currentDirectory = Directory.GetCurrentDirectory();

            Console.WriteLine($"Current directory {currentDirectory}");
        }
Example #26
0
        private List <byte[]> CreateNSBTXes(SaveForm SF)
        {
            List <byte[]> NSBTXes = new List <byte[]>();

            using (Py.GIL())
            {
                dynamic TexturePy = Python.Runtime.Py.Import("ndspy.texture");

                using (PyScope scope = Py.CreateScope())
                {
                    scope.Exec("import ndspy.texture");

                    for (int j = 0; j < 3; j++)
                    {
                        scope.Exec("nsbtx = ndspy.texture.NSBTX()");

                        for (int i = 0 + (255 * j); i < 255 + (255 * j); i++)
                        {
                            SF.SetMessage("Encoding Tile " + (i + 1) + " (Format: " + TextureFormats[i] + ")");

                            Console.WriteLine("Tex " + i);
                            Bitmap   TileBitmap = new Bitmap(16, 16);
                            Graphics g          = Graphics.FromImage(TileBitmap);
                            g.DrawImage(this.MainPNG, new Rectangle(new Point(0, 0), new Size(16, 16)), Helpers.GetRectangleForTileID(i), GraphicsUnit.Pixel);

                            EncodedImage Enc = null;

                            if (TextureFormats[i] == 7)
                            {
                                Enc = Images.EncodeImage_RGB555(TileBitmap);
                            }
                            else if (TextureFormats[i] == 5)
                            {
                                Enc = Images.Encode_Tex4x4(TileBitmap, 16);
                            }
                            else
                            {
                                PalettedImage p = Images.CreatePalettedImage(TileBitmap, this.TextureFormats[i]);
                                Enc = Images.EncodeImage(p, TextureFormats[i]);
                            }

                            string  Name = "Tile " + i.ToString();
                            dynamic Tex  = TexturePy.Texture.fromFlags(0, 0, true, true, false, false, TileBitmap.Width, TileBitmap.Height,
                                                                       this.TextureFormats[i], false, 0, 0x80010040, Enc.Texture, Enc.Texture2);
                            dynamic Pal = TexturePy.Palette.fromColors(0, 0, 0, Enc.Palette);

                            scope.Set("Name", Name.ToPython());
                            scope.Set("Tex", Tex);
                            scope.Set("Pal", Pal);
                            scope.Exec("nsbtx.textures.append((Name,Tex))");
                            scope.Exec("nsbtx.palettes.append((Name,Pal))");
                        }

                        scope.Exec("nsbtxf = nsbtx.save()");
                        NSBTXes.Add(scope.Get <byte[]>("nsbtxf"));
                    }
                }
            }

            return(NSBTXes);
        }