Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question looking for a way to send commands in an existing cmd process from a unity script

Discussion in 'Scripting' started by unity_ijgS4SPw1sa8Sg, Nov 10, 2023.

  1. unity_ijgS4SPw1sa8Sg

    unity_ijgS4SPw1sa8Sg

    Joined:
    Mar 26, 2023
    Posts:
    2
    basically i have this python script that converts text to speech and it only works if i run it through cmd using the command: python teest.py character "text_to_say" and it returns a wav audio file saying text_to_say and using characters voice to the folder in a unity project. I tried countless times to run a new cmd process using system.diagnostics in a unity script and execute commands there but it never worked and never returned any audio file. It has to be easier than that I just need to send a simple command right? If you guys know any way i could do this please let me know thank you in advance!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,263
    post your script and we can help you fix it ;)
    Process.Start is the way to go

    I assume you don‘t need this at runtime, right? Because for runtime you would have to bundle Python with your (desktop) app.
     
    Nad_B likes this.
  3. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Well it's time to debug. And in this case, you should read the output of your python program, maybe it's an error message:


    Code (CSharp):
    1. var pInfo = new ProcessStartInfo("python.exe");
    2. pInfo.Arguments = "pythonFile.py";
    3. pInfo.CreateNoWindow = true;
    4. pInfo.UseShellExecute = false;
    5. pInfo.RedirectStandardOutput = true;
    6. pInfo.RedirectStandardError = true;
    7.  
    8. var process = Process.Start(pInfo);
    9.  
    10. process.WaitForExit();
    11.  
    12. var standardOutput = process.StandardOutput.ReadToEnd();
    13. var errorOutput = process.StandardError.ReadToEnd();
    14.  
    15. var outputBuilder = new StringBuilder(standardOutput);
    16. if (!string.IsNullOrEmpty(errorOutput))
    17.     outputBuilder.AppendLine(errorOutput);
    18.  
    19. Debug.Log($"Python exited with code {process.ExitCode} output: {outputBuilder}");
    Note: you obviously can't do that in game, since your whole game will pause until the process ends because of the use of process.WaitForExit()
     
  4. unity_ijgS4SPw1sa8Sg

    unity_ijgS4SPw1sa8Sg

    Joined:
    Mar 26, 2023
    Posts:
    2
    I actually do need this at runtime. Here is the code:
    Code (CSharp):
    1. import sys
    2. from gradio_client import Client
    3. import shutil
    4. import codecs
    5.  
    6. if len(sys.argv) != 3:
    7.     print("Usage: python your_script.py character text_to_say")
    8.     sys.exit(1)
    9.  
    10. character = sys.argv[1]
    11. text_to_say = sys.argv[2]
    12.  
    13.  
    14.  
    15. client = Client("http://127.0.0.1:7860/")
    16. result = client.predict(
    17.     character,
    18.     -10,
    19.     text_to_say,
    20.     "ru-RU-SvetlanaNeural-Female",
    21.     5,
    22.     "pm",
    23.     0,
    24.     0,
    25.     fn_index=0
    26. )
    27. print(result)
    28.  
    29. new_location = "C:/Users/80nin/ai/Assets/generatedWAV/new_audio.wav"
    30.  
    31. shutil.copy(result[2], new_location)
    im kind of new to programming, ive been learning c# and it was my first time ever writing in python (i wrote this code using the gradio docs and chatgpt so maybe it has errors). It also immediately closes if i open it not by executing command with cmd and it doesnt save any files not sure if its normal
     
    Last edited: Nov 11, 2023
  5. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    So your problem is in the Python script, not C# part? In this case try asking help in some Python forums, not in a Unity forums.
     
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,661
    I agree this is way off-topic for Unity.

    Once the CMD process is already open, you cannot submit additional commands to it unless you own the CMD process's stdin and you're using CMD in an interactive mode. Then you essentially "type" into the process.