0
$\begingroup$

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")
    bpy.ops.mesh.primitive_monkey_add()

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

from bpy.app.handlers import persistent

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

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

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 !

$\endgroup$
  • 1
    $\begingroup$ Explicitly executing like this can hardly be called "automatic". $\endgroup$ – dr. Sybren Jul 8 '17 at 14:09
  • $\begingroup$ 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) $\endgroup$ – Another Tux User Jul 8 '17 at 14:13

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.