Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Created April 17, 2023 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CGArtPython/9e7affc6028ad087e9b71f17ba7bd1f3 to your computer and use it in GitHub Desktop.
Save CGArtPython/9e7affc6028ad087e9b71f17ba7bd1f3 to your computer and use it in GitHub Desktop.
Beginner Blender Python tutorial code for retrieving the script file path when running from Blender's Text Editor, an Add-on, or the Command Line (tutorial video: https://youtu.be/jT2EbqMfKU8)
import bpy
import pathlib
# check if we are running from the Text Editor
if bpy.context.space_data != None and bpy.context.space_data.type == "TEXT_EDITOR":
# get the path to the SAVED TO DISK script when running from blender
print("bpy.context.space_data script_path")
script_path = bpy.context.space_data.text.filepath
else:
print("__file__ script_path")
# __file__ is built-in Python variable that represents the path to the script
script_path = __file__
print(f"script_path -> {script_path}")
script_dir = pathlib.Path(script_path).resolve().parent
print(f"[pathlib] script_dir -> {script_dir}")
# get a path to a file that is next to the script
path_to_file = str(script_dir / "my_materials.blend")
print(f"path_to_file -> {path_to_file}")
@Isiddique313
Copy link

when I tried to run the command in CMD didn't run. I want to know how to import STL files into Blender using Python scripting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment