使用commandPort (python脚本)将数据从Autodesk Maya发送到外部应用程序

本文关键字:Maya Autodesk 应用程序 外部 数据 commandPort python 脚本 使用 | 更新日期: 2023-09-27 18:06:12

我已经实现了一个c#应用程序,它使用TCP连接与Autodesk Maya通信。Maya作为服务器,我的应用程序作为主机。

在Maya中执行的python脚本是-

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)

我写了一个TCPClient c#应用程序(它运行得很好)。应用程序发送的数据如下所示:

Connection.sendData(String.Format("if flag:'n" + "'tcmds.dolly(10,20,30)"));

这个语句的问题是python脚本中声明的标志变量在这里无法识别。当我输入cmd .dolly(10,20,30)时,这个命令执行得很好。

现在,我的问题是我如何使标志变量可见到我的c#应用程序或有一种方法通过命令端口从玛雅发送到c#应用程序的标志值?

任何想法都会很感激!

使用commandPort (python脚本)将数据从Autodesk Maya发送到外部应用程序

我更改了mayas侧的一些部分

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

实际上不需要''n',分隔是由分号:Connection.sendData(String.Format("if FLAG: cmds.dolly(10,20,30) else cmds.warning('press Start')"));完成的您还可以创建一个节点并添加自定义attr,并通过start或close写入标志,而不是将标志读取为attr(您可以轻松友好地更改,无需任何ui),例如:

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