# Run additional code after user has executed and finished an operator

The script https://gist.github.com/zeffii/5671824 creates a polysphere, but allows the user to set how spherical it is and how many subdivisions it has and its radius as parameters.

The problem is the sphere has doubles along all sides as a result of the way the geometry is created. Doubles are no good for sculpting -- and sculpting is the reason for the polysphere.

I would like to know if there is a way to automatically run remove_doubles(threshold=1.0e-5) just once, on the newly created object when the script is finished. (by finished i mean you can no longer adjust parameters)

edit:
I guess my alternative will be to construct the mesh without doubles in the first place, which is going to be a bit of a mind-bender or delete the mesh on each parameter adjustment and remove doubles too on each adjustment.

edit2
I reluctantly went for the delete / create / remove doubles combo, the script does work as desired from the point of view of the user. Script is here

• at each setting change or when the user is happy with the overall thing? – iKlsR May 29 '13 at 18:46
• Can you describe the workflow you intend in more detail? I assume you want quick updates but call remove doubles as a final step only once. – ideasman42 May 29 '13 at 19:12
• @zeffii it's tricky since as far as I know, there isn't a method from bpy.types.Op that cleans up like a destructor and nothing can execute after return. – iKlsR May 29 '13 at 19:27
• @zeffii how about calling it at every modification at the panel? – iKlsR May 29 '13 at 19:33
• @iKlsR, I tried this before posting, it didn't seem to be in the right context to apply the remove_doubles. – zeffii May 29 '13 at 19:40

No, at least operators don't have the notion of a final state.

I can see in some cases you may want to setup a tools options, then perform some action once the options are set.

You could use a pop-up dialog 1 (as image-new does), but then you dont get feedback when changing values.

Some script writers add toggles to their operators that perform some extra calculation, so the user can choose to quickly setup options then press this last, while this isn't in keeping with our intended workflow, it makes some sense if the operator can't give interactive feedback because they're so slow.

There is one way you might be able to do what you want, and that is to create a Modal Operator, see the example in the text editor templates. This way you start a tool which has its own mode (like grab or rotate), you can choose to handle events to adjust the settings, then a key such as Enter can finalize the operation. So this may work in some cases - but modal operators assume a fairly different workflow so it depends a lot on what you do.

• I could try the modal form (I have coded a few of them previously) with a final button to accept. Will report back if I go this way. – zeffii May 29 '13 at 19:37
• I've accepted this, not because it solved my question immediately, but because it offers useful terminology for further investigation. Thanks! – zeffii May 29 '13 at 19:45
• In fact I misunderstood the question, I thought you wanted to run the tool, edit settings (using operator redo), then only remove doubles once at the end. – ideasman42 May 31 '13 at 20:43
• it would have been more optimal if i could remove doubles just once, instead of on every update. so at the time of asking the question, knowing what I did shaped how i formulated it. – zeffii May 31 '13 at 21:22

The solution I arrived at goes like this, not sure if this is a hack or reasonable practice:

def execute(self, context):

# this removes the existing mesh data, when user adjusts slider
if context.object:
bpy.ops.mesh.delete()

# this deals with mesh repopulation
verts, faces = make_polysphere(self)
base = create_mesh_object(context, verts, [], faces, "PolySphere")
context.object.show_wire = True

# perform the final operations on the object.
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.remove_doubles(threshold=1.0e-5)
return {'FINISHED'}


Therefore, perhaps the original question wasn't stated as accurately as possible.

edit: this is buggy, I don't use it. Still trying to figure out a reasonable solution to the remove doubles problem.