Created
April 17, 2023 06:11
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.