Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. bl_info = {
  2.     "name": "crease-set-editor",
  3.     "description": "Add and manage crease sets within Blender",
  4.     "author": "Tom Paul",
  5.     "version": (0,1),
  6.     "blender": (2,80,0),
  7.     "warning": "",
  8.     "tracker_url": "",
  9.     "category": "Modelling"
  10. }
  11.  
  12. import bpy
  13.  
  14. from bpy.props import (IntProperty,
  15.                        BoolProperty,
  16.                        StringProperty,
  17.                        CollectionProperty,
  18.                        PointerProperty)
  19.                        
  20. from bpy.types import (Operator,
  21.                        Panel,
  22.                        PropertyGroup,
  23.                        UIList)
  24.                        
  25.  
  26. class CUSTOM_OT_actions(Operator):
  27.     bl_idname = "custom.list_action"
  28.     bl_label = "List Actions"
  29.     bl_description = "Move items up and down, add and remove"
  30.     bl_options = {'REGISTER'}
  31.    
  32.     action: bpy.props.EnumProperty(
  33.         items=(
  34.             ('UP', "Up", ""),
  35.             ('DOWN', "Down", ""),
  36.             ('REMOVE', "Remove", ""),
  37.             ('ADD', "Add", "")))
  38.            
  39.            
  40.     def invoke(self, context, event):
  41.         scn = context.scene
  42.         idx = scn.custom_index
  43.        
  44.         try:
  45.             item = scn.custom[idx]
  46.         except IndexError:
  47.             pass
  48.         else:
  49.             if self.action == 'DOWN' and idx < len(scn.custom) - 1:
  50.                 item_next = scn.custom[idx+1].name
  51.                 scn.custom.move(idx, idx+1)
  52.                 scn.custom_index += 1
  53.                 info = 'Item "%s" moved to position %d' % (item.name,scn.custom_index + 1)
  54.                 self.report({'INFO'}, info)
  55.                
  56.             elif self.action == 'UP' and idx >= 1:
  57.                 item_prev = scn.custom[idx-1].name
  58.                 scn.custom.move(idx, idx-1)
  59.                 scn.custom_index -= 1
  60.                 info = 'Item "%s" moved to position %d' % (item.name, scn.custom_index + 1)
  61.                 self.report({'INFO'}, info)
  62.                
  63.             elif self.action == 'REMOVE':
  64.                 item = scn.custom[scn.custom_index]
  65.                 item_name = item.name
  66.                 info = '%s removed from scene' % (item_name)
  67.                 scn.custom.remove(idx)
  68.                 if scn.custom_index == 0:
  69.                     scn.custom_index = 0
  70.                 else:
  71.                     scn.custom_index -= 1
  72.                 self.report({'INFO'}, info)
  73.                
  74.         if self.action == 'ADD':
  75.             item = scn.custom.add()
  76.             item.id = len(scn.custom)
  77.             item.name = "Crease %d" % (item.id)
  78.             scn.custom_index = (len(scn.custom)-1)
  79.             info = '%s added to list' % (item.name)
  80.             self.report({'INFO'}, info)
  81.         return {"FINISHED"}
  82.    
  83. class CUSTOM_OT_clearList(Operator):
  84.     bl_idname = "custom.clear_list"
  85.     bl_label = "Clear Crease List"
  86.     bl_description = "Clear all items of the list and remove from scene"
  87.     bl_options = {'INTERNAL'}
  88.    
  89.     @classmethod
  90.     def poll(cls, context):
  91.         return bool(context.scene.custom)
  92.    
  93.     def invoke(self,context,event):
  94.         return context.window_manager.invoke_confirm(self, event)
  95.    
  96.     def execute(self, context):
  97.        
  98.         if bool(context.scene.custom):
  99.             context.scene.custom.clear()
  100.             self.report({'INFO'}, "List Cleared")
  101.         else:
  102.             self.report({'INFO'}, "Nothing to remove")
  103.         return{'FINISHED'}
  104.    
  105. class CUSTOM_UL_items(UIList):
  106.     def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
  107.         if self.layout_type in {'DEFAULT', 'COMPACT'}:
  108.             split = layout.split(factor=1)
  109.             split.label(text = "Crease %d" % (index+1))
  110.     def invoke(self, context, event):
  111.         pass
  112.    
  113.  
  114. class CUSTOM_PT_objectList(Panel):
  115.     bl_idname = 'CREASE_PT_my_panel'
  116.     bl_label = "Custom Crease List"
  117.     bl_space_type = "VIEW_3D"
  118.     bl_region_type = "UI"
  119.     bl_category = "Crease Manager"
  120.    
  121.     def draw(self, context):
  122.         layout = self.layout
  123.         scn = bpy.context.scene
  124.        
  125.         rows = 2
  126.         row = layout.row()
  127.         row.template_list("CUSTOM_UL_items", "custom_def_list", scn, "custom", scn, "custom_index", rows=rows)
  128.        
  129.         col = row.column(align = True)
  130.         col.operator("custom.list_action", icon='ADD', text = "").action = 'ADD'
  131.         col.operator("custom.list_action", icon='REMOVE', text = "").action = 'REMOVE'
  132.         col.separator()
  133.         col.operator("custom.list_action", icon='TRIA_UP', text = "").action = 'UP'
  134.         col.operator("custom.list_action", icon='TRIA_DOWN', text = "").action = 'DOWN'
  135.        
  136.         row = layout.row()
  137.         col = row.column(align=True)
  138.         row = col.row(align=True)
  139.         row.operator("custom.clear_list", icon="X")
  140.        
  141. classes = (
  142.     CUSTOM_OT_actions,
  143.     CUSTOM_OT_clearList,
  144.     CUSTOM_UL_items,
  145.     CUSTOM_PT_objectList
  146. )
  147.  
  148. def register():
  149.     from bpy.utils import register_class
  150.     for cls in classes:
  151.         register_class(cls)
  152.        
  153.     bpy.types.Scene.custom_index = IntProperty()
  154.    
  155. def unregister():
  156.     from bpy.utils import unregister_class
  157.     for cls in reversed(classes):
  158.         unregister_class(cls)
  159.        
  160.     del bpy.types.Scene.custom_index
  161.    
  162. if __name__ == "__main__":
  163.     register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement