Skip to content

Instantly share code, notes, and snippets.

@RNavega
Created November 13, 2018 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RNavega/77a68c506afa959b7cfda7f8ccd655c1 to your computer and use it in GitHub Desktop.
Save RNavega/77a68c506afa959b7cfda7f8ccd655c1 to your computer and use it in GitHub Desktop.
Go to the Text Editor in Blender, press the plus ("+") button to create a new text object, paste the code below and press "Run Script". Then on the 3D View, press SPACE and type "random theme operator" and ENTER to use it.
import bpy
import blf
import bgl
import random
def drawCallback(self):
blf.size(0, 16, 72)
text = ('Last changed: ' + self.currentPropName) if self.currentPropName else 'Press LEFT or RIGHT to change color, ESC to stop'
blf.enable(0, blf.SHADOW)
blf.shadow(0, 3, 0.0, 0.0, 0.0, 1.0)
blf.shadow_offset(0, 2, -2)
bgl.glColor3f(1.0, 1.0, 1.0)
blf.position(0, 40, 40, 0)
blf.draw(0, '(Press ESC to stop)')
blf.position(0, 40, 70, 0)
blf.draw(0, text)
blf.disable(0, blf.SHADOW)
class RandomThemeOperator(bpy.types.Operator):
bl_idname = "wm.random_theme_operator"
bl_label = "Random Theme Operator"
bl_options = {'REGISTER'}
def getColorProps(self, colorsList, prop, propName, parentName=''):
for subProp in prop.rna_type.properties:
if subProp.identifier in {'rna_type', 'srna', 'fixed_type'}: # Ignore some properties.
continue
if hasattr(subProp, 'type'):
if subProp.type == 'POINTER':
self.getColorProps(colorsList, getattr(prop, subProp.identifier), subProp.name, parentName if parentName else prop.name)
elif subProp.subtype == 'COLOR_GAMMA' or subProp.subtype == 'COLOR':
colorsList.append(
(prop, subProp.identifier, parentName + ' ' + propName + ' ' + subProp.identifier)
)
else:
self.getColorProps(colorsList, getattr(prop, subProp.identifier), subProp.name, parentName if parentName else prop.name)
return colorsList
def updateColor(self, r, g, b):
prop, attr, name = self.allColorsList[self.index]
setattr(prop, attr, (r, g, b, 1.0) if len(getattr(prop, attr)) == 4 else (r, g, b))
self.currentPropName = name
def modal(self, context, event):
context.area.tag_redraw()
if event.type in {'LEFTMOUSE', 'RIGHTMOUSE', 'ESC'}:
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
bpy.ops.ui.reset_default_theme()
self.report({'INFO'}, 'Cancelled')
return {'CANCELLED'}
if event.type == 'RIGHT_ARROW' and event.value == 'PRESS':
r, g, b = 0.1, 0.3, 0.7
if self.lastDirection == 0:
self.updateColor(r, g, b)
self.index = min(self.index + 1, self.maxLimit)
self.updateColor(r, g, b)
self.lastDirection = 1
elif event.type == 'LEFT_ARROW' and event.value == 'PRESS':
r, g, b = 0.7, 0.5, 0.0
if self.lastDirection == 1:
self.updateColor(r, g, b)
self.index = max(self.index - 1, 0)
self.updateColor(r, g, b)
self.lastDirection = 0
return {'RUNNING_MODAL'}
def execute(self, context):
self.index = 0
theme = context.user_preferences.themes[0]
self.allColorsList = tuple(reversed(self.getColorProps([ ], theme, theme.name)))
self.currentPropName = ''
self.maxLimit = len(self.allColorsList)-1
self.lastDirection = 1
self._handle = bpy.types.SpaceView3D.draw_handler_add(drawCallback, (self,), 'WINDOW', 'POST_PIXEL')
context.window_manager.modal_handler_add(self)
context.area.tag_redraw()
return {'RUNNING_MODAL'}
def register():
bpy.utils.register_class(RandomThemeOperator)
def unregister():
bpy.utils.unregister_class(RandomThemeOperator)
if __name__ == "__main__":
register()
# Go to the 3D View, press SPACE and type "random theme operator".
# Then hold the RIGHT ARROW key.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment