Importing .stl Objects in a Python Script

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

neanderslob
Posts: 4
Joined: Mon Oct 22, 2012 12:06 am

Importing .stl Objects in a Python Script

Postby neanderslob » Mon Oct 22, 2012 5:15 am

Hi all,

I'm very new to python scripting in blender, however I've run across an application where I need to do so. I'm trying to write a python script that imports a .stl file, does some other stuff (not important), and then exports it as an x3d file. I'd assume this would be very easy; this functionality exists under the gui File menu, however I can't figure out how to get it to work in a python script. My understanding is that the two functions in the file menu call python scripts of their own located in the scripts/addons/ directory. Any tips as to how I can make this happen?

Thanks in advance! :)

neanderslob
Posts: 4
Joined: Mon Oct 22, 2012 12:06 am

Postby neanderslob » Mon Oct 22, 2012 9:57 pm

Some nice fellow by the name of "Atom" gave me a tip over at blenderartists.org and it helped me clear the whole thing up. I'll post my findings below to help out other hapless noobs like me. I also ran into a general tool that would probably be helpful for anyone trying to write any python script in Blender.

So the code ended up looking like the following:

Code: Select all

bpy.ops.import_mesh.stl(filepath="/home/sam/KAPPA.stl", filter_glob="*.stl", files=[{"name":"KAPPA.stl", "name":"KAPPA.stl"}], directory="/home/sam/")
where /home/sam/ was my home directory. I was able to find this by running Blender in my terminal with debugging on (apparently a great little tool for trying to script something you know how to do in the GUI). For linux users, like myself, just pop open the terminal and enter

Code: Select all

blender -d
. Then preform whatever tasks you want to code and watch the terminal to see what text pops up. It's a bit like watching the "Info" window on scripting mode but it's a lot more explicit.

Cheers!


Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests