3
$\begingroup$

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 

def addText (context):
    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):
        addText(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()
$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – batFINGER
    Sep 28, 2015 at 21:14
  • $\begingroup$ I suppose you have tested it... Do you think it has to do with I'm working on a Mac right now? $\endgroup$
    – VJ Galaxy
    Sep 28, 2015 at 21:21
  • $\begingroup$ Forget it, it is working perfectly, thank you a lot. $\endgroup$
    – VJ Galaxy
    Sep 28, 2015 at 21:23

1 Answer 1

1
$\begingroup$

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.

If a text block has a .py extension, and register text block is set then its register method is called on startup.

without the if would suffice.

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – VJ Galaxy
    Sep 28, 2015 at 21:03

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