Get the texture from selected node or from UV/Image Editor

Hello,

I’m having some trouble with that script and Cycles: https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/UV/Bake_Texture_to_Vertex_Colors

Sometimes it works properly, and sometimes it doesn’t use the texture that is supposed to be active. It seems to be related to Cycles materials, I can kinda fix it by switching to blender internal and recreating the shaders with the texture, but that’s a bit of a hassle.

So I’m trying to modify the script, instead of using data.uv_textures.active I’d like to use the texture that I select in the node editor, or the texture that I’m looking at in the UV/Image editor, both are fine.

I’m still a noob at scripting with Blender, can anyone help me finding the command I need to do so?

Thanks!