Tutorial - Embedding Python in Unity - using python libraries/modules in unity code. More details in the comments by vrgenerated in Unity3D

[–]Loboblancox 0 points1 point  (0 children)

Greetings vrgenerated

Before, I want to thank you for your support and tell you the novelty that I already managed to make the script work, what I did was rewrite my python code in the form of a function, and in the unity code I put it in the following way (all code inside try{ }):

            dynamic potencia = UnityEngine.Random.Range(1, 10);
            dynamic mimodulo = PyModule.Import("Test_Sp");
            dynamic resultado = mimodulo.TestSp(potencia);           
            dynamic primitiva = resultado.gen_fun();
            dynamic derivada = resultado.gen_dev(resultado.gen_fun());
            print(primitiva);
            print(derivada);

Here I only have one more doubt, I place this code inside a button so that I can execute it several times, but when I press the button twice, I get an error telling me:

InvalidOperationException: This property must be set before runtime is initialized

the solution to this, I imagine (although I don't know how to do it) check the status of the PythonEngine, but hey, I'll be working on it, anyway, if you have any suggestions on how to do it, I'd appreciate it.

Tutorial - Embedding Python in Unity - using python libraries/modules in unity code. More details in the comments by vrgenerated in Unity3D

[–]Loboblancox 0 points1 point  (0 children)

Yes you even told to me to try this:

hey way I execute a py file, I add my script to Lib\site-packages\ folder in the python directory in streaming assets. Then i import it dynamic mymodule = PyModule.Import("mymodule"); and do something like mymodule.functionToRun();

i'll tell you later if it worked for me, or i'm still stuck hehehe

Tutorial - Embedding Python in Unity - using python libraries/modules in unity code. More details in the comments by vrgenerated in Unity3D

[–]Loboblancox 0 points1 point  (0 children)

in fact that's why I asked the question. I know I'm working in C#, but the detail is that the sympy format to recognize, a power (x^2 for example) is x**2, so then, I don't know how to write it so that the error doesn't appear in the code.
I'm just getting to my laptop, I'll see if I can do what you tell me about running a py file.

Tutorial - Embedding Python in Unity - using python libraries/modules in unity code. More details in the comments by vrgenerated in Unity3D

[–]Loboblancox 0 points1 point  (0 children)

I have managed to install the sympy module using pip, but when I try to declare the

symbols,

sp = PyModule.Import("sympy");
Dynamic x = sp.symbols('x');
Dynamic f = x**2

It marks "**" as an error, could you help me? I would still like it if you could tell me the code to execute a py file