Script to change background color upon mode

Hello!

I’ve “developed” a script to change background color depending on what mode I’m working with. I.E: gray when object mode, red-ish when edit mode… I don’t what to move eyes to know what mode I’m currently working… you know :slight_smile:

It’s a very primitive script, and I do know it’s causing some caos with other add-ons, but it’s my script and I like it.

Since sharing is caring, and I do know this board is full of people talented with these endeavours, I´d like to sharing it here to hear some suggestions and mods, and, why not, lets put this elsewhere to a broad audience if someone wants to give it a try, or just make its own… whatever! This script is serving me well since 2.80, and I found it a vital part of my daily work. I only want this bloody thing works better! (i.e. have a proper color picker at preferences) DISCLAIMER: I’ve almost ZERO idea on python coding. I do know the basics of programming, but nothing more, right? don’t roast me :smiley:

bl_info = {
    "name": "Active Background",
    "author": "Dspazio",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "View3D",
    "description": "Changes background gradient color for each mode.",
    "warning": "",
    "doc_url": "",
    "category": "Themes",
}

import bpy
from bpy.app.handlers import persistent

Modes = {'OBJECT':(0.30, 0.30, 0.30),
        'EDIT':(0.30, 0.20, 0.20),
        'SCULPT':(0.05, 0.05, 0.05),
        'VERTEX_PAINT':(0.05, 0.05, 0.05),
        'WEIGHT_PAINT':(0.05, 0.05, 0.05),
        'TEXTURE_PAINT':(0.05, 0.05, 0.05)}

@persistent
def my_handler(scene):
    mode = bpy.context.object.mode
    bpy.context.preferences.themes[0].view_3d.space.gradients.high_gradient = Modes[mode]
    
class addActiveBackgroundPreferences(bpy.types.AddonPreferences):
      bl_idname = __name__
 
      edit_mode_color: bpy.props.FloatVectorProperty(name="Edit Mode Color", subtype='COLOR', default=Modes['EDIT'], size=3, min=0, max=1)
        
      def draw(self, context):
          layout = self.layout
          layout.label(text='Edit Mode :')
          row = layout.row()
          row.prop(self, 'edit_mode_color', expand=True)
    
def register():
    bpy.app.handlers.depsgraph_update_post.append(my_handler)
    bpy.utils.register_class(addActiveBackgroundPreferences)

def unregister():
    bpy.app.handlers.depsgraph_update_post.remove(my_handler)
    bpy.utils.register_class(addActiveBackgroundPreferences)

if __name__ == "__main__":
    register()
6 Likes

Wow! I needed exactly this! Thank you! I made a proposal on Rightclickselect forum, linked your post there:

One problem: If the script it is active and I create az image empty and try to scale/move it with mouse (dragging the frame corners or center), Blender instantly crashes.

2 Likes

Hey, I was once developing the same thing but using an outline in the viewport instead. It seems to be what you’re suggesting on RCS. I remember my implementation of the ‘mode tracking’ was a huuuuge hurdle to get right, and wasn’t event 100% reliable since I was using an obscure part of the Python API, can’t even remember the name now.

@Dspazio’s solution seems really practical however, and all the rest of the code I already have.

If you’re interested I could revive that little project and finish it! At the time I thought it was just a really personal aesthethic quirk, but it’s nice to see there are others with the same quirk :smile:

2 Likes

I am definitely interested in your script, it’s much more than aesthetic quirk! In a good software user always have to know the actual interaction mode, and border color coding is the best method for this. I’m wondering how many people disagree in this fact.

1 Like

Hello! Thank you for give it a try. Yes thats the main problem. It crashes when interacting with these image gizmos.

I’m not skilled in phyton, and I’m now focused on other tasks but would be great if someone comes up with a solution for this. I’ll try to take a look at it again on next months to see if I can fix it.

Would be great. I’m not a fan of the coloured frame. I’d prefeer a subtle change of the background gradient, but maybe it can evolve to a more customizable theme manager.

Anyway, thank you so much and yes! Do please share your progress :slightly_smiling_face: :pray: Cheers!

1 Like

Alright then, I’m in! Yeah, I always mess up when working with pose mode, never know if I’m in edit, object or what. It’s nice to have more visual interfaces and not just textual data.

What exactly makes it crash you say? It crashed once when I was trying it out, but I hadn’t created any empties, and now I’m scaling an image empty and it works fine.

I too think a background change would be less intrusive, but might aswell have it all and let the user decide. I like adding flexible configuration to things.

Alright. I’ll see if this becomes my pet project on the weekend and I’ll post any updates here.

2 Likes

Thank you so much for give it a push!

IIRC my script crashed when you used these yellow/red image resize/rotation/move gizmo. It shows, but as soon as you click on one of these controls, blender crashed.

Happened to me at some stages of decalmachine atlas creation, but I also found it crashed on vanilla blender.

Sorry for not being more specific, I think that if you drop a jpg in the viewport you have such controls!

Cheers!

1 Like

It alwals crashes when I create an Image Empty object, for example I drag and drop an image from file manager into a Blender 3d view, and I am trying to resize it with mouse. My OS is Win7 64 bit, maybe on Win10 it’s not crashing. I am new to Blender Python API yet, now learning, so don’t exactly know, what causes the crash.
I reduced the problem to the critical line and posted on blender.stackexchange, some guys answered it:

1 Like

Oh, timers. That’s another interesting solution. I started fiddling with the ‘obscure’ thing I mentioned before (the message bus) to see if I could spare recalling the check all the time, but ran into other issues. Just like old times. Perhaps it is just unavoidable.

Anyhow, all of these 3 are definitely good building blocks. I’ll keep testing stuff then.

1 Like