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

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!

post your script and we can help you fix it :wink:
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.

1 Like

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:

var pInfo = new ProcessStartInfo("python.exe");
pInfo.Arguments = "pythonFile.py";
pInfo.CreateNoWindow = true;
pInfo.UseShellExecute = false;
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;

var process = Process.Start(pInfo);

process.WaitForExit();

var standardOutput = process.StandardOutput.ReadToEnd();
var errorOutput = process.StandardError.ReadToEnd();
  
var outputBuilder = new StringBuilder(standardOutput);
if (!string.IsNullOrEmpty(errorOutput))
    outputBuilder.AppendLine(errorOutput);

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()

I actually do need this at runtime. Here is the code:

import sys
from gradio_client import Client
import shutil
import codecs

if len(sys.argv) != 3:
    print("Usage: python your_script.py character text_to_say")
    sys.exit(1)

character = sys.argv[1]
text_to_say = sys.argv[2]



client = Client("http://127.0.0.1:7860/")
result = client.predict(
    character,
    -10,
    text_to_say,
    "ru-RU-SvetlanaNeural-Female",
    5,
    "pm",
    0,
    0,
    fn_index=0
)
print(result)

new_location = "C:/Users/80nin/ai/Assets/generatedWAV/new_audio.wav"

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

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.

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.