Show transparent viewport image (via script)

[blender 2.8]

I'd like to show a transparent image on the viewport (similar to the axes gizmo) and came up with two different approaches:

1) GPU image shader - which works in principle, but doesn't seem to support alpha (which I need)

2) Script a object lock to the viewport. Basically what "Lock camera to view" does, but with an object that does not move relative to the viewport. With an image plane and the proper viewport shading, this might be a viable workaround. Can "Lock view to camera" be misused for that?

Any thoughts?

2 Answers

Your second idea works!

1. Use the "Import images as planes" option (activate addon in preferences) or make Empty and set display as image.
2. Then make a dummy camera and parent it (keep transform) to you image plane
3. Activate textured view and make sure that in Materials -> Settings you set Blend mode to "Alpha blend" (or use alpha is activated if you used an Empty as Image)
4. You can use +/- to zoom camera border, so its no longer in view. (changing the zoom here works only while camera not locked to view)
5. Make the plane "unselectable" (by clicking cursor symbol in Outliner) otherwise you will always select the plane if locked to view ;)
6. Finally activate "Lock Camera to view"

And you're done! Here is my implementation:

+

Here is the same procedure in Code: PS: However there is a small TODO section for setting the 3d view to the Camera view in script, which i havent quite figured out how to do yet

import bpy
from mathutils import Euler ##For Rotations

######Deletes all Objects in default blender startup
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
######
########### Make Objects you need: Camera-Dummy and Empty as Image (and set it as parent)

imgEmpty = bpy.data.objects.new("myImgEmpty", None)
scene = bpy.context.scene
bpy.context.collection.objects.link(imgEmpty)
imgEmpty.location = (0, 0, -1.5)
imgEmpty.rotation_euler = Euler((0, 0, 0), 'XYZ')
imgEmpty.empty_display_type = 'IMAGE'
imgOverlay = bpy.data.images.load("C:/Users/robert/Desktop/Installers/2019/blender-2.80.0-git.d30f72dfd8ac-windows64/TestOverlay.png", check_existing=True) ##### CHANGE PATH !!!
imgEmpty.data = imgOverlay

bpy.ops.object.camera_add(location=(0, 0, 0), rotation=(1.5708, 0, 0)) ##90deg in radian
dummyCamera = bpy.context.view_layer.objects.active
layer = bpy.context.view_layer
layer.update()

imgEmpty.parent = dummyCamera

###########
################ Set 3d view to Camera view and Zoom

#TODO ;)

################
##################### Set camera to Lock camera to view and Set 3d View to Textured view
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
space.lock_camera = True
space.shading.type = 'MATERIAL'
#####################


Happy Blending!

• The easiest (work-around style) solution to "script" this would be to do it without scripting first, save a blend file and then open that file at startup of your script. – Robert Roth Jul 2 '19 at 14:38
• I now also added a Script to my answer, following the same procedure. Don't forget to change the image Path! – Robert Roth Jul 2 '19 at 16:23
• Thanks Robert - this is a viable answer within the scope of my question! However it is less robust, than I'd like it to be - i.e. it breaks when using any of the views from numpad. I was working on approach 1 in the meanwhile an will post a solution using that approach. – squarespiral Jul 2 '19 at 17:15

Ok, here is how I did it: my starting point was an svg with "holes" so there was a need for (binary) transparency, but no real requirement for alpha (not sure how to implement the latter). I imported the svg, created a mesh from it and exported it's IBO.

Then I slightly modified method 1 from my question by reading the IBO from a file and rendering the tris onto the viewport directly. The method has the advantage that it is very robust and does not break when changing between different views in the viewport. It does have the disadvantage that you need to triangulate first and alpha is not possible (it probably is, but I couldn't find the proper shading settings).