I'm trying to import a stl file manually via python. My code below is pretty straight forward, but I'm struggling with the following line. I'm not sure about the syntax and format of the arguments:
bpy.ops.import_mesh.stl(filepath=???, files=???)
The current state of my operator:
import bpy
class FILE_OT_import_stl(bpy.types.Operator):
bl_idname = "file.import_model_stl"
bl_label = "Import Model STL"
bl_options = {'REGISTER', 'UNDO'} # Must have REGISTER!
def execute(self, context):
#my_filepath = 'K:/PQJ/Mold Generator/Blender test/'
#filepath = '//898L-OutputModel-SLA[]_09'
#print(filepath)
all_files=[bpy.types.Scene.import_path]
#print(file[0])
bpy.ops.import_mesh.stl(filepath=my_filepath, files=all_files)
return {'FINISHED'}
class FILE_PANEL_PT_import_export(bpy.types.Panel):
bl_label = "Import/Export"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
# bl_context = "object" # object vs edit mode
def draw(self, context):
layout = self.layout
layout.label(text="Import")
layout.prop(context.scene, "import_path")
row = layout.row()
row.operator("file.import_model_stl")
classes = (
FILE_OT_import_stl,
FILE_PANEL_PT_import_export
)
def register():
for cls in classes:
bpy.types.Scene.import_path = bpy.props.StringProperty(name='Import path', subtype='FILE_PATH')
bpy.utils.register_class(cls)
def unregister():
del bpy.types.Scene.import_path
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()
Q: How to import a certain STL file using the file browser (and add a custom material)?
bpy.ops.import_mesh.stl('INVOKE_DEFAULT')
or uselayout.operator("import_mesh.stl", text="Import STL", icon='FILE_NEW')
in your draw method if you want to import 1 file. Have a look into the Operator File Import template (magic is done by its ImportHelper class). Related: How to use the File Browser's (with ImportHelper) execute function?, batch import example: How to batch import Wavefront OBJ files?. Do you'd like to import 1 or a few files? $\endgroup$