Guest User

Untitled

a guest
Nov 13th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import bpy
  2. import blf
  3. import bgl
  4. import random
  5.  
  6.  
  7. def drawCallback(self):
  8. blf.size(0, 16, 72)
  9. text = ('Last changed: ' + self.currentPropName) if self.currentPropName else 'Press LEFT or RIGHT to change color, ESC to stop'
  10.  
  11. blf.enable(0, blf.SHADOW)
  12. blf.shadow(0, 3, 0.0, 0.0, 0.0, 1.0)
  13. blf.shadow_offset(0, 2, -2)
  14.  
  15. bgl.glColor3f(1.0, 1.0, 1.0)
  16. blf.position(0, 40, 40, 0)
  17. blf.draw(0, '(Press ESC to stop)')
  18. blf.position(0, 40, 70, 0)
  19. blf.draw(0, text)
  20.  
  21. blf.disable(0, blf.SHADOW)
  22.  
  23.  
  24.  
  25. class RandomThemeOperator(bpy.types.Operator):
  26. bl_idname = "wm.random_theme_operator"
  27. bl_label = "Random Theme Operator"
  28. bl_options = {'REGISTER'}
  29.  
  30.  
  31. def getColorProps(self, colorsList, prop, propName, parentName=''):
  32. for subProp in prop.rna_type.properties:
  33. if subProp.identifier in {'rna_type', 'srna', 'fixed_type'}: # Ignore some properties.
  34. continue
  35.  
  36. if hasattr(subProp, 'type'):
  37. if subProp.type == 'POINTER':
  38. self.getColorProps(colorsList, getattr(prop, subProp.identifier), subProp.name, parentName if parentName else prop.name)
  39. elif subProp.subtype == 'COLOR_GAMMA' or subProp.subtype == 'COLOR':
  40. colorsList.append(
  41. (prop, subProp.identifier, parentName + ' ' + propName + ' ' + subProp.identifier)
  42. )
  43. else:
  44. self.getColorProps(colorsList, getattr(prop, subProp.identifier), subProp.name, parentName if parentName else prop.name)
  45. return colorsList
  46.  
  47.  
  48. def updateColor(self, r, g, b):
  49. prop, attr, name = self.allColorsList[self.index]
  50. setattr(prop, attr, (r, g, b, 1.0) if len(getattr(prop, attr)) == 4 else (r, g, b))
  51. self.currentPropName = name
  52.  
  53.  
  54. def modal(self, context, event):
  55. context.area.tag_redraw()
  56.  
  57. if event.type in {'LEFTMOUSE', 'RIGHTMOUSE', 'ESC'}:
  58. bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
  59. bpy.ops.ui.reset_default_theme()
  60. self.report({'INFO'}, 'Cancelled')
  61. return {'CANCELLED'}
  62.  
  63. if event.type == 'RIGHT_ARROW' and event.value == 'PRESS':
  64. r, g, b = 0.1, 0.3, 0.7
  65. if self.lastDirection == 0:
  66. self.updateColor(r, g, b)
  67. self.index = min(self.index + 1, self.maxLimit)
  68. self.updateColor(r, g, b)
  69. self.lastDirection = 1
  70.  
  71. elif event.type == 'LEFT_ARROW' and event.value == 'PRESS':
  72. r, g, b = 0.7, 0.5, 0.0
  73. if self.lastDirection == 1:
  74. self.updateColor(r, g, b)
  75. self.index = max(self.index - 1, 0)
  76. self.updateColor(r, g, b)
  77. self.lastDirection = 0
  78.  
  79. return {'RUNNING_MODAL'}
  80.  
  81.  
  82. def execute(self, context):
  83. self.index = 0
  84. theme = context.user_preferences.themes[0]
  85. self.allColorsList = tuple(reversed(self.getColorProps([ ], theme, theme.name)))
  86. self.currentPropName = ''
  87. self.maxLimit = len(self.allColorsList)-1
  88. self.lastDirection = 1
  89.  
  90. self._handle = bpy.types.SpaceView3D.draw_handler_add(drawCallback, (self,), 'WINDOW', 'POST_PIXEL')
  91. context.window_manager.modal_handler_add(self)
  92. context.area.tag_redraw()
  93. return {'RUNNING_MODAL'}
  94.  
  95.  
  96. def register():
  97. bpy.utils.register_class(RandomThemeOperator)
  98.  
  99.  
  100. def unregister():
  101. bpy.utils.unregister_class(RandomThemeOperator)
  102.  
  103.  
  104. if __name__ == "__main__":
  105. register()
  106.  
  107. # Go to the 3D View, press SPACE and type "random theme operator".
  108. # Then hold the RIGHT ARROW key.
Add Comment
Please, Sign In to add comment