Guest User

Untitled

a guest
Jul 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. import bpy
  2. from bpy.types import Panel, EnumProperty, WindowManager
  3. from bpy.props import *
  4. import bpy.utils.previews
  5.  
  6. import os
  7. import json
  8.  
  9. props_list = []
  10.  
  11. with open(os.path.join(os.path.dirname(__file__), "props_list.json")) as json_file:
  12. data = json.load(json_file)
  13. props_list = data["Models"]
  14.  
  15. # UI
  16. class PropsPreviewsPanel(bpy.types.Panel):
  17. bl_label = "Props Library"
  18. bl_idname = "OBJECT_PT_previews"
  19. bl_space_type = 'VIEW_3D'
  20. bl_region_type = 'TOOLS'
  21. bl_category = "Props Library"
  22.  
  23. def draw(self, context):
  24. layout = self.layout
  25. wm = context.window_manager
  26. # This tells Blender to draw the my_previews window manager object
  27. # (Which is our preview)
  28. row = layout.row()
  29. row.template_icon_view(context.scene, "props_thumbnails", show_labels=True)
  30.  
  31. row = layout.row()
  32. row.prop(context.scene, 'PropEnum')
  33.  
  34. row = layout.row()
  35. row.prop(context.scene, 'PropEnumSec')
  36.  
  37. # Just a way to access which one is selected
  38. row = layout.row()
  39. row.label(text="You selected: " + bpy.context.scene.PropEnum)
  40.  
  41. row = layout.row()
  42. row.operator(
  43. Props_Import.bl_idname,
  44. text = "Import",
  45. icon='APPEND_BLEND')
  46.  
  47. preview_collections = {}
  48.  
  49. class Props_Import(bpy.types.Operator):
  50. bl_idname = "object.props_imp"
  51. bl_label = "Import"
  52. bl_options = {'REGISTER', 'UNDO'}
  53.  
  54. def execute_import(self, context):
  55.  
  56. dirpath = os.path.join(os.path.dirname(__file__), "blends/retro.blend/Object/")
  57.  
  58. bpy.ops.wm.append(filename="Stop", directory=dirpath)
  59.  
  60. return {'FINISHED'}
  61.  
  62. def generate_previews(self, context):
  63. # We are accessing all of the information that we generated in the register function below
  64. pcoll = preview_collections["thumbnail_previews"]
  65. image_location = pcoll.images_location
  66. VALID_EXTENSIONS = ('.png', '.jpg', '.jpeg')
  67.  
  68. enum_items = []
  69. i = 0
  70.  
  71. # Generate the thumbnails
  72. for prop, category, subcategory, prop_image in props_list:
  73. filepath = os.path.join(image_location, prop_image)
  74. pcoll.clear()
  75. thumb = pcoll.load(prop, filepath, 'IMAGE', force_reload=True)
  76. enum_items.append((prop, prop, "", thumb.icon_id, i))
  77. i += 1
  78.  
  79. return enum_items
  80.  
  81. def generate_subcategories(self, context):
  82.  
  83. enum_subcat = []
  84.  
  85. if self.PropEnum == 'Kitchen & Food':
  86. enum_subcat.append(('Glassware', 'Glassware', ''))
  87. enum_subcat.append(('subcat2', 'Subcategory 2', ''))
  88. enum_subcat.append(('subcat3', 'Subcategory 3', ''))
  89. enum_subcat.append(('subcat4', 'Subcategory 4', ''))
  90. enum_subcat.append(('subcat5', 'Subcategory 5', ''))
  91. enum_subcat.append(('subcat6', 'Subcategory 6', ''))
  92.  
  93. elif self.PropEnum == 'cat2':
  94. enum_subcat.append(('subcat1', 'Subcategory 1', ''))
  95. enum_subcat.append(('subcat2', 'Subcategory 2', ''))
  96. enum_subcat.append(('subcat3', 'Subcategory 3', ''))
  97. enum_subcat.append(('subcat4', 'Subcategory 4', ''))
  98.  
  99. else: enum_subcat = []
  100.  
  101. return enum_subcat
  102.  
  103.  
  104. def register():
  105. from bpy.types import Scene
  106. from bpy.props import StringProperty, EnumProperty
  107.  
  108. # Create a new preview collection (only upon register)
  109. pcoll = bpy.utils.previews.new()
  110.  
  111. # This line needs to be uncommented if you install as an addon
  112. pcoll.images_location = os.path.join(os.path.dirname(__file__), "images")
  113.  
  114. # This line is for running as a script. Make sure images are in a folder called images in the same
  115. # location as the Blender file. Comment out if you install as an addon
  116. #pcoll.images_location = bpy.path.abspath('//images')
  117.  
  118. # Enable access to our preview collection outside of this function
  119. preview_collections["thumbnail_previews"] = pcoll
  120.  
  121. # This is an EnumProperty to hold all of the images
  122. # You really can save it anywhere in bpy.types.* Just make sure the location makes sense
  123. bpy.types.Scene.props_thumbnails = EnumProperty(
  124. items = generate_previews
  125. )
  126.  
  127. bpy.types.Scene.PropEnum = EnumProperty(
  128. items = [('Kitchen & Food', 'Kitchen & Food', ''),
  129. ('cat2', 'Category 2', ''),
  130. ('cat3', 'Category 3', ''),
  131. ('cat4', 'Category 4', ''),
  132. ('cat5', 'Category 5', ''),
  133. ('cat6', 'Category 6', '')],
  134. name = "Category",
  135. )
  136.  
  137. bpy.types.Scene.PropEnumSec = EnumProperty(
  138. name = "Subcategory",
  139. items = generate_subcategories
  140. )
  141.  
  142. def unregister():
  143. from bpy.types import WindowManager
  144. for pcoll in preview_collections.values():
  145. bpy.utils.previews.remove(pcoll)
  146. preview_collections.clear()
  147.  
  148. del bpy.types.Scene.props_thumbnails
  149.  
  150. if __name__ == "__main__":
  151. register()
  152.  
  153. {
  154. "Models": [
  155. [
  156. "Burgundy",
  157. "Kitchen & Food",
  158. "Glassware",
  159. "F_Burgundy.png"
  160. ],
  161. [
  162. "Chardonnay",
  163. "Kitchen & Food",
  164. "Glassware",
  165. "F_Chardonnay.png"
  166. ],
  167. [
  168. "Cabernet",
  169. "Kitchen & Food",
  170. "Glassware",
  171. "F_Cabernet.png"
  172. ],
  173. [
  174. "Champagne",
  175. "Kitchen & Food",
  176. "Glassware",
  177. "F_Champagne.png"
  178. ],
  179. [
  180. "Fries",
  181. "Kitchen & Food",
  182. "Fast Food",
  183. "Fastfood.JPG"
  184. ]
  185. ]
  186. }
Add Comment
Please, Sign In to add comment