hi all
i read  Eric Pavey's  wingide and maya working together tutorial here 
<http://mayamel.tiddlyspot.com/#[[How%20can%20I%20have%20Wing%20send%20Python%20or%20mel%20code%20to%20Maya%3F]]>,
 
and used 

executeWingCode.py <http://pastebin.ubuntu.com/945883/> and wingHotkeys.py 
<http://pastebin.ubuntu.com/945884/>
from here  <http://www.eksod.com/2011/07/wing-ide-with-maya-on-linux/>

it works perfectly fine in maya 2010, but in 2011 / 2012 / 2013 it doesn't work.
if i use
mSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
it works for the first time, but won't be able to send python commands to maya 
from second time on ?
what should i change to make it work in newer versions of maya ? thanks.

in office, the same setup works fine though, ( office environment is centos 4 + 
maya 2011 )
thanks!

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe
"""
executeWingCode.py
Eric Pavey - 2011-03-23
Module that Maya calls to when Wing pings it through a socket, telling Maya
to execute the commands in the temp file as either Python or mel code.
"""
import __main__
import os
import maya.OpenMaya as om
import sys

def main(codeType):
    """
    Evaluate the temp file on disk, made by Wing, in Maya.

    codeType : string : Supports either 'python' or 'mel'

    """
    if sys.platform.startswith('win'):
        tempFile = os.path.join(os.environ['TMP'], 'wingData.txt')
    if sys.platform.startswith('linux'):
        tempFile = "//tmp//wingData.txt" # tempfile.gettempdir() gives different results between wing/maya

    if os.access(tempFile , os.F_OK):
        # open and print the file in Maya:
        f =  open(tempFile , "r")
        lines = f.readlines()
        for line in lines:
            print line.rstrip()
        f.close()
        print "\n",

        if codeType == "python":
            # execute the file contents in Maya:
            f = open(tempFile , "r")
            exec(f, __main__.__dict__, __main__.__dict__)
            f.close()

        elif codeType == "mel":
            melCmd = 'source "%s"'%tempFile
            # This causes the "// Result: " line to show up in the Script Editor:
            om.MGlobal.executeCommand(melCmd, True, True)
    else:
        print "No temp file exists: " + tempFile
# wingHotkeys.py
# Author:  Eric Pavey - [email protected]

import wingapi
import socket
import os
import sys

def getWingText():
   """
   Based on the Wing API, get the selected text, and return it
   """
   editor = wingapi.gApplication.GetActiveEditor()
   if editor is None:
      return
   doc = editor.GetDocument()
   start, end = editor.GetSelection()
   txt = doc.GetCharRange(start, end)
   return txt

def send_to_maya(language):
   """
   Send the selected code to be executed in Maya

   language : string : either 'mel' or 'python'
   """
   # The commandPort you opened in userSetup.mel.  Make sure this matches!
   commandPort = 6000

   if language != "mel" and language != "python":
      raise ValueError("Expecting either 'mel' or 'python'")

   # Save the text to a temp file.  If we're dealing with mel, make sure it
   # ends with a semicolon, or Maya could become angered!
   txt = getWingText()
   if language == 'mel':
      if not txt.endswith(';'):
         txt += ';'
   
   # This saves a temp file on Windows or Linux.
   # Mac might need to change this to support their OS. (/tmp/wingData.txt would work also)
   if sys.platform.startswith('win'):
      tempFile = os.path.join(os.environ['TMP'], 'wingData.txt')
   if sys.platform.startswith('linux'):
      tempFile = "//tmp//wingData.txt" # tempfile.gettempdir() gives different results between wing/maya
      
   f = open(tempFile, "w")
   f.write(txt)
   f.close()

   # Create the socket that will connect to Maya,  Opening a socket can vary from
   # machine to machine, so if one way doesn't work, try another... :-S
   #mSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # works on Linux! :)
   #mSocket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) # Works!
   # More generic code for socket creation thanks to Derek Crosby:
   if sys.platform.startswith('win'):
      res = socket.getaddrinfo("localhost", commandPort, socket.AF_UNSPEC, socket.SOCK_STREAM)
      af, socktype, proto, canonname, sa = res[0]
      mSocket = socket.socket(af, socktype, proto)
   if sys.platform.startswith('linux'):
      mSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

   # Now ping Maya over the command-port
   try:
      # Make our socket-> Maya connection:   There are different connection ways
      # which vary between machines, so sometimes you need to try different
      # solutions to get it to work... :-S
      #mSocket.connect(("127.0.0.1", commandPort))
      #mSocket.connect(("::1",commandPort))  #works!
      mSocket.connect(("localhost", commandPort))

      # Send our code to Maya:
      mSocket.send('python("import executeWingCode; executeWingCode.main(\'%s\')")'%language)
   except Exception, e:
      print "Send to Maya fail:", e

   mSocket.close()

# wrappers to be added on keymap.normal on wingide installation folder. example syntax:
# 'Ctrl-P': 'python_to_maya()'
def python_to_maya():
   """Send the selected Python code to Maya"""
   send_to_maya('python')
   
def all_python_to_maya():
   """Select ALL current file and send to Maya"""
   wingapi.gApplication.ExecuteCommand('select-all')   
   send_to_maya('python')
   
def mel_to_maya():
   """Send the selected code to Maya as mel"""
   send_to_maya('mel')
   
def all_mel_to_maya():
   """Select ALL current file and send to Maya"""
   wingapi.gApplication.ExecuteCommand('select-all')   
   send_to_maya('mel')

Reply via email to