Advertisement
Guest User

CoDEmanX - ui_list_test4.py (see 3D View's T-panel)

a guest
Jun 1st, 2013
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.15 KB | None | 0 0
  1. import bpy
  2. from bpy.props import *
  3.  
  4. sire_items = (
  5.     ('s', "Scale", ""),
  6.     ('i', "Inset", ""),
  7.     ('r', "Rotate", ""),
  8.     ('e', "Extrude", "")
  9. )
  10.  
  11. sire_items_dict = {}
  12. for identifier, label, description in sire_items:
  13.     sire_items_dict[identifier] = label
  14.  
  15.  
  16. class SireStep(bpy.types.PropertyGroup):
  17.     name = StringProperty()
  18.    
  19.     trans = EnumProperty(items=sire_items) # default 's' required?
  20.    
  21.     val = FloatProperty(min=0.001, soft_max=1000, default=1.0, precision=3)
  22.    
  23.     rot = EnumProperty(items=(
  24.         ('cw', 'CW', ''),
  25.         ('ccw', 'CCW', '')),
  26.         name="Rotation" # name required if enum expanded, or labels will be missing
  27.     )
  28.    
  29.     other = EnumProperty(items=(
  30.         ('in', 'In', ''),
  31.         ('out', 'Out', '')),
  32.         name="Other"
  33.     )
  34.    
  35. class Sire(bpy.types.PropertyGroup):
  36.     type_add = EnumProperty(
  37.         items=sire_items,
  38.         name = "some name",
  39.         default = 's'
  40.     )
  41.    
  42.     steps = CollectionProperty(type=SireStep, name="Sire Step")
  43.     index = IntProperty() # min/max/default?
  44.  
  45.  
  46. class SIRE_UL_steplist(bpy.types.UIList):
  47.    
  48.     def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
  49.        
  50.         # draw_item must handle the three layout types... Usually 'DEFAULT' and 'COMPACT' can share the same code.
  51.         if self.layout_type in {'DEFAULT', 'COMPACT'}:
  52.  
  53.             row = layout.row(align=True)
  54.             row.label(item.name)
  55.            
  56.             row.prop(item, "val", text="")
  57.            
  58.             sub = row.row()
  59.             sub.scale_x = 0.4
  60.            
  61.             if item.name == sire_items_dict['r']:
  62.                 sub.prop(item, "rot", expand=True)
  63.             else:
  64.                 sub.prop(item, "other", expand=True) # if not expanded, use text=""
  65.            
  66.         # 'GRID' layout type should be as compact as possible (typically a single icon!).
  67.         elif self.layout_type in {'GRID'}:
  68.             layout.alignment = 'CENTER'
  69.             layout.label("", icon_value=icon)
  70.  
  71.  
  72. # ------ operator 0 ------ add item to collection
  73. class VIEW3D_OT_sire_step_add(bpy.types.Operator):
  74.     bl_idname = 'view3d.sire_step_add'
  75.     bl_label = 'Add'
  76.     bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
  77.  
  78.     @classmethod
  79.     def poll(cls, context):
  80.         return (context.active_object and
  81.                 context.active_object.type == 'MESH' and
  82.                 context.mode == 'EDIT_MESH')
  83.  
  84.     def execute(self, context):
  85.         steps = context.scene.sire.steps
  86.  
  87.         step = steps.add()
  88.         step.name = sire_items_dict[context.scene.sire.type_add]
  89.        
  90.         context.scene.sire.index = len(steps) - 1
  91.        
  92.         return {'FINISHED'}
  93.  
  94.  
  95. # ------ operator 1 ------ remove item from collection
  96. class VIEW3D_OT_sire_step_remove(bpy.types.Operator):
  97.     bl_idname = 'view3d.sire_step_remove'
  98.     bl_label = 'Remove'
  99.     bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
  100.  
  101.     @classmethod
  102.     def poll(cls, context):
  103.         return (context.active_object and
  104.                 context.active_object.type == 'MESH' and
  105.                 context.mode == 'EDIT_MESH' and
  106.                 len(context.scene.sire.steps) > 0)
  107.        
  108.     def execute(self, context):
  109.         idx = context.scene.sire.index
  110.         move_idx = len(context.scene.sire.steps) - 1 == idx
  111.         context.scene.sire.steps.remove(idx)
  112.         if move_idx:
  113.             context.scene.sire.index -= 1
  114.        
  115.         return {'FINISHED'}
  116.    
  117.    
  118. # ------ operator 2 ------ move item up
  119. class VIEW3D_OT_sire_step_moveup(bpy.types.Operator):
  120.     bl_idname = 'view3d.sire_step_moveup'
  121.     bl_label = 'Move up'
  122.     bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
  123.  
  124.     @classmethod
  125.     def poll(cls, context):
  126.         return (context.active_object and
  127.                 context.active_object.type == 'MESH' and
  128.                 context.mode == 'EDIT_MESH' and
  129.                 len(context.scene.sire.steps) > 0 and
  130.                 context.scene.sire.index > 0)
  131.  
  132.     def draw(self, context):
  133.         layout = self.layout
  134.        
  135.     def execute(self, context):
  136.         idx = context.scene.sire.index
  137.         context.scene.sire.steps.move(idx, idx-1)
  138.         context.scene.sire.index -= 1
  139.  
  140.         return {'FINISHED'}
  141.  
  142.  
  143. # ------ operator 3 ------ move item down
  144. class VIEW3D_OT_sire_step_movedown(bpy.types.Operator):
  145.     bl_idname = 'view3d.sire_step_movedown'
  146.     bl_label = 'Move down'
  147.     bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
  148.  
  149.     @classmethod
  150.     def poll(cls, context):
  151.         return (context.active_object and
  152.                 context.active_object.type == 'MESH' and
  153.                 context.mode == 'EDIT_MESH' and
  154.                 len(context.scene.sire.steps) > 0 and
  155.                 context.scene.sire.index < len(context.scene.sire.steps) - 1)
  156.  
  157.     def execute(self, context):
  158.         idx = context.scene.sire.index
  159.         context.scene.sire.steps.move(idx, idx+1)
  160.         context.scene.sire.index += 1
  161.        
  162.         return {'FINISHED'}
  163.  
  164.  
  165. class VIEW3D_PT_sire_steplist(bpy.types.Panel):
  166.     """Tooltip"""
  167.     bl_label = "SIRE"
  168.     bl_idname = "VIEW3D_PT_sire_steplist"
  169.     bl_space_type = 'VIEW_3D'
  170.     bl_region_type = 'TOOLS'
  171.     bl_context = "mesh_edit"
  172.  
  173.     def draw(self, context):
  174.         layout = self.layout
  175.  
  176.         layout.prop(context.scene.sire, "type_add", expand=True)
  177.  
  178.         row = layout.row()
  179.         row.template_list("SIRE_UL_steplist", "", context.scene.sire, "steps", context.scene.sire, "index", rows=4, maxrows=10)
  180.        
  181.         col = row.column(align=True)
  182.         col.operator("view3d.sire_step_add", text="", icon="ZOOMIN")
  183.         col.operator("view3d.sire_step_remove", text="", icon="ZOOMOUT")
  184.        
  185.         col.separator()
  186.         col.operator('view3d.sire_step_moveup', text='', icon='TRIA_UP')
  187.         col.operator('view3d.sire_step_movedown', text='', icon='TRIA_DOWN')
  188.            
  189.  
  190. def register():
  191.     bpy.utils.register_module(__name__)
  192.     bpy.types.Scene.sire = PointerProperty(type=Sire)
  193.  
  194. def unregister():
  195.     bpy.utils.unregister_module(__name__)
  196.     del bpy.types.Scene.sire
  197.  
  198.  
  199. if __name__ == "__main__":
  200.     register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement