# On Screen Display Xray mode

I'm new to Python

I would like to display in the 3D viewport a better representation of the Xray mode. The header icon is not visible enough to me. Not always easy to see the difference between ON and OFF.

I have search for a potential addon or script without success.

• Hello :). Does this help? How to edit 2.8 UI icons? – Jachym Michal Oct 10 '20 at 15:05
• Hi and thanks for your answer but I don't see how it can help for my question – Stefakapapy Oct 11 '20 at 1:42
• Hey :). I thought you wanted to change the header icon, to 'better represent the X-Ray mode', right? If not, please make your question more clear. – Jachym Michal Oct 11 '20 at 5:51
• Hi. Sorry I tought the screenshot was pretty clear. In fact I would like to have a bar at the bottom as you can see – Stefakapapy Oct 11 '20 at 11:20
• It was clear enough, i just missed it :). – Jachym Michal Oct 11 '20 at 11:37

Make some visual overlay

Could not help but see the link between this and Bug in addon to Change header color dynamically The addon there was to remind user they are in automatic keyframe mode.

Remember at the time thinking would make an interesting addon if given a generic way to link a property toggle with toggling a setting. However have not returned until now

Similarly an edit to change the behaviour to instead change the bg color of the 3d view if in xray mode.

Consider this a "proof of concept" test script, a more subtle indicator could be used.

paste into text editor click run script.

import blf
import bpy

bl_info = {
"name": "New Object",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Add > Mesh > New Object",
"description": "Adds a new Mesh Object",
"warning": "",
"wiki_url": "",
}

highlight_color = (0.4, 0, 0)

class DrawingClass:
def __init__(self, prop):
from bpy import context
self.prop = prop
self.draw_text_callback, (), 'WINDOW', 'POST_PIXEL')

def draw_text_callback(self):
from bpy import context
font_id = 0  # XXX, need to find out how best to get this.
if not hasattr(context, "scene"):
return None
# draw some text
blf.position(font_id, 18, 50, 0)
blf.size(font_id, 50, 72)
blf.draw(font_id, f"{self.prop}")

else:

def remove_handle(self):
from bpy import context
bpy.types.SpaceView3D.draw_handler_remove(self.handle, 'WINDOW')

dc = None

def register():
global dc
dc = DrawingClass("XRay Mode")

def unregister():
global dc
if dc:
dc.remove_handle()
dc = None

if __name__ == "__main__":
register()


An issue could be since a draw handler is defined for type, could be an issue with syncing when more than 1 view 3d is being drawn on the screen. Also setting the theme bg color of the viewport is blend file wide.

A quick test confirmed my fears. Notice the text is correct but the theme bg color is not.

What is interesting is having what is like two themes active at once

• Amazing, exactly what I was looking for, thanks a million times – Stefakapapy Oct 11 '20 at 11:25
• Is there a way to center this "XRay Mode" text on the screen and when the screen is resized ? – Stefakapapy Oct 11 '20 at 12:40
• blender.stackexchange.com/a/107646/15543 – batFINGER Oct 11 '20 at 12:42
• Thanks again, I will work on all of your informations you gave me – Stefakapapy Oct 11 '20 at 13:32

Here is the final script if anyone is interested ;)

import blf
import bpy

bl_info = {
"name": "Better Xray Visualization",
"author": "Stefakapapy",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "Toggle Xray",
"description": "Displays better visual representation on screen",
"category": "Interface",
}

class DrawingClass:
def __init__(self, prop):
from bpy import context

self.prop = prop
self.draw_text_callback, (), 'WINDOW', 'POST_PIXEL')

def draw_text_callback(self):
from bpy import context

if not hasattr(context, "scene"):
return None

display_xray = True
else:
display_xray = False

if display_xray:
font_id = 0  # XXX, need to find out how best to get this.
color1 = (0, 0.624, 1.0, 1.0)
width = context.area.width

blf.size(font_id, 40, 72)
dim_x = blf.dimensions(font_id, self.prop)[0]/2
blf.position(font_id, width/2-dim_x, 18, 0)
blf.color(font_id, *color1)
blf.draw(font_id, f"{self.prop}")

def remove_handle(self):
from bpy import context
bpy.types.SpaceView3D.draw_handler_remove(self.handle, 'WINDOW')

dc = None

def register():
global dc
dc = DrawingClass("XRay Mode")

def unregister():
global dc
if dc:
dc.remove_handle()
dc = None

if __name__ == "__main__":
register()

• Good one. For future ref, can simply edit your previous answer. At a certain rep deleted answers are visible. This post is hidden. It was deleted 55 mins ago by the post author. – batFINGER Oct 14 '20 at 13:35