3
$\begingroup$

I am trying to load a couple of simple python scripts on startup for later use. I don't want to run the scripts on startup, I only want them available in the Text Editor so that I can quickly run them on demand, rather than search them out individually. I have a simple script that loads 2 scripts from a file path. It works if I run the script from the text editor or from the Python console, but the script will not run if I try to load it on startup. This is on Mac OS. Here is the script...

import bpy 
bpy.ops.text.open(filepath="/Users/my_name/Documents/Blender/scripts/modules/Script1.py")
bpy.ops.text.open(filepath="/Users/my_name/Documents/Blender/scripts/modules/Script2.py")

This script is in my startup scripts directory, and does not execute but errors on the first line after the import bpy. I want the 2 scripts in the file paths to appear in the Text Editor after Blender loads. I can run these 2 commands in the Python console, or run this script from the Text Editor, and the scripts will load in the Text Editor. But they will not load if I try to run this code at startup.

What is different when running this code on startup, from running it after startup?

$\endgroup$
  • $\begingroup$ How do you load the script on startup? $\endgroup$ – Justas Jun 28 at 19:27
  • $\begingroup$ I placed the script in my User path startup scripts directory. Its the default scripts path in /User/Library/Application Support/ . So maybe its being run too early in the startup process? Should I place it in the alternate user defined startup path? $\endgroup$ – zippy Jun 28 at 19:55
  • $\begingroup$ And what is the error that you get? $\endgroup$ – Justas Jun 28 at 20:49
  • $\begingroup$ No meaningful error message. In the startup Terminal it just prints.. File "/Users/my_name/Library/Application Support/Blender/2.80/scripts/startup/import_scripts.py", line 7, in <module> bpy.ops.text.open(filepath="/Users/my_name/Documents/Blender/scripts/modules/Camera_mover.py"). It just identifies the first line in the script, but doesn't indicate what the specific problem is. $\endgroup$ – zippy Jun 28 at 21:15
  • $\begingroup$ Try loading your script file from the command line: blender --python path/to/import_scripts.py $\endgroup$ – Justas Jun 29 at 18:20
3
$\begingroup$

Ok, I found a solution for this problem. Thanks to all for providing answers. I posed this as a scripting question since I could see no other way of solving my initial problem. The problem is creating a library of often used scripts that are immediately available every time I open Blender. This is to emulate Maya's Tool Shelf in which the user can save scripts as buttons in the UI.

The solution is to open a script in Blender's Text Editor - this creates a text data block for the scene, which is saved with the scene. Do this on the fresh Default Blender scene to open scripts in the default New Scene, then re-save the scene as Default. If you want specific scripts available for a specific scene, just open those scripts as data blocks for that scene and save it.

This is not quite a decent solution, since the entire script is saved within the blender file, which could add up to a lot of extra data, plus it exposes the script to unintentional changes. A better method would be to save each script path in a run_script command as an individual text object.

import bpy

path = "/Users/user_name/Documents/Blender/scripts/modules/script_name.py" bpy.ops.script.python_file_run(filepath=path)

Then you only save these few lines in the Blender scene file and run it from the Text Editor, and the script is isolated from changes or prying eyes. It just won't work if the blender file is opened from within a different machine or file system. Its just a convenient hack until a more elegant UI solution is invented by someone.

$\endgroup$
  • 1
    $\begingroup$ nice workaround the initial issue $\endgroup$ – Justas Jun 29 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.