Adam's Lair Forum

[PLUGIN] Python Scripting
Page 1 of 1

Author:  RockyTV [ 2017/04/15, 19:55 ]
Post subject:  [PLUGIN] Python Scripting

I decided to write a simple plugin, just for fun, based off skoggy's Lua Scripting plugin for Duality. I thought "why not add Python scripting support as well?", and this is the result.

It's available on NuGet, and it's split into 2 plugins:
- The Editor plugin, which adds support for adding/removing/editing .py files
- The Core plugin, which as the name suggests, adds all the core functionality of the plugin (execution engine, etc)

It's pretty straight forward, I've also included some notes in the default script file that is generated when you create a new script.

Example script:
# You can access the parent GameObject by calling `game_object`.
# To use Duality classes, you must first import them:
#    from Duality import Vector2
# By using `import Duality`, you must use qualified names:
#    import Duality
#    Duality.Log.Game.Write('Hello, world!')
# All code must be inside a class called `PyModule`.
# Code outside the class scope will be executed as well,
# except that you won't be able to access functions nor variables.

import Duality
from Duality.Component import InitContext
class PyModule:
    def __init__(self):

    # Called when a Component is initializing.
    # `context`: what kind of initialization is being performed on the Component.
    def start(self, context):
        if context == InitContext.Activate:

    # Called once per frame in order to update the Component.
    # `delta`: time the last frame took, in milliseconds, with time scale applied.
    def update(self, delta):
        print('Time last frame took: %d ms' % delta)

Let me know if you need any help or if you have any feedback.

Core Plugin
Editor Plugin
Source code

Author:  RockyTV [ 2017/05/26, 19:54 ]
Post subject:  Re: [PLUGIN] Python Scripting

A new version was released:
  • Added support for ICmpRenderer interface
  • Fixed a rare bug that would cause the plugin to crash if the execution engine wasn't initialized

To implement the ICmpRenderer interface on your Python script, you must add the following methods: draw(self, device) and is_visible(self, device). Example script:

import Duality

from Duality import Vector2, Vector3, DualityApp
from Duality.Drawing import Canvas, VisibilityFlag, ColorRgba

class PyModule:
    def __init__(self):

    def bound_radius(self):
        return 1.37
    def start(self, context):
        if context == Duality.Component.InitContext.Activate: print('Activated.')
    def update(self, delta):
    def draw(self, device):
        canvas = Canvas(device)

        canvas.FillRect(100, 100, 0, 250, 50)
        canvas.State.ColorTint = ColorRgba.Black
        canvas.DrawText("Hello, world!", 105, 105)
        canvas.DrawText("Welcome to IronPython!", 105, 120)

        if (DualityApp.Mouse.IsAvailable):
            canvas.State.ColorTint = ColorRgba.White
            canvas.FillCircle(DualityApp.Mouse.X, DualityApp.Mouse.Y, 2)

    def is_visible(self, device):
        return (device.VisibilityMask & VisibilityFlag.AllGroups) != VisibilityFlag.None and (device.VisibilityMask & VisibilityFlag.ScreenOverlay) != VisibilityFlag.None

Page 1 of 1 All times are UTC + 1 hour [ DST ]
Powered by phpBB® Forum Software © phpBB Group