4
$\begingroup$

I'm writing an operator for which it is crucial that every time it gets called all the properties are set to their default values.

The ops.object.delete has 1 param. use_global. It is always false, no matter what the user put in last time it got called. I would like to replicate exactly that.

(For example: I want to modify delta scale. The default val for my_factor is 1, so nothing happens until the user alters that number. If the user calls the operator again, it will have stored whatever number the user has used last, however I want it to always start with 1.)

$\endgroup$
1
  • 1
    $\begingroup$ if someone wants an answer adding a condition, like if the operator is running on a new object -> default value, else previous value, I posted an answer there. because the moderator thought it was off of topic. but thks for the question it was exactly what I needed. and just adding a condition to this... blenderartists.org/t/… $\endgroup$ Aug 17, 2021 at 14:43

2 Answers 2

9
$\begingroup$

Add 'SKIP_SAVE' to the blender property option set.

When defining properties for the operator, any with 'SKIP_SAVE' in their property definition options set, will (like the name suggests) not be saved, and revert to their default value the next time the operator is invoked.

    scale_factor = bpy.props.FloatProperty(name="Scale Factor", 
            default=1,
            options={'SKIP_SAVE'})
$\endgroup$
1
  • $\begingroup$ Also a nice approach, also in my case simpler. Thank you. $\endgroup$ Nov 10, 2017 at 20:42
2
$\begingroup$

Add an invoke method to your operator and reset your property.

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    bl_options = {'REGISTER', 'UNDO'}

    d_loc = bpy.props.FloatProperty(name="Delta Location", default=1)

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

    def execute(self, context):        
        delta_vector = (self.d_loc, self.d_loc, self.d_loc)
        context.active_object.delta_location = delta_vector
        return {'FINISHED'}

    def invoke(self, context, event):
        self.d_loc = 1.0
        return self.execute(context)

def register():
    bpy.utils.register_class(SimpleOperator)

def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

    bpy.ops.object.simple_operator() # test call

Example based on Python > Templates > Operator Simple.

What do operator methods do?

$\endgroup$
1
  • $\begingroup$ Thank you very much, works like a charm. Just to be clear invoke() is what happens the 1. time you execute the operator and execute is what happens everytime you change a value? $\endgroup$ Nov 10, 2017 at 9:51

You must log in to answer this question.

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