Skip to content

Game Loop

The previous chapter described the custom_ui and imports methods. This chapter will lay out the game loop. We start with the template code supplied by OmniStep:

# --- import modules in the 'imports' method below ---

class CustomUserScript(UserScriptBase):
    def custom_ui(self):
        self.add_property('my_string', 'STRING')

    def imports(self):
        self.local_import('import bpy')
        self.local_import('from mathutils import Vector, Quaternion, Matrix')

    def start(self, context, data):
        pass

    def update(self, context, data):
        if data.input.action1 == InputState.DOWN:
            self.display_message(str(self.my_string))

    def done(self, context, canceled, data):
        pass

Start

The start method is called once when the OmniStep operator begins running. This is where you can initialize variables, call methods, and define private variables. The data class passed to this method is described in the next chapter

    def start(self, context, data):
        self.my_local_var = 123.4

Update

The update method is called every frame. This method corresponds to the viewport update, not the Blender timeline. In this template, when the user presses the action1 input ( Left Mouse by default), it displays a message in the bottom of the viewport with the content of the variable my_string.

    def update(self, context, data):
        if data.input.action1 == InputState.DOWN:
            self.display_message(str(self.my_string))

Late Update

There is an additional optional method called late_update, which is called after the player and the animation data is updated. This can be useful for correcting elements that 'lag' behind the player for a single frame.

    def late_update(self, context, data):
        # do things

Here is an excerpt of the OmniStep update loop showing the positions of the update and late_update function:

if event.type == "TIMER":
    self.update_scene(context)
    self.input.process_mousemove(event, False)
    self.input.update_direction()

    self.userscript.update()
    self.player.update()
    self.animation.update()
    self.animation.update_timeline()
    self.userscript.late_update()

    self.input.clear_triggers()
    context.area.tag_redraw()

Done

The done method is called once when you end or abort the operator. The done method has an additional parameter: canceled, a boolean that indicates whether the user pressed Esc or Enter.

def done(self, context, canceled, data):
    if canceled:
        # revert changes
    else:
        # apply changes