From c2645dc4003b1391aba9b387a79a66cff1e63d3e Mon Sep 17 00:00:00 2001 From: Thomas Grill Date: Tue, 22 Oct 2002 23:16:30 +0000 Subject: This commit was generated by cvs2svn to compensate for changes in r189, which included commits to RCS files with non-trunk default branches. svn path=/trunk/; revision=190 --- externals/grill/py/scripts/script.py | 53 +++++++++ externals/grill/py/scripts/sendrecv.py | 173 +++++++++++++++++++++++++++ externals/grill/py/scripts/simple.py | 206 +++++++++++++++++++++++++++++++++ externals/grill/py/scripts/tcltk.py | 77 ++++++++++++ externals/grill/py/scripts/threads.py | 43 +++++++ 5 files changed, 552 insertions(+) create mode 100644 externals/grill/py/scripts/script.py create mode 100644 externals/grill/py/scripts/sendrecv.py create mode 100644 externals/grill/py/scripts/simple.py create mode 100644 externals/grill/py/scripts/tcltk.py create mode 100644 externals/grill/py/scripts/threads.py (limited to 'externals/grill/py/scripts') diff --git a/externals/grill/py/scripts/script.py b/externals/grill/py/scripts/script.py new file mode 100644 index 00000000..4796949c --- /dev/null +++ b/externals/grill/py/scripts/script.py @@ -0,0 +1,53 @@ +# py/pyext - python script objects for PD and MaxMSP +# +# Copyright (c) 2002 Thomas Grill (xovo@gmx.net) +# For information on usage and redistribution, and for a DISCLAIMER OF ALL +# WARRANTIES, see the file, "license.txt," in this distribution. +# + +"""Several functions to show the py script functionality""" + +import sys + +print "Script initialized" + +try: + print "Script arguments: ",sys.argv +except: + print + +def numargs(*args): # variable argument list + """Return the number of arguments""" + return len(args) + +def strlen(arg): + """Return the string length""" + return len(arg) + + +def strcat(*args): + """Concatenate several symbols""" + s = "" + for si in args: + s += str(si) + return s + + +def addall(*args): # variable argument list + s = 0 + for si in args: + s += si + return s + + +def ret1(): + return 1,2,3,4 + + +def ret2(): + return "sd","lk","ki" + + +def ret3(): + return ["sd","lk","ki"] + diff --git a/externals/grill/py/scripts/sendrecv.py b/externals/grill/py/scripts/sendrecv.py new file mode 100644 index 00000000..f31eae2d --- /dev/null +++ b/externals/grill/py/scripts/sendrecv.py @@ -0,0 +1,173 @@ +# py/pyext - python script objects for PD and MaxMSP +# +# Copyright (c) 2002 Thomas Grill (xovo@gmx.net) +# For information on usage and redistribution, and for a DISCLAIMER OF ALL +# WARRANTIES, see the file, "license.txt," in this distribution. +# + +"""This is an example script for the py/pyext object's send/receive functionality. + +You can: +- bind + + +There are several classes exposing py/pyext features: +- ex1: A class receiving messages and sending them out again +- ex2: A class receiving messages and putting them out to an outlet +- ex3: Do some PD scripting + +""" + +import pyext +from time import sleep + +################################################################# + +def recv_gl(arg): + """This is a global receive function, it has no access to class members.""" + print "GLOBAL",arg + +class ex1(pyext._class): + """Example of a class which receives and sends messages + + It has two creation arguments: a receiver and a sender name. + There are no inlets and outlets. + Python functions (one global function, one class method) are bound to PD's or Max/MSP's receive symbols. + The class method sends the received messages out again. + """ + + + # no inlets and outlets + _inlets=0 + _outlets=0 + + recvname="" + sendname="" + + def recv(self,arg): + """This is a class-local receive function, which has access to class members.""" + + # print some stuff + print "CLASS",self.recvname,arg + + # send data to specified send address + self._send(self.sendname,arg) + + + def __init__(self,args): + """Class constructor""" + + # store sender/receiver names + if len(args) >= 1: self.recvname = args[0] + if len(args) >= 2: self.sendname = args[1] + + # bind functions to receiver names + # both are called upon message + self._bind(self.recvname,self.recv) + self._bind(self.recvname,recv_gl) + + + def __del__(self): + """Class destructor""" + + # you can but you don't need to + # unbinding is automatically done at destruction + # you can also comment the _unbind lines + self._unbind(self.recvname,self.recv) + self._unbind(self.recvname,recv_gl) + + pass + + +################################################################# + +class ex2(pyext._class): + """Example of a class which receives a message and forwards it to an outlet + + It has one creation argument: the receiver name. + """ + + + # define inlets and outlets + _inlets=0 + _outlets=1 + + recvname="" + + def recv(self,arg): + """This is a class-local receive function""" + + # send received data to outlet + self._outlet(1,arg) + + + def __init__(self,rname): + """Class constructor""" + + # store receiver names + self.recvname = rname + + # bind function to receiver name + self._bind(self.recvname,self.recv) + + +################################################################# + +from math import sin,cos,pi +from cmath import exp +from random import random,randint + +class ex3(pyext._class): + """Example of a class which does some object manipulation by scripting""" + + + # define inlets and outlets + _inlets=1 + _outlets=0 + + def __init__(self): + """Class constructor""" + + # called scripting method should run on its own thread + self._detach(1) + + + def bang_1(self): + """Do some scripting - PD only!""" + + num = 12 # number of objects + ori = complex(150,150) # origin + rad = 100 # radius + l = range(num) # initialize list + + # make flower + self._tocanvas("obj",ori.real,ori.imag,"bng",20,250,50,0,"empty","yeah","empty",0,-6,64,8,-24198,-1,-1) + for i in range(num): + l[i] = ori+rad*exp(complex(0,i*2*pi/num)) + self._tocanvas("obj",l[i].real,l[i].imag,"bng",15,250,50,0,"empty","yeah"+str(i),"empty",0,-6,64,8,0,-1,-1) + self._tocanvas("connect",2,0,3+i,0) + + # blink + for i in range(10): + self._send("yeah","bang") + sleep(1./(i+1)) + + # move objects around + for i in range(200): + ix = randint(0,num-1) + l[ix] = ori+rad*complex(2*random()-1,2*random()-1) + self._send("yeah"+str(ix),"pos",l[ix].real,l[ix].imag) + sleep(0.02) + + # now delete + # this is not well-done... from time to time an object remains + self._tocanvas("editmode",1) + for i in range(num): + self._tocanvas("mouse",l[i].real,l[i].imag,0,0) + self._tocanvas("cut") + + self._tocanvas("mouse",ori.real+1,ori.imag+1,0,0) + self._tocanvas("cut") + + self._tocanvas("editmode",0) + diff --git a/externals/grill/py/scripts/simple.py b/externals/grill/py/scripts/simple.py new file mode 100644 index 00000000..60cebec7 --- /dev/null +++ b/externals/grill/py/scripts/simple.py @@ -0,0 +1,206 @@ +# py/pyext - python script objects for PD and MaxMSP +# +# Copyright (c) 2002 Thomas Grill (xovo@gmx.net) +# For information on usage and redistribution, and for a DISCLAIMER OF ALL +# WARRANTIES, see the file, "license.txt," in this distribution. +# + +"""This is an example script for the py/pyext object's basic functionality. + +pyext Usage: +- Import pyext + +- Inherit your class from pyext._class + +- Specfiy the number of inlets and outlets: + Use the class members (variables) _inlets and _outlets + If not given they default to 1 + You can also use class methods with the same names to return the respective number + +- Constructors/Destructors + You can specify an __init__ constructor and/or an __del__ destructor. + The constructor will be called with the object's arguments + + e.g. if your PD or MaxMSP object looks like + [pyext script class arg1 arg2 arg3] + + then the __init__(self,args) function will be called with a tuple argument + args = (arg1,arg2,arg3) + With this syntax, you will have to give at least one argument. + By defining the constructor as __init__(self,*args) you can also initialize + the class without arguments. + +- Methods called by pyext + The general format is 'tag_inlet(self,args)' resp. 'tag_inlet(self,*args)': + tag is the PD or MaxMSP message header.. either bang, float, list etc. + inlet is the inlet (starting from 1) from which messages are received. + args is a tuple which corresponds to the content of the message. args can be omitted. + + The inlet index can be omitted. The method name then has the format 'tag_(self,inlet,args)'. + Here, the inlet index is a additional parameter to the method + + You can also set up methods which react on any message. These have the special forms + _anything_inlet(self,args) + or + _anything_(self,inlet,args) + + Please see below for examples. + + Any return values are ignored - use _outlet (see below). + + Generally, you should avoid method_, method_xx forms for your non-pyext class methods. + Identifiers (variables and functions) with leading underscores are reserved for pyext. + +- Send messages to outlets: + Use the inherited _outlet method. + You can either use the form + self._outlet(outlet,arg1,arg2,arg3,arg4) ... where all args are atoms (no sequence types!) + or + self._outlet(outlet,arg) ... where arg is a sequence containing only atoms + +- Use pyext functions and methods: + See the __doc__ strings of the pyext module and the pyext._class base class. + +""" + +import pyext + +################################################################# + +class ex1(pyext._class): + """Example of a simple class which receives messages and prints to the console""" + + # number of inlets and outlets + _inlets=3 + _outlets=0 + + + # methods for first inlet + + def bang_1(self): + print "Bang into first inlet" + + def float_1(self,f): + print "Float ",f," into first inlet" + + def list_1(self,s): + print "List ",s," into first inlet" + + + # methods for second inlet + + def hey_2(self): + print "Tag 'hey' into second inlet" + + def ho_2(self): + print "Tag 'ho' into second inlet" + + def lets_2(self): + print "Tag 'lets' into second inlet" + + def go_2(self): + print "Tag 'go' into second inlet" + + def _anything_2(self,args): + print "Some other message into second inlet:",args + + + # methods for third inlet + + def onearg_3(self,a): + print "Tag 'onearg' into third inlet:",a + + def twoargs_3(self,a): + if len(a) == 2: + print "Tag 'twoargs' into third inlet:",a[0],a[1] + else: + print "Tag 'twoargs': wrong number of arguments" + + def threeargs_3(self,a): + if len(a) == 3: + print "Tag 'threeargs' into third inlet",a[0],a[1],a[2] + else: + print "Tag 'threeargs': wrong number of arguments" + + def varargs_3(self,*args): + # with *args there can be arguments or not + + print "Tag 'varargs' into third inlet",args + + + +################################################################# + +class ex2(pyext._class): + """Example of a simple class which receives messages and writes to outlets""" + + # number of inlets and outlets + _inlets=3 + _outlets=2 + + # methods for all inlets + + def hello_(self,n): + print "Tag 'hello' into inlet",n + + def _anything_(self,n,args): + print "Message into inlet",n,":",args + + + # methods for first inlet + + def float_1(self,f): + self._outlet(2,f) + + # methods for second inlet + + def float_2(self,f): + self._outlet(1,f) + + +################################################################# + +# helper function - determine whether argument is a numeric type +def isNumber(value): + import types + if type(value) in (types.FloatType, types.IntType, types.LongType): + return 1 + else: + return 0 + + +class ex3(pyext._class): + """Example of a simple class doing a typical number addition + + It uses a constructor and a class member as temporary storage. + """ + + # number of inlets and outlets + _inlets=2 + _outlets=1 + + # temporary storage + tmp=0 + + # constructor + def __init__(self,*args): + if len(args) == 1: + if isNumber(args[0]): + self.tmp = args[0] + else: + print "ex3: __init__ has superfluous arguments" + + # methods + + def float_1(self,f): + self._outlet(1,self.tmp+f) + + def float_2(self,f): + self.tmp = f + + # handlers for MaxMSP int type + def int_1(self,f): + self.float_1(f) + + def int_2(self,f): + self.float_2(f) diff --git a/externals/grill/py/scripts/tcltk.py b/externals/grill/py/scripts/tcltk.py new file mode 100644 index 00000000..154a51b1 --- /dev/null +++ b/externals/grill/py/scripts/tcltk.py @@ -0,0 +1,77 @@ +# py/pyext - python script objects for PD and MaxMSP +# +# Copyright (c) 2002 Thomas Grill (xovo@gmx.net) +# For information on usage and redistribution, and for a DISCLAIMER OF ALL +# WARRANTIES, see the file, "license.txt," in this distribution. +# + +"""This is an example script for showing a nonsense tcl/tk application.""" + +import pyext +from Tkinter import * +import random + + +class Application(Frame): + """This is the TK application class""" + + # Button pressed + def say_hi(self): + self.extcl._outlet(1,"hi there, everyone!") + + # Mouse motion over canvas + def evfunc(self, ev): + x = random.uniform(-3,3) + y = random.uniform(-3,3) + self.mcanv.move('group',x,y) + + # Create interface stuff + def createWidgets(self): + self.hi = Button(self) + self.hi["text"] = "Hi!" + self.hi["fg"] = "red" + self.hi["command"] = self.say_hi + self.hi.pack({"side": "left"}) + + self.mcanv = Canvas(self) + self.mcanv.pack({"side": "left"}) + self.mcanv.bind("", self.evfunc) + self.mcanv.create_rectangle(50,50,200,200) + r = self.mcanv.create_rectangle(50,50,200,200) + self.mcanv.addtag_withtag('group',r) + + for i in range(500): + x = random.uniform(50,200) + y = random.uniform(50,200) + l = self.mcanv.create_line(x,y,x+1,y) + self.mcanv.addtag_withtag('group',l) + + # Constructor + def __init__(self,cl): + self.extcl = cl + Frame.__init__(self) + self.pack() + self.createWidgets() + pass + + +# derive class from pyext._class + +class myapp(pyext._class): + """This class demonstrates how a TCL/TK can be openened from within a pyext external""" + + # how many inlets and outlets? + _inlets = 1 + _outlets = 1 + + # Constructor + def __init__(self): + # detach bang method + self._detach(1) + + def bang_1(self): + self._priority(-3) + # display the tcl/tk dialog + app = Application(self) + app.mainloop() + diff --git a/externals/grill/py/scripts/threads.py b/externals/grill/py/scripts/threads.py new file mode 100644 index 00000000..e8486699 --- /dev/null +++ b/externals/grill/py/scripts/threads.py @@ -0,0 +1,43 @@ +# py/pyext - python script objects for PD and MaxMSP +# +# Copyright (c) 2002 Thomas Grill (xovo@gmx.net) +# For information on usage and redistribution, and for a DISCLAIMER OF ALL +# WARRANTIES, see the file, "license.txt," in this distribution. +# + +"""This is an example script for the py/pyext object's threading functionality. + +For threading support pyext exposes several function and variables + +- _detach([0/1]): by enabling thread detaching, threads will run in their own threads +- _priority(prio+-): you can raise or lower the priority of the current thread +- _stop({wait time in ms}): stop all running threads (you can additionally specify a wait time in ms) +- _shouldexit: this is a flag which indicates that the running thread should terminate + +""" + +import pyext +from time import sleep + +################################################################# + +class ex1(pyext._class): + """This is a simple class with one method looping over time.""" + + # number of inlets and outlets + _inlets=2 + _outlets=2 + + sltime=0.2 # sleep time + loops=30 # loops to iterate + + # method for bang to any inlet + def bang_(self,n): + for i in range(30): + # if _shouldexit is true, the thread ought to stop + if self._shouldexit: break + + self._outlet(n,i) + sleep(self.sltime) + + -- cgit v1.2.1