2
$\begingroup$

Is it possible to reload by pressing F8 and restore property to default value? I don't seem to get this working? If there's a way to do so, would it work with EnumProperty?

bpy.types.Scene.my_string_prop = bpy.props.StringProperty \
  (
    name = "Text Field",
    description = "My description",
    default = "some default text here"
  )

In the panel def draw:

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

        #Set TextField
        col = layout.column(align = True)
        col.prop(context.scene, "my_string_prop")
$\endgroup$
  • $\begingroup$ Properties as above, eg scene.prop are "stored" as scene["prop"] once set to something other than default. Remove with scene.property_unset("prop") or del scene["prop"]. $\endgroup$ – batFINGER May 20 '18 at 5:14
  • $\begingroup$ I put these in the unregister(): scene.property_unset("prop") del scene["prop"] still didn't work on F8, did i missed something? $\endgroup$ – adrian li May 20 '18 at 11:50
  • $\begingroup$ When an addon is registered (ie in the register method), it has "restricted context" during which most data cannot be edited. Also your property is called "my_string_prop" so you would use (not in register method) scene.property_unset("my_string_prop") ... and it would have to be set up to be in F8... added an answer. $\endgroup$ – batFINGER May 20 '18 at 13:17
0
$\begingroup$

Make an operator to add functionality before calling another

The text editor > templates > python > Operator Simple template edited (minimally) to reset a user defined "xxxx" property of the scene to its default value. Can either use scene.property_unset("xxx") or simply remove its storage del(scene["xxx"]).

After which the reload script is called. (default op of F8) Edit the shortcut in user preferences to point to new operator.

enter image description here the screen F8 shortcut after changing "script.reload" to "object.simple_operator"

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "My Reload Scripts"

    @classmethod
    def poll(cls, context):
        return True

    def execute(self, context):
        scene = context.scene
        scene.property_unset("xxx")
        #del(scene["xxx"]) # same
        # what F8 calls
        bpy.ops.script.reload()
        return {'FINISHED'}


def register():
    bpy.types.Scene.xxx = bpy.props.IntProperty()
    bpy.utils.register_class(SimpleOperator)

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


if __name__ == "__main__":
    register()
    # test code
    scene = bpy.context.scene
    scene.xxx = 10
    print("before", scene.xxx)
    bpy.ops.object.simple_operator()
    print("after", scene.xxx) 
$\endgroup$
  • $\begingroup$ I have employed your method and it seemed to make sense, however it doesnt seem to restore default bpy.props.StringProperty. I am editing the addon` innit.py` file as is in the text editor window inside blender and press "Run Script" to test things, could this be the problem why F8 wont work? If so, what should be the proper workflow to edit and test code on the fly(i have multiple python files)? $\endgroup$ – adrian li May 22 '18 at 7:30
  • $\begingroup$ I put del bpy.types.Scene.my_string_prop in unregister() and F8 still doesnt revert it to default value? $\endgroup$ – adrian li May 22 '18 at 7:36

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.