0

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)?

brockmann
  • 12,613
  • 4
  • 50
  • 93
DrDress
  • 563
  • 4
  • 19

1 Answers1

2

You can just add the built-in operator import_mesh.stl to your draw method of your panel which will open up the File Browser as usual and allows the user to select one .stl file:

enter image description here

import bpy

class OBJECT_PT_CustomPanel(bpy.types.Panel): bl_label = "My Panel" bl_idname = "OBJECT_PT_custom_panel" bl_space_type = "VIEW_3D"
bl_region_type = "UI" bl_category = "Tools" bl_context = "objectmode"

def draw(self, context):
    layout = self.layout
    #layout.operator_context = 'INVOKE_DEFAULT' #'INVOKE_AREA'
    layout.operator("import_mesh.stl", text="New", icon='FILE_NEW')

def register(): bpy.utils.register_class(OBJECT_PT_CustomPanel)

def unregister(): bpy.utils.unregister_class(OBJECT_PT_CustomPanel)

if name == "main": register()

Related: How to call a confirmation dialog box?


In case you want to import and modify the .stl object for whatever reason, have a look into the Operator File Import template (operator_file_import.py) that comes with Blender.

enter image description here

Demo operator based on the template, allows to import the file, print the object reference and assigns a new material to the incoming object in one go:

# ImportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator

class ImportSomeData(Operator, ImportHelper): """This appears in the tooltip of the operator and in the generated docs""" bl_idname = "import_test.some_data" bl_label = "Import Some Stl File" bl_options = {'REGISTER', 'UNDO'}

# ImportHelper mixin class uses this
filename_ext = ".stl"
filter_glob: bpy.props.StringProperty(
            default="*.stl",
            options={'HIDDEN'})

def execute(self, context):
    if self.filepath:
        bpy.ops.import_mesh.stl(filepath=self.filepath)
        # Print the imported object reference
        print ("Imported object:", context.object)
        # Create a material
        mat = bpy.data.materials.new(name="STLMaterial")
        # Append the material to the last slot
        context.object.data.materials.append(mat)

    return {'FINISHED'}

Further reading:

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • Yeah. The 'INVOKE_DEFAULT' works like a charm. But I need to change the name of the object right after import (and maybe a few other things). This only imports (like in the menu) – DrDress Nov 13 '20 at 12:42
  • I have seen the template and it just says: "bpy.ops.import_mesh.stl(filepath=self.filepath)". I can't find where "self.filepath" is set. – DrDress Nov 13 '20 at 12:47
  • Comes from the ImportHelper class, see my demo code. – brockmann Feb 22 '21 at 10:06