Sending data from Autodesk Maya to an external application using commandPort (python scripting)
<p>I have a implemented a C# application which communicates to Autodesk Maya using a TCP Connection. Maya acts as the server and my application acts as the host. </p>

<p>The python script that is executed in Maya is - </p>

<pre><code>import socket
import maya.cmds as cmds
flag = None
cmds.commandPort(name = "localhost:7777", stp = "python")

def start():
  global flag
  flag = True

def stop():
  global flag 
  flag = False

def close():
  cmds.commandPort(name = "localhost:7777", close = True)

windowZ = cmds.window(title="Object Navigate", w= 350)
cmds.columnLayout(adjustableColumn = True)

startbtn= cmds.button(label = "Start", c = "start()")
stopbtn= cmds.button(label = "Stop", c = "stop()")
closebtn= cmds.button(label = "close", c = "close()")

cmds.showWindow(windowZ)
</code></pre>

<p>I have written a TCPClient C# application (which runs perfectly fine). The data that the application sends looks like this.</p>

<pre><code>Connection.sendData(String.Format("if flag:\n" + "\tcmds.dolly(10,20,30)"));
</code></pre>

<p>The problem with this statement is the flag variable that was declared in the python script is not recognized here. When I just say    cmds.dolly(10,20,30) this command gets executed perfectly.</p>

<p>Now, my question is how do I make the flag variable visible to my C# application OR is there a way to send the value of flag from Maya to the C# application through the commandPort?</p>

<p>Any ideas would be appreciated! </p>

<p>i changed some parts for mayas side</p>

<pre><code>import socket
import maya.cmds as cmds
from functools import partial
FLAG = None
class Hermes(object):
    def __init__(self, **kwargs):
        self.host_id = kwargs['host_id']
        cmds.commandPort(name = "localhost:{0}".format(self.host_id), stp = "python")

    def delete_win(self, **kwargs):
        if cmds.window(kwargs["win_id"], q=True, exists=True):
           cmds.deleteUI(kwargs["win_id"], window=True)

    def submitter(self, **kwargs):
        mod = globals()['Hermes']()
        meth = getattr(mod , kwargs['method'])
        #your flag 
        FLAG = meth()             

    def start(self, flag = True, *args):
        return flag

    def stop(self, flag = False, *args): 
        return flag

    def close(self, *args):
        cmds.commandPort(name = "localhost:{0}".format(self.host_id), close = True)

    def hermes_ui(self, *args):
        delete_win(win_id = "windowZ")
        toll_ui = {}
        toll_ui["windowZ"] = cmds.window("windowZ", title="Object Navigate", w= 350)
        toll_ui["main_pan"] = cmds.paneLayout( configuration='quad', parent =  toll_ui["windowZ"])
        toll_ui["start_btn"]= cmds.button(label = "Start", parent = toll_ui["main_pan"], c = partial(self.submitter, method = "start"))
        toll_ui["stop_btn"]= cmds.button(label = "Stop", parent = toll_ui["main_pan"],c = partial(self.submitter, method = "stop"))
        toll_ui["close_btn"]= cmds.button(label = "close", parent = toll_ui["main_pan"],c = partial(self.submitter,method = "close"))

        cmds.showWindow(toll_ui["windowZ"])


def run(*args):
    hrm = Hermes(host_id = '7777')
    hrm.hermes_ui()
</code></pre>

<p>you need not really the '\n', the seperating is done by simicolon: <code>Connection.sendData(String.Format("if FLAG: cmds.dolly(10,20,30) else cmds.warning('press Start')"));</code>
you can also create a node and add a custom attr and write the flag by start or close than you read the flag as attr(you can change that really easy and friendly without any ui) like:</p>

<pre><code>Connection.sendData(String.Format("if cmds.getAttr('{0}.type'.format('hermes_node')): cmds.dolly(10,20,30)"));
</code></pre>

c# python sockets tcpclient maya