Advertisement
Guest User

Untitled

a guest
Oct 5th, 2020
1,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import bpy
  2. from bpy.props import IntProperty, PointerProperty
  3. from bpy.types import Panel, PropertyGroup, Operator
  4. import random
  5.  
  6.  
  7.  
  8. class MyProperties(PropertyGroup):
  9.    
  10.     #This integer will hold the Random Number and then is displayed on the Panel.
  11.     random_number : IntProperty(name= "Random Number", default= 0, min= 0, max= 2)
  12.    
  13.     #This is a list of Text. It will display one of these letters/strings depending
  14.     #on the Random Number.
  15.     text_list = ["A","B","C"]
  16.    
  17.     #This is a list of Icons. Just like the text_list, one icon will be displayed in
  18.     #panel, depending on the Random Number.
  19.     icon_list = ["GHOST_DISABLED", "MATERIAL", "PINNED"]
  20.  
  21.  
  22.  
  23.  
  24.  
  25. class ADDONNAME_PT_main_panel(Panel):
  26.     bl_label = "Main Panel"
  27.     bl_idname = "ADDONNAME_PT_main_panel"
  28.     bl_space_type = 'VIEW_3D'
  29.     bl_region_type = 'UI'
  30.     bl_category = "New Tab"
  31.  
  32.     def draw(self, context):
  33.         layout = self.layout
  34.         scene = context.scene
  35.         mytool = scene.my_tool
  36.        
  37.         #We are displaying the text_list and we are using the random_number
  38.         #to select the index number.
  39.         layout.label(text= mytool.text_list[mytool.random_number])
  40.        
  41.         #Just like the text_list, we are displaying the icon_list and we are
  42.         #also using the random_number to select the index number.
  43.         layout.label(icon= mytool.icon_list[mytool.random_number])
  44.        
  45.         #Here we are displaying the random_number property in our Panel.
  46.         layout.prop(mytool, "random_number")
  47.        
  48.         #This is our Button that will call the Operator
  49.         layout.operator("addonname.myop_operator")
  50.  
  51.  
  52.  
  53.  
  54.  
  55. class ADDONNAME_OT_my_op(Operator):
  56.     bl_label = "Generate Random Number"
  57.     bl_idname = "addonname.myop_operator"
  58.    
  59.    
  60.    
  61.     def execute(self, context):
  62.         scene = context.scene
  63.         mytool = scene.my_tool
  64.        
  65.         #In the Operators execute function, we can generate a random number.
  66.         #For a Random Integer (whole) Number we need to use random.choice
  67.         x = random.choice(range(0, 3))
  68.        
  69.         #Here we are telling our Custom Integer Property
  70.         #(the one in our Property Group), to equal x.
  71.         mytool.random_number = x
  72.        
  73.         #So when the button is pressed, it will generate a Random Number,
  74.         #it will then change the Custom Integer Property to equal what ever
  75.         #the Random Number is.
  76.         return {'FINISHED'}
  77.    
  78.  
  79.  
  80.  
  81. classes = [MyProperties, ADDONNAME_PT_main_panel, ADDONNAME_OT_my_op]
  82.  
  83.  
  84.  
  85. def register():
  86.     for cls in classes:
  87.         bpy.utils.register_class(cls)
  88.        
  89.         bpy.types.Scene.my_tool = PointerProperty(type= MyProperties)
  90.  
  91. def unregister():
  92.     for cls in classes:
  93.         bpy.utils.unregister_class(cls)
  94.         del bpy.types.Scene.my_tool
  95.  
  96.  
  97.  
  98. if __name__ == "__main__":
  99.     register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement