Search code examples
pythonsubprocessnuke

Subprocess Popen function in Python


  import subprocess
  subprocess.Popen(['C:\Program Files\Nuke6.1v3\\Nuke6.1.exe', '-t', 'C:\Users\user\Desktop\\server.py'

I am currently using this to call server.py, which is setup in the following way

import sys
import os
import distutils
import shutil
import nuke

def imageScale(source,destination,scale):
    myRead = nuke.createNode("Read")
    #SET VALUES
    myRead["file"].setValue(source)
    myRead["selected"].setValue(True)
    #CREATE REFORMAT
    myReformat = nuke.createNode("Reformat")
    #SET VALUES
    myReformat["type"].setValue("scale")
    myReformat["scale"].setValue(scale)#.75)
    #SELECTION
    myRead["selected"].setValue(False)
    myReformat["selected"] .setValue(True)
    #CREATE WRITE
    myWrite = nuke.createNode("Write")

    #SET VALUES
    myWrite["file"].setValue(destination)
    myWrite["file_type"].setValue("png")
    myWrite["channels"].setValue("rgba")
    myWrite["name"].setValue("temp")
    nuke.execute("temp",1,1,1)

Would I be able to use subprocess.popen to pass in arguments to my imageScale function i.e. I pass in arguments source,destination,and scale directly from subprocess


Solution

  • Short answer is yes. You want to read arguments from sys.argv in your server.py.

    However, If you know you have a python function and know you will be calling it from python, I would have a look at

    import multiprocessing
    import server
    
    p = multiprocessing.Process(target=server.imageScale, args=(source,destination,scale))
    p.start()
    p.join()
    

    Apart from being more esthetic, you are free to do other stuff between start and join.

    EDIT: Actually, I am just assuming that Nuke will do the right thing with arguments. No guarantee that it will, so the second strategy is probably better for that reason too.