poll() as a python '@staticmethod' was too limiting and didnt allow useful base class...
[blender.git] / release / scripts / templates / operator.py
1 import bpy
2
3 def write_some_data(context, filepath, use_some_setting):
4     print("running write_some_data...")
5     pass
6
7 from bpy.props import *
8
9 class ExportSomeData(bpy.types.Operator):
10     '''This appiers in the tooltip of the operator and in the generated docs.'''
11     bl_idname = "export.some_data" # this is important since its how bpy.ops.export.some_data is constructed
12     bl_label = "Export Some Data"
13
14     # List of operator properties, the attributes will be assigned
15     # to the class instance from the operator settings before calling.
16
17     # TODO, add better example props
18     filepath = StringProperty(name="File Path", description="File path used for exporting the PLY file", maxlen= 1024, default= "")
19     use_setting = BoolProperty(name="Example Boolean", description="Example Tooltip", default= True)
20
21     type = bpy.props.EnumProperty(items=(('OPT_A', "First Option", "Description one"), ('OPT_B', "Second Option", "Description two.")),
22                         name="Example Enum",
23                         description="Choose between two items",
24                         default='OPT_A')
25
26     @classmethod
27     def poll(cls, context):
28         return context.active_object != None
29
30     def execute(self, context):
31
32         # # Bug, currently isnt working
33         #if not self.is_property_set("filepath"):
34         #    raise Exception("filename not set")
35
36         write_some_data(self.properties.filepath, context, self.properties.use_setting)
37
38         return {'FINISHED'}
39
40     def invoke(self, context, event):
41         wm = context.manager
42
43         if True:
44             # File selector
45             wm.add_fileselect(self) # will run self.execute()
46             return {'RUNNING_MODAL'}
47         elif True:
48             # search the enum
49             wm.invoke_search_popup(self)
50             return {'RUNNING_MODAL'}
51         elif False:
52             # Redo popup
53             return wm.invoke_props_popup(self, event) #
54         elif False:
55             return self.execute(context)
56
57
58 # Only needed if you want to add into a dynamic menu
59 menu_func = lambda self, context: self.layout.operator("export.some_data", text="Example Exporter...")
60 bpy.types.INFO_MT_file_export.append(menu_func)
61
62 if __name__ == "__main__":
63     bpy.ops.export.some_data('INVOKE_DEFAULT', filepath="/tmp/test.ply")