Change Blender's Preferences Dynamically I have a bit of a tricky request. I am trying to create a dynamic script that changes Blender's user preferences based on the current selection. Basically, I want to change the user preference option, "rotate around selection" when I don't have anything selected. This means I can dynamically change between selections as I work in the viewport. I have gotten the basic functionality down, but I ran into two problems. 

The first issue is that it doesn't automatically run on start up. I have tried putting it into the registered function but to no avail so I have to activate it with a keypress. 

The second issue is what I am really curious about. I have looked all around but it seems the only way to continually look for input is to use a modal operation. I don't want it to affect the speed of the viewport, but i would really like to take advantage of the events in the modal operations. I don't know if there is another way to handle this, but one of the issues i have found it that it stops the script refreshing command (F8). I can turn the script off with F8 and then press the F8 shortcut again but i would really not like to do that. I was wondering if after the process is killed with F8 it can automatically call the refresh script function. I am not entirely sure how to approach this and any help would be greatly appreciated!

import bpy

    class NewPreferenceOperator(bpy.types.Operator):
        bl_idname = "object.modal_timer_operator"
        bl_label = "Modal Timer Operator"

        _timer = None

        def modal(self, context, event):
            if event.type == 'F8':
                return {'CANCELLED'}

            if event.type == 'MIDDLEMOUSE':
                selected = bpy.context.selected_objects
                if not selected:
                    bpy.context.user_preferences.view.use_rotate_around_active = False
                else:
                    bpy.context.user_preferences.view.use_rotate_around_active = True
            return {'PASS_THROUGH'}

        def execute(self, context):
            self._timer = context.window_manager.event_timer_add(0.1, context.window)
            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}

        def cancel(self, context):
            context.window_manager.event_timer_remove(self._timer)

    def register():
        bpy.utils.register_module(__name__)
        wm = bpy.context.window_manager
        km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
        kmi = km.keymap_items.new(NewPreferenceOperator.bl_idname, 'Y', 'PRESS', ctrl=False, shift=False)
        pass

    def unregister():
        bpy.utils.unregister_module(__name__)
        pass

    if __name__ == '__main__':
        register()
When registered to run on startup, it has the name of the module, in the case below ui_panel.py is a registered text.

if __name__ in ["__main__", "ui_panel"]:
    print("Running ", __name__)  # see the diff between reg or run script
    register()


The refresh (aka reload) script operator is, which IIRC is the operator called by hitting F8

bpy.ops.script.reload()


Instead of cancelling with

return {'CANCELLED'}


use

return self.cancel(context)


then append the operator call to your cancel method, (ie after removing the timer),.  

My preferred way of turning on and off modals is using the update method of a boolproperty

def start(self, context):
    if self.bgl_draw_speaker:
        bpy.ops.wm.draw_speaker_vis()

bpy.types.Scene.bgl_draw_speaker = BoolProperty(update=start,
                                                name="Draw Details",                                                    
                                                description="Show BGL Visualiser")


and have if not scene.bge_draw_speaker as a way to terminate the modal op.