Advertisement
Guest User

Untitled

a guest
Jul 13th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. import bpy
  2.  
  3.  
  4. def read_some_data(context, filepath, use_some_setting):
  5.     print("running read_some_data...")
  6.     f = open(filepath, 'r', encoding='utf-8')
  7.     data = f.read()
  8.     f.close()
  9.  
  10.     # would normally load the data here
  11.     print(data)
  12.  
  13.     return {'FINISHED'}
  14.  
  15.  
  16. # ImportHelper is a helper class, defines filename and
  17. # invoke() function which calls the file selector.
  18. from bpy_extras.io_utils import ImportHelper
  19. from bpy.props import StringProperty, BoolProperty, EnumProperty
  20. from bpy.types import Operator
  21.  
  22.  
  23. class ImportSomeData(Operator, ImportHelper):
  24.     """This appears in the tooltip of the operator and in the generated docs"""
  25.     bl_idname = "import_test.some_data"  # important since its how bpy.ops.import_test.some_data is constructed
  26.     bl_label = "Import Some Data"
  27.  
  28.     # ImportHelper mixin class uses this
  29.     filename_ext = ".txt"
  30.  
  31.     filter_glob = StringProperty(
  32.             default="*.txt",
  33.             options={'HIDDEN'},
  34.             )
  35.  
  36.     # List of operator properties, the attributes will be assigned
  37.     # to the class instance from the operator settings before calling.
  38.     use_setting = BoolProperty(
  39.             name="Example Boolean",
  40.             description="Example Tooltip",
  41.             default=True,
  42.             )
  43.  
  44.     type = EnumProperty(
  45.             name="Example Enum",
  46.             description="Choose between two items",
  47.             items=(('OPT_A', "First Option", "Description one"),
  48.                    ('OPT_B', "Second Option", "Description two")),
  49.             default='OPT_A',
  50.             )
  51.  
  52.     def execute(self, context):
  53.         return read_some_data(context, self.filepath, self.use_setting)
  54.  
  55.  
  56. # Only needed if you want to add into a dynamic menu
  57. def menu_func_import(self, context):
  58.     self.layout.operator(ImportSomeData.bl_idname, text="Text Import Operator")
  59.  
  60.  
  61. def register():
  62.     bpy.utils.register_class(ImportSomeData)
  63.     bpy.types.INFO_MT_file_import.append(menu_func_import)
  64.  
  65.  
  66. def unregister():
  67.     bpy.utils.unregister_class(ImportSomeData)
  68.     bpy.types.INFO_MT_file_import.remove(menu_func_import)
  69.  
  70.  
  71. if __name__ == "__main__":
  72.     register()
  73.  
  74.     # test call
  75.     bpy.ops.import_test.some_data('INVOKE_DEFAULT')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement