Guest User

Untitled

a guest
Oct 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. class ObjectRefItem(bpy.types.PropertyGroup):
  2. name = bpy.props.StringProperty(name="Object Name", default="Unknown")
  3. ref = bpy.props.IntProperty(name="Ref", default=22, subtype='UNSIGNED')
  4.  
  5. class ExamplePanel(Panel):
  6. bl_idname = "SCENE_PT_Example"
  7. bl_label = "Example"
  8. bl_space_type = 'PROPERTIES'
  9. bl_region_type = 'WINDOW'
  10. bl_context = "scene"
  11.  
  12. def draw(self, context):
  13. layout = self.layout
  14. scn = bpy.context.scene
  15.  
  16. row = layout.row()
  17. row.operator("custom.example_action", text="DIALOG").action = 'DIALOG'
  18. row.operator("custom.example_action", text="POPUP").action = 'POPUP'
  19. row.operator("custom.example_action", text="SEARCH").action = 'SEARCH'
  20.  
  21. def item_getter(self, context):
  22. numItems= 1000
  23. for i in range(numItems):
  24. yield (str(i), "Object %i" % i, "")
  25.  
  26. class ExampleDialog(bpy.types.Operator):
  27. bl_idname = "object.dialog_operator"
  28. bl_label = "Simple Dialog Operator"
  29.  
  30. def execute(self, context):
  31. scn = bpy.context.scene
  32. index = scn.custom_index
  33. message = '%s (index %d) selected' % (scn.custom[index].name, index)
  34. self.report({'INFO'}, message)
  35. return {'FINISHED'}
  36.  
  37. def invoke(self, context, event):
  38. wm = context.window_manager
  39. return wm.invoke_props_dialog(self, width=800, height=800)
  40.  
  41. def draw(self, context):
  42. layout = self.layout
  43. scn = bpy.context.scene
  44.  
  45. col = layout.column()
  46. col.template_list("UI_UL_list", "example_dialog", scn, "custom", scn, "custom_index", rows=4)
  47.  
  48. class ExamplePopup(bpy.types.Operator):
  49. bl_idname = "object.popup_operator"
  50. bl_label = "Simple Popup Operator"
  51. bl_options = {'REGISTER', 'UNDO'}
  52.  
  53. my_enum = bpy.props.EnumProperty(items=item_getter)
  54.  
  55. def execute(self, context):
  56. message = '%s selected' % self.my_enum
  57. self.report({'INFO'}, message)
  58. return {'FINISHED'}
  59.  
  60. def invoke(self, context, event):
  61. wm = context.window_manager
  62. return wm.invoke_props_popup(self, event)
  63.  
  64. class ExampleSearch(bpy.types.Operator):
  65. bl_idname = "object.search_operator"
  66. bl_label = "Simple Search Operator"
  67. bl_property = "my_enum"
  68.  
  69. my_enum = bpy.props.EnumProperty(items=item_getter)
  70.  
  71. def execute(self, context):
  72. message = '%s selected' % self.my_enum
  73. self.report({'INFO'}, message)
  74. return {'FINISHED'}
  75.  
  76. def invoke(self, context, event):
  77. wm = context.window_manager
  78. wm.invoke_search_popup(self)
  79. return {'FINISHED'}
  80.  
  81. class ExampleActions(Operator):
  82. bl_idname = "custom.example_action"
  83. bl_label = "Example Action"
  84.  
  85. action = bpy.props.EnumProperty(
  86. items=(
  87. ('DIALOG', "Dialog", ""),
  88. ('POPUP', "Pop-up", ""),
  89. ('SEARCH', "Search", "")
  90. )
  91. )
  92.  
  93. def invoke(self, context, event):
  94.  
  95. scn = context.scene
  96. wm = context.window_manager
  97.  
  98. if self.action == 'DIALOG':
  99. scn.custom.clear()
  100. for index, name, _ in item_getter(self, context):
  101. item = scn.custom.add()
  102. item.ref = int(index)
  103. item.name = name
  104. bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
  105.  
  106. elif self.action == 'POPUP':
  107. bpy.ops.object.popup_operator('INVOKE_DEFAULT')
  108.  
  109. elif self.action == 'SEARCH':
  110. bpy.ops.object.search_operator('INVOKE_DEFAULT')
  111.  
  112. return {"FINISHED"}
  113.  
  114. def register():
  115. bpy.utils.register_class(ExamplePanel)
  116. bpy.utils.register_class(ExampleDialog)
  117. bpy.utils.register_class(ExamplePopup)
  118. bpy.utils.register_class(ExampleSearch)
  119. bpy.utils.register_class(ExampleActions)
  120. bpy.utils.register_class(ObjectRefItem)
  121. bpy.types.Scene.custom = CollectionProperty(type=ObjectRefItem)
  122. bpy.types.Scene.custom_index = IntProperty()
  123.  
  124. def unregister():
  125. bpy.utils.unregister_class(ExamplePanel)
  126. bpy.utils.unregister_class(ExampleDialog)
  127. bpy.utils.unregister_class(ExamplePopup)
  128. bpy.utils.unregister_class(ExampleSearch)
  129. bpy.utils.unregister_class(ExampleActions)
  130. bpy.utils.unregister_class(ObjectRefItem)
  131. del bpy.types.Scene.custom
  132. del bpy.types.Scene.custom_index
  133.  
  134. if __name__ == "__main__":
  135. register()
Add Comment
Please, Sign In to add comment