autoexec python code / script doesn't seem to work

Using Blender v2.78, auto executing python code / script doesn't seem to work no matter how you're trying to do that.

Following explanations from Blender manual - Scripting & Security

1/ Setting auto execution manually should be possible when Register is checked in the text editor containing your python code.

NOT working : script does not run automatically (even if you also check Auto Execution in User Preferences)

2/ Setting auto execution from command line should be possible when -y (or --enable-autoexec) is provided as argument.

NOT working : this parameter should override User preferences (and factory startup by the way, see : Auto-run scripts security options)

Here is the dummy code I put in an empty blender file to do very basic testing (trying to "see" startup process using different ways ... but since script doesn't seem to be started, app handler is also probably not called):

import bpy

def onStartup():
print("[DEBUG] doing something on startup")

print("[INFOS] Blender v" + bpy.app.version_string)
onStartup()

from bpy.app.handlers import persistent

@persistent
print("[DEBUG] handler called AFTER blender file is loaded (" +     bpy.data.filepath + ")")
onStartup()


• Related Have you given your textblock name the required .py extension? – batFINGER Jul 8 '17 at 14:05
• Works fine when I copy-paste your code into a blendfile and name the text block autoruntest.py. – dr. Sybren Jul 8 '17 at 14:08
• Was actually reading your answer from another post ... and yep it's another way to do it ! Thanks for your help – Another Tux User Jul 8 '17 at 14:09
• Thank you both for your help. It's a shame the online manual is not providing this simple precision ! – Another Tux User Jul 8 '17 at 14:16

Found a solution !

Ages ago (before we were relying only on Internet), I was told "If you try searching for some explanation start with man man and dig further from there (yep, I was surrounded by prehistorical nerds)". So an old reflex just came back, I've done an old fashion man blender from my terminal and found the following option :

--python-text <name>
Run the given Python script text block


Since your text editor creates a block called Text by default, you just need to specify this block name ... and it works as expected !

While it shouldn't, the online manual seems, somehow, providing less information than the one available locally !

So, execution of random python code can be executed automatically with the following command :

blender -y yourfile.blend --python-text Text

or

blender --enable-autoexec yourfile.blend --python-text Text

where Text is the name / label or your text block (you can get it from the header of your text editor view)

As pointed by batFINGER and dr. Sybren, another easy way is to modify the name generated by default when you create a new text block, by adding ".py" extension to it !

• Explicitly executing like this can hardly be called "automatic". – dr. Sybren Jul 8 '17 at 14:09
• Now, it's fully automatic (can be put in scripted environment) for me, allowing me to run blender in my headless server for some 3D printing automation tasks (programmatic modeling) – Another Tux User Jul 8 '17 at 14:13