Changes in using ctypes (python) for Maya 2014/2015?

Changes in using ctypes (python) for Maya 2014/2015?

Anonymous
Not applicable
869 Views
1 Reply
Message 1 of 2

Changes in using ctypes (python) for Maya 2014/2015?

Anonymous
Not applicable

I have a nice little clipboard function that I found on stackoverflow that works fine in Maya 2011. However in Maya 2015 this function is no longer working. It doesn't throw any errors and it completely breaks my clipboard, meaning I can't copy/cut/paste text outside of Maya.

 

Since this code is a too dense for me, I am asking you guys if there's been any changes I need to account for since Maya updated python from 2.6.4 to 2.7.3 to get it working again. 

 

def winSetClipboard(text):
    GMEM_DDESHARE = 0x2000
    ctypes.windll.user32.OpenClipboard(None)
    ctypes.windll.user32.EmptyClipboard()
    try:
        # works on Python 2 (bytes() only takes one argument)
        hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text))+1)
    except TypeError:
        # works on Python 3 (bytes() requires an encoding)
        hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text, 'ascii'))+1)
    pchData = ctypes.windll.kernel32.GlobalLock(hCd)
    try:
        # works on Python 2 (bytes() only takes one argument)
        ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text))
    except TypeError:
        # works on Python 3 (bytes() requires an encoding)
        ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text, 'ascii'))
    ctypes.windll.kernel32.GlobalUnlock(hCd)
    ctypes.windll.user32.SetClipboardData(1,hCd)
    ctypes.windll.user32.CloseClipboard()

 

 

0 Likes
870 Views
1 Reply
Reply (1)
Message 2 of 2

mikey.n
Enthusiast
Enthusiast

Hello,

I ran into the same issue and posted about it on the forum. The responses my help you: https://forums.autodesk.com/t5/maya-programming/ctypes-bug-cannot-copy-data-to-clipboard-via-python/...

0 Likes