How to copy and paste to the clipboard?

I can’t find a native way to copy and paste to and from the clipboard in bpy.
The method needs to be cross-platform so that it will work on all systems.

I’ve tried out the following :http://coffeeghost.net/2010/10/09/pyperclip-a-cross-platform-clipboard-module-for-python/

I’ve only tested it on Windows, but sometimes it just stops working, even if I shutdown blender, restart it, and then re-start my script which uses the module. I’m not sure if that happens because I reload my script at least 20 times an hour when I’m testing things.

Also, using ‘pyperclip’ means users would have to all install it in the modules folder.

Is there a better way?

text clipboard access is possible via bpy.context.window_manager.clipboard

http://www.blender.org/documentation/blender_python_api_2_67_release/bpy.types.WindowManager.html?highlight=clipboard#bpy.types.WindowManager.clipboard

There’s also a relatively new feature to exchange blender data between multiple windows or scenes (blender internal clipboard), see Blender buffer copy/paste operators:
http://www.blender.org/documentation/blender_python_api_2_67_release/bpy.ops.view3d.html?highlight=paste#bpy.ops.view3d.copybuffer
http://www.blender.org/documentation/blender_python_api_2_67_release/bpy.ops.view3d.html?highlight=paste#bpy.ops.view3d.pastebuffer

2 Likes

Thanks,

If you only need to stay in bpy then that works better than pyperclip, which is good for general python scripts.

Also, if you need the formatting characters, like I do, for the pasted text then do this:

clipboard = bpy.context.window_manager.clipboard
       
#Encode text so it includes formating characters like a file read.
encoded = clipboard.encode('utf8')
2 Likes