1
$\begingroup$

I'm new to Blender and Python, but have programming experience.

I'm writing a script that iterates through all the .stl files in the current directory (current being where the script is), does a few things, then exports them as .fbx files.

When running it from the Scripting screen in Blender, it works great, but I want to run it from the command line.

When I do that it hits an error on this line that grabs the directory where the script resides:

workDir = os.path.dirname(bpy.context.space_data.text.filepath)

The error is:

AttributeError: 'NoneType' object has no attribute 'text'

Why does this work via the GUI and not the command line? Is there a different command I should be using?

Thanks

$\endgroup$
3
  • 1
    $\begingroup$ When you run from the text editor, the context.space_data.type is 'TEXT_EDITOR' ie it is the context space. When you run from CL the context space is None, hence your error. What about something like bpy.path.abspath(bpy.data.texts['Text'].filepath) However prob better blender.stackexchange.com/questions/6817/… and pass the path via a command line argument. $\endgroup$
    – batFINGER
    Commented May 17, 2016 at 15:52
  • $\begingroup$ bpy.path.abspath(bpy.data.texts['Text'].filepath) gives me the error: "KeyError: 'bpy_prop_collection[key]: key "Text" not found'" Passing it as a command line arg may be what I do, but I'd hoped to be able to use the script from the command line or the gui. $\endgroup$ Commented May 19, 2016 at 14:17
  • 2
    $\begingroup$ "Text" needs to be the name of your text block. Passing the path via the command line would be a better solution AFAIC than having the script in same folder as stl files and also having script opened in blender text editor. $\endgroup$
    – batFINGER
    Commented May 19, 2016 at 14:19

1 Answer 1

-1
$\begingroup$

In the text editor window the [filepath] refers to file location where you save the script. In the console window there is no such structure.

import bpy
import datetime

print(datetime.datetime.today())
print(bpy.context.space_data.text.filepath)
print(bpy.data.filepath)

The above was executed in the Script window. You can compare the results.

$\endgroup$
2
  • $\begingroup$ bpy.data.filepath gives me no output either in the GUI or via command line. $\endgroup$ Commented May 19, 2016 at 14:19
  • 2
    $\begingroup$ The file needs to be saved to have a bpy.data.filepath $\endgroup$
    – batFINGER
    Commented Feb 15, 2017 at 5:17

You must log in to answer this question.

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