# How to auto-run a simple script?

I'm trying to auto-load a script when the user opens the .blend file (I want the user to have a specific panel available when he opens the .blend file). In this case a simple panel to modify the text. I have already activated the "Auto Run Python Scripts" and the "Register" checkbox, but it is not working.

Why is this not working?

import bpy

bpy.ops.object.editmode_toggle()
bpy.ops.font.delete(type='ALL')
bpy.ops.font.text_insert(text = context.scene.my_string_prop)
bpy.ops.object.editmode_toggle()

class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"

@classmethod
def poll(cls, context):
return context.active_object is not None

def execute(self, context):
return {'FINISHED'}

class Title (bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "TEXT CONTENT"
bl_idname = "OBJECT_PT_title"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "data"

def draw(self, context):
layout = self.layout

obj = context.scene

row = layout.row()
row.prop(obj,"my_string_prop")

row = layout.row()
row.operator("object.simple_operator" , text = "Insert Text")

def register():
bpy.utils.register_class(SimpleOperator)
bpy.utils.register_class(Title)
bpy.types.Scene.my_string_prop = bpy.props.StringProperty \
(
name = "3D Text",
description = "Insert your text here",
default = "TEXT HERE"
)

def unregister():
bpy.utils.unregister_class(SimpleOperator)
bpy.utils.unregister_class(Title)
del bpy.types.Scene.my_string_prop

register()

• The blender text area is handy to make code blocks for the markup here. Select all and press tab will indent all, copy, then shift tab will unindent. – batFINGER Sep 28 '15 at 20:59
• I formatted your script & also put in the only the register method, meant to change it back to if name == main .. anyway it creates the panel on startup. – batFINGER Sep 28 '15 at 21:14
• I suppose you have tested it... Do you think it has to do with I'm working on a Mac right now? – VJ Galaxy Sep 28 '15 at 21:21
• Forget it, it is working perfectly, thank you a lot. – VJ Galaxy Sep 28 '15 at 21:23

If the file is being run as a registered text block then

The text block's name must have a .py extension.

if __name__ == "__main__"


won't be true, it takes the module name, eg for test.py

print(__name__)


will print test, whereas when run from the text editor will print

__main__


which is handy for testing addon scripts.

For a registered text block, perhaps just having

register()


without the if would suffice.

• I have tried that one too. However when I re-open the file I don't see the custom panel. By the way, thank you for the previous explanation, that explains why with only register() the run script didn't work. – VJ Galaxy Sep 28 '15 at 21:03