2
$\begingroup$

First time here! So, I'm trying to create a python script that uses bpy (This script is not being used from within blender, but rather bpy is being called from python as a module), and in this script I am trying to import a mesh into the scene. However, I cannot figure out the correct usage of "bpy.ops.import_mesh.stl". I have the following block of code:

print("Importing mesh...")
bpy.ops.import_mesh.stl(filepath="Meshes/cm.stl")
print("Mesh imported!")

But when I try to run this code, this is what I see in the terminal:

Importing mesh...
Traceback (most recent call last):
  File "C:\Users\Browser\Desktop\blendTest\blend_mesh_test.py", line 220, in <module>
    import_stl()
  File "C:\Users\Browser\Desktop\blendTest\blend_mesh_test.py", line 11, in import_stl
    bpy.ops.import_mesh.stl(filepath="Meshes/mesh.stl")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Browser\Desktop\blendTest\myEnv\Lib\site-packages\bpy\4.3\scripts\modules\bpy\ops.py", line 109, in __call__
    ret = _op_call(self.idname_py(), kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Calling operator "bpy.ops.import_mesh.stl" error, could not be found

In case it was because I was entering the filepath incorrectly, I also tried with the absolute file path:

print("Importing mesh...")
bpy.ops.import_mesh.stl(filepath="c:/Users/Browser/Desktop/blendTest/Meshes/cm.stl")
print("Mesh imported!")

But that just comes up with the same error:

Importing mesh...
Traceback (most recent call last):
  File "C:\Users\Browser\Desktop\blendTest\blend_mesh_test.py", line 220, in <module>
    import_stl()
  File "C:\Users\Browser\Desktop\blendTest\blend_mesh_test.py", line 11, in import_stl
    bpy.ops.import_mesh.stl(filepath="c:/Users/Browser/Desktop/blendTest/Meshes/cm.stl")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Browser\Desktop\blendTest\myEnv\Lib\site-packages\bpy\4.3\scripts\modules\bpy\ops.py", line 109, in __call__
    ret = _op_call(self.idname_py(), kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Calling operator "bpy.ops.import_mesh.stl" error, could not be found

It doesn't seem like I'm calling the function correctly, and I have no idea what the problem could be. Would anyone happen to have an idea as to what I might be doing wrong? Thank you so much in advance!

New contributor
Syl is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
5
  • $\begingroup$ Hello I'm not sure what you mean by This script is not being used from within blender, but rather bpy is being called from python as a module. How do you call the script then if not from within the Text Editor of Blender? Do you execute from command line like this? blender myblendfile --python myscript.py like this? Where the code is inside myscript.py or how do you execute your script then if not within Blender? Or is it called when you run an operator, perhaps from the File > Import menu? $\endgroup$
    – Harry McKenzie
    Commented Jul 10 at 4:59
  • $\begingroup$ It looks like the STL addon is not enabled in your session. If you have one of the more recent versions I suggest you rather use the (now builtin) operator bpy.ops.wm.stl_import : docs.blender.org/api/current/… $\endgroup$
    – Gorgious
    Commented Jul 10 at 9:16
  • $\begingroup$ @HarryMcKenzie , I complied Blender myself using the instructions on this page: docs.blender.org/api/3.4/info_advanced_blender_as_bpy.html This allows me to use bpy from a python script I'm creating in visual studio and running from the terminal, without having Blender open or running from the text editor of Blender. $\endgroup$
    – Syl
    Commented Jul 10 at 11:15
  • $\begingroup$ @Gorgious this works perfectly, thank you! I'll create an answer with your solution. $\endgroup$
    – Syl
    Commented Jul 10 at 11:23
  • $\begingroup$ Happy to help. Cheers for posting the answer $\endgroup$
    – Gorgious
    Commented Jul 10 at 15:10

1 Answer 1

3
$\begingroup$

Gorgious provided me with the solution in a comment beneath my question. The answer was to replace my function with a different operator, bpy.ops.wm.stl_import from this part of the docs: https://docs.blender.org/api/current/bpy.ops.wm.html#bpy.ops.wm.stl_import. My code now looks like the following:

print("Importing mesh...")
bpy.ops.wm.stl_import(filepath="c:/Users/Browser/Desktop/blendTest/Meshes/cm.stl")
print("Mesh imported!")

Which gives me the following output in the terminal:

Importing mesh...
Timer 'STL Import' took 11.92 ms
Mesh imported!

And I can confirm does in fact import my STL. Thank you, Gorgious!

New contributor
Syl is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .