Message 1 of 2
Changes in using ctypes (python) for Maya 2014/2015?

Not applicable
01-26-2015
09:55 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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()