How to change the background with a picture in 3d window?

See my answers above.

If you are thinking about changing the background color so that you can remove that color and replace it with an image, then there is a much simpler way: you can save the view content with transparent background. See “Capture 3D view into PNG file with transparent background” example in the script repository.