python nuke deadline submit

https://github.com/aizwellenstan/python_nuke_deadline

```python
"""
This script will submit current file to deadline for render
"""

import os
import sys
import subprocess
import os
import json

# https://docs.thinkboxsoftware.com/products/deadline/10.1/1_User%20Manual/manual/manual-submission.html
def job_info(info_txt):
    """
    this function will collect scene file information and write a job file
    :return:
    """
    job_info_file = r'{}\job_info.job'.format(os.getenv('TEMP'))
    with open(job_info_file, 'w') as job_file:
        job_file.write(info_txt)
    return job_info_file

def plugin_info(info_txt):
    """
    this function will collect maya deadline information and write a job file
    """
    plugin_info_file = r'{}\plugin_info.job'.format(os.getenv('TEMP'))
    with open(plugin_info_file, 'w') as job_file:
        job_file.write(info_txt)
    return plugin_info_file

def submit_to_deadline(job_info_txt, plugin_info_txt):
    # Change deadline exe root
    deadline_cmd = r"C:\Program Files\Thinkbox\Deadline10\bin\deadlinecommand.exe"
    job_file = job_info(job_info_txt)
    info_file = plugin_info(plugin_info_txt)
    command = '{deadline_cmd} "{job_file}" "{info_file}"'.format(**vars())
    process = subprocess.Popen(command, stdout=subprocess.PIPE)
    lines_iterator = iter(process.stdout.readline, b"")
    #  Lets print the output log to see the Error / Success 
    for line in lines_iterator:
        print(line)
        sys.stdout.flush()
        try:
            with open(f'D:\nuke_deadline_debug.txt', 'w') as f:
                print(line, file=f)
        except:
            pass

def GetInfoTxt(filePath, frames, chunkSize=1, excuteNodes="REN_EXR", pool="nuke", priority=50, machineLimit=30, initialStatus="Active"):
    import socket
    machinename = socket.gethostname()
    import os

    job_info_txt = f"""
    Frames={frames}
    MachineLimit={machineLimit}
    ChunkSize={chunkSize}
    Group=nuke_13
    Name={os.path.basename(filePath)}
    OverrideTaskExtraInfoNames=False
    Plugin=CommandLine
    Pool={pool}
    Priority={priority}
    SecondaryPool=all
    UserName={os.getlogin()}
    MachineName={machinename}
    InitialStatus={initialStatus}
    """

    plugin_info_txt = f"""
    Arguments= -t -x -X {excuteNodes} -- {filePath} <STARTFRAME>,<ENDFRAME>,1
    Executable={os.environ['NUKE_DEADLINE_EXCUTABLE']}
    Shell=default
    ShellExecute=False
    SingleFramesOnly=False
    StartupDirectory=
    """

    return str(job_info_txt.strip()), str(plugin_info_txt)
```

### nuke menu
```menu.py
from deadline import DeadlineRenderSelected
nuke.menu('Nuke').addCommand('Deadline/Render Selected Write Nodes', 'DeadlineRenderSelected()', 'shift+F7')
```

### nuke UI パネル
```deadline.py
import sys
import nuke

def getPoolStr():
    sys.path.append(r'I:\script\bin\td\deadline\submit\python_api')
    import deadline_api
    from deadline_api import GetPools
    pool = ['nuke']
    pool2 = GetPools()
    for p in pool2:
        if p == "nuke": continue
        pool.append(p)
    poolString = ""
    for i in pool:
        poolString += " "+i
    return poolString

def DeadlineRenderSelected():
    sys.path.append(r'I:\script\bin\td\deadline\submit\nuke\modules')
    from nuke_deadline_v2 import GetInfoTxt, submit_to_deadline

    pool = getPoolStr()

    selectedNodes = []
    for i in nuke.selectedNodes("Write"):
        selectedNodes.append(i.name())
    selectedNodesStr = ""
    if len(selectedNodes) > 0:
        for i in selectedNodes:
            selectedNodesStr += i + ","
        selectedNodesStr = selectedNodesStr[:len(selectedNodesStr)-1]
    else:
        selectedNodesStr = "REN_EXR"

    p = nuke.Panel('Submit to Deadline')
    p.addEnumerationPulldown('pool', pool)
    p.addSingleLineInput('FrameRange', str(int(nuke.root()['first_frame'].value()))+'-'+str(int(nuke.root()['last_frame'].value())))
    p.addSingleLineInput('ChunkSize', "1")
    p.addSingleLineInput('ExcuteNodes', selectedNodesStr)
    p.addSingleLineInput('priority', "50")
    p.addSingleLineInput('machineLimit', "40")
    p.addBooleanCheckBox('suspend', False)
    ret = p.show()
    
    if not ret: return

    frames = p.value('FrameRange')
    root_name = nuke.Root().name()
    chunkSize = p.value('ChunkSize')
    excuteNodes = p.value('ExcuteNodes')
    pool = p.value('pool')
    priority = p.value('priority')
    machineLimit = p.value('machineLimit')
    initialStatus = 'Active'
    if p.value('suspend'):
        initialStatus = 'Suspended'

    job_info_txt, plugin_info_txt = GetInfoTxt(root_name, frames, chunkSize, excuteNodes, pool, priority, machineLimit, initialStatus)
    nuke.message(job_info_txt+""+plugin_info_txt)
    submit_to_deadline(job_info_txt, plugin_info_txt)
```

```nuke13.0v2.bat
@echo off

set NUKE_PATH=C:\Users\%USERNAME%\.nuke
set NUKE_PATH=D:\gizmos_customMenu;D:\nuke

set OPTICAL_FLARES_LICENSE_PATH=C:\Program Files\Nuke13.0v2
set OPTICAL_FLARES_PATH=C:\Program Files\Common Files\Nuke\13.0\plugins
set OPTICAL_FLARES_PRESET_PATH=C:\Program Files\Common Files\Nuke\13.0\plugins\OpticalFlares_Nuke_13.0_Node-Locked_1.0.86\Textures-And-Presets
set peregrinel_LICENSE=license@license

set OCIO=D:\proj\config.ocio

@REM excutable path for deadline
set NUKE_DEADLINE_EXCUTABLE=I:\script\bin\td\bin\vd2_nuke13.0v2.bat

call "C:\Program Files\Nuke13.0v2\Nuke13.0.exe" %*
```

Support Me
https://ibkr.com/referral/zhenwei375
https://ko-fi.com/traderaiz