Skip to content

Methods

You can create your objects with Python. For the complex, you create a new class using pd.new_object() and define configurations, object types, etc using the methods of this class, next are presented all methods used for the class returned from pd.new_object("myobjectname").

addmethod_bang

This function will be executed when the object receives a bang.

  • Python Example

    import pd
    import random
    
    def randomNumber():
        return random.randint(0, 100)
    
    
    def py4pdLoadObjects():
        random = pd.new_object("py.random")
        random.addmethod_bang(randomNumber)
        random.add_object()
    
  • PureData Example

    Scores

addmethod_float

  • Python Example

    import pd
    import random
    
    def randomNumber(limit):
        return random.randint(0, limit) 
    
    def py4pdLoadObjects():
        random = pd.new_object("py.floatRandom")
        random.addmethod_float(randomNumber)
        random.add_object()
    
  • PureData Example

    Scores

addmethod_symbol

  • Python Example

    import pd
    
    def splitSymbol(symbol):
        # return a list with all chars separated
        return list(symbol)
    
    def py4pdLoadObjects():
        random = pd.new_object("py.splitSymbol")
        random.addmethod_symbol(splitSymbol)
        random.add_object()
    
  • PureData Example

    Scores

addmethod_list

  • Python Example

    import pd
    
    def listmethod(list):
        newlist = [] 
        for x in list:
            newlist.append(x * x)
        return newlist
    
    def py4pdLoadObjects():
        random = pd.new_object("py.listmultiplier")
        random.addmethod_list(listmethod)
        random.add_object()
    
  • PureData Example

    Scores

addmethod_anything

  • Python Example

    import pd
    
    def anythingmethod(mything):
        return f"Received {mything}"
    
    def py4pdLoadObjects():
        random = pd.new_object("py.anything")
        random.addmethod_anything(anythingmethod)
        random.add_object()
    
  • PureData Example

    Scores

addmethod

  • Python Example

    import pd
    
    def portugues(a, b, c):
        return "Portuguese method"
    
    def english(d, e):
        return "English method"
    
    def german(f):
        return "German method"
    
    def dutch():
        return "Dutch method"    
    
    def py4pdLoadObjects():
        random = pd.new_object("py.methods")
        random.addmethod("casa", portugues)
        random.addmethod("home", english)
        random.addmethod("haus", german)
        random.addmethod("huis", dutch)
        random.add_object()
    
  • PureData Example

    Scores

    Inlets

    Note that as the function with more arguments is portugues (with 3) arguments, the object will have 3 inlets.

add_type

With this function, you can define functions for each data type. For example, a function to execute when the object receive a PyObject int (pointer) another function when receive PyObject float (pointer), and another when receive PyObject list (pointer). You can check the data type of some PyObject using the object py.type avaible with [py4pd -lib py4pd].

  • Python Example

    import pd
    
    def receivedFloat(a):
        return "Received Python Float"
    
    def receivedString(d):
        return "Received Python String"
    
    def receivedList(f):
        return "Received Python List"   
    
    def py4pdLoadObjects():
        random = pd.new_object("py.4types")
        random.addtype("float", receivedFloat)
        random.addtype("str", receivedString)
        random.addtype("list", receivedList)
        random.add_object()
    
  • PureData Example

    Scores

    py.type to get the type

    Use the object py.type to get the name of the type you want.

add_object

Don't be fooled by false class. py4pd still uses procedural language. After configuring your object, you need to use the pd.add_object() function for the object to be available in the PureData patch.

import pd
import random

def randomNumber():
    return random.randint(0, 100)


def py4pdLoadObjects():
    myrandom = pd.new_object("py.random")
    myrandom.addmethod_bang(randomNumber)
    myrandom.add_object() # without this, py.random will not be available in the patch.

Danger

Without random.add_object() py.random will not be available in the patch.