dialogs.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from bpy import context
  2. CONTEXT = {
  3. 0: {
  4. 'title': "Error Message",
  5. 'icon': 'CANCEL'
  6. },
  7. 1: {
  8. 'title': "Warning Message",
  9. 'icon': 'ERROR' # I prefer this icon for warnings
  10. },
  11. 2: {
  12. 'title': "Message",
  13. 'icon': 'NONE'
  14. },
  15. 3: {
  16. 'title': "Question",
  17. 'icon': 'QUESTION'
  18. }
  19. }
  20. def error(message, title="", wrap=40):
  21. """Creates an error dialog.
  22. :param message: text of the message body
  23. :param title: text to append to the title
  24. (Default value = "")
  25. :param wrap: line width (Default value = 40)
  26. """
  27. _draw(message, title, wrap, 0)
  28. def warning(message, title="", wrap=40):
  29. """Creates an error dialog.
  30. :param message: text of the message body
  31. :param title: text to append to the title
  32. (Default value = "")
  33. :param wrap: line width (Default value = 40)
  34. """
  35. _draw(message, title, wrap, 1)
  36. def info(message, title="", wrap=40):
  37. """Creates an error dialog.
  38. :param message: text of the message body
  39. :param title: text to append to the title
  40. (Default value = "")
  41. :param wrap: line width (Default value = 40)
  42. """
  43. _draw(message, title, wrap, 2)
  44. def question(message, title="", wrap=40):
  45. """Creates an error dialog.
  46. :param message: text of the message body
  47. :param title: text to append to the title
  48. (Default value = "")
  49. :param wrap: line width (Default value = 40)
  50. """
  51. _draw(message, title, wrap, 3)
  52. # Great idea borrowed from
  53. # http://community.cgcookie.com/t/code-snippet-easy-error-messages/203
  54. def _draw(message, title, wrap, key):
  55. """
  56. :type message: str
  57. :type title: str
  58. :type wrap: int
  59. :type key: int
  60. """
  61. lines = []
  62. if wrap > 0:
  63. while len(message) > wrap:
  64. i = message.rfind(' ', 0, wrap)
  65. if i == -1:
  66. lines += [message[:wrap]]
  67. message = message[wrap:]
  68. else:
  69. lines += [message[:i]]
  70. message = message[i+1:]
  71. if message:
  72. lines += [message]
  73. def draw(self, *args):
  74. """
  75. :param self:
  76. :param *args:
  77. """
  78. for line in lines:
  79. self.layout.label(line)
  80. title = "%s: %s" % (title, CONTEXT[key]['title'])
  81. icon = CONTEXT[key]['icon']
  82. context.window_manager.popup_menu(
  83. draw, title=title.strip(), icon=icon)