Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- from bpy.props import IntProperty, PointerProperty
- from bpy.types import Panel, PropertyGroup, Operator
- import random
- class MyProperties(PropertyGroup):
- #This integer will hold the Random Number and then is displayed on the Panel.
- random_number : IntProperty(name= "Random Number", default= 0, min= 0, max= 2)
- #This is a list of Text. It will display one of these letters/strings depending
- #on the Random Number.
- text_list = ["A","B","C"]
- #This is a list of Icons. Just like the text_list, one icon will be displayed in
- #panel, depending on the Random Number.
- icon_list = ["GHOST_DISABLED", "MATERIAL", "PINNED"]
- class ADDONNAME_PT_main_panel(Panel):
- bl_label = "Main Panel"
- bl_idname = "ADDONNAME_PT_main_panel"
- bl_space_type = 'VIEW_3D'
- bl_region_type = 'UI'
- bl_category = "New Tab"
- def draw(self, context):
- layout = self.layout
- scene = context.scene
- mytool = scene.my_tool
- #We are displaying the text_list and we are using the random_number
- #to select the index number.
- layout.label(text= mytool.text_list[mytool.random_number])
- #Just like the text_list, we are displaying the icon_list and we are
- #also using the random_number to select the index number.
- layout.label(icon= mytool.icon_list[mytool.random_number])
- #Here we are displaying the random_number property in our Panel.
- layout.prop(mytool, "random_number")
- #This is our Button that will call the Operator
- layout.operator("addonname.myop_operator")
- class ADDONNAME_OT_my_op(Operator):
- bl_label = "Generate Random Number"
- bl_idname = "addonname.myop_operator"
- def execute(self, context):
- scene = context.scene
- mytool = scene.my_tool
- #In the Operators execute function, we can generate a random number.
- #For a Random Integer (whole) Number we need to use random.choice
- x = random.choice(range(0, 3))
- #Here we are telling our Custom Integer Property
- #(the one in our Property Group), to equal x.
- mytool.random_number = x
- #So when the button is pressed, it will generate a Random Number,
- #it will then change the Custom Integer Property to equal what ever
- #the Random Number is.
- return {'FINISHED'}
- classes = [MyProperties, ADDONNAME_PT_main_panel, ADDONNAME_OT_my_op]
- def register():
- for cls in classes:
- bpy.utils.register_class(cls)
- bpy.types.Scene.my_tool = PointerProperty(type= MyProperties)
- def unregister():
- for cls in classes:
- bpy.utils.unregister_class(cls)
- del bpy.types.Scene.my_tool
- if __name__ == "__main__":
- register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement