How do I Execute Multi-Line commandPort Code?

How do I Execute Multi-Line commandPort Code?

Henrik_Cederblad
Advocate Advocate
922 Views
2 Replies
Message 1 of 3

How do I Execute Multi-Line commandPort Code?

Henrik_Cederblad
Advocate
Advocate

I've managed to send single-line commands via commandPort, but haven't figured out how to send multi-line Python code. I'm getting a lot weirdness on scripts that otherwise work if executed as shelf buttons. Maya reads the lines one by one, as separate commands!

 

This is my send_to_Maya.pyw script:

 

import socket

HOST = '127.0.0.1'
PORT = 54322  # I'm using 54321 for MEL and 54322 for Python commands
ADDR = (HOST, PORT)

def send_command():
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(ADDR)
    command = '#HOW DO I PROPERLY FORMAT MULTILINE CODE HERE?'
    message = command
    client.send(str.encode(message))
    data = client.recv(1024)  # receive the result info
    client.close()
    print(data)

if __name__ == '__main__':
    
    # Run this within Maya to open the ports. The port can be any 5 digits
    """
    import maya.cmds as cmds
    cmds.commandPort(name=":54322", sourceType="python")
    """
    
    send_command()

 

Here's an example snippet that I want to send to Maya:

 

import maya.cmds as cmds
ver = cmds.about(v=True)

cmds.loadPlugin("ziCut_{}".format(ver))

if cmds.contextInfo('ziDrillCtx1', ex=True):
    cmds.deleteUI('ziDrillCtx1', tc=True)

ziCutctx = cmds.ziDrillCtx()
cmds.setToolTo(ziCutctx)

 

^ How do I incorporate this in the send_to_Maya.pyw file?

 

 

0 Likes
923 Views
2 Replies
Replies (2)
Message 2 of 3

Henrik_Cederblad
Advocate
Advocate

I'm truly sorry for bumping the topic but it's been a month and this is becoming really crucial in my case. I'm no friends with Shelf buttons as I find it very ineffective, and I'm not friends binding hotkeys to commands since I've ended up using all available hotkeys in the program. I'm neither friends with setting up Marking menus as it's too complicated to maintain, when you've got hundreds of specific niche scripts.

 

I'd be so thankful if someone knowledgeable, perhaps even someone @Anonymous  could help me out formating multi-line Python code properly when sending commands via commandPort. Single-line works perfectly, just not multi.....

0 Likes
Message 3 of 3

j.stoilov
Community Visitor
Community Visitor

Hi,

Did you try something like this?:
command = "python('line1;line2;line3');"

 

it would be like this:

message = r"""python('import maya.cmds as cmds;
ver = cmds.about(v=True);
cmds.loadPlugin("ziCut_{}".format(ver));
cmds.deleteUI('ziDrillCtx1', tc=True) if cmds.contextInfo('ziDrillCtx1', ex=True): else pass;
ziCutctx = cmds.ziDrillCtx();
cmds.setToolTo(ziCutctx)')"""

I just embed two lines of python in that way within a mel script and it worked.

 

Or maybe this (not tested):

message = r"""import maya.cmds as cmds
ver = cmds.about(v=True)

cmds.loadPlugin("ziCut_{}".format(ver))

if cmds.contextInfo('ziDrillCtx1', ex=True):
    cmds.deleteUI('ziDrillCtx1', tc=True)

ziCutctx = cmds.ziDrillCtx()
cmds.setToolTo(ziCutctx)"""
command = "python('%s')" % message

 

 

0 Likes