Re: Golly scripts
Posted: March 8th, 2011, 6:34 pm
Would anyone modify the ssstream script to work in Golly 2.2? I would be very thankful.
This should work:12Glider wrote:Would anyone modify the ssstream script to work in Golly 2.2? I would be very thankful.
Code: Select all
# Golly python script.
# Written by PM 2Ring, April 2009
''' Create a spaceship stream of any period.
The pattern is built from the current selection, which is assumed to be a spaceship.
'''
from glife import *
import golly
class ArgError(Exception): pass
def getargs():
''' Get input data '''
prompt = 'Enter period and number of spaceships'
usage = 'Create a stream of the selected spaceship.\n' + prompt + ', separated by spaces.'
#Loop until we get correct args, or are aborted by ESC
while True:
try:
args = golly.getstring(prompt + ':').split()
if args == []:
raise ArgError
if len(args) < 2:
raise ArgError, 'Not enough data.'
#Check that args are valid integers
for s in args:
if not validint(s):
raise ArgError, '[%s] is not a valid integer.' % s
#Convert arguments to integer
args = [int(s) for s in args]
for i in args:
if i<=0:
raise ArgError, 'Data must be > 0, not %d!' % i
except ArgError, s:
g.warn('%s\n\n%s' % (s, usage))
else:
break
return args
def main():
selrect = golly.getselrect()
if len(selrect) == 0: golly.exit("No selection")
ss = pattern(golly.getcells(selrect))
#Get input data
period, num = getargs()
all = pattern()
for i in xrange(1, num):
all += ss[i * period]
all.put()
main()
It doesn't work for me with python 2.5.4...which version of python should I use with the ssship script?Andrew wrote:This should work:12Glider wrote:Would anyone modify the ssstream script to work in Golly 2.2? I would be very thankful.Code: Select all
# Golly python script. # Written by PM 2Ring, April 2009 ''' Create a spaceship stream of any period. The pattern is built from the current selection, which is assumed to be a spaceship. ''' from glife import * import golly class ArgError(Exception): pass def getargs(): ''' Get input data ''' prompt = 'Enter period and number of spaceships' usage = 'Create a stream of the selected spaceship.\n' + prompt + ', separated by spaces.' #Loop until we get correct args, or are aborted by ESC while True: try: args = golly.getstring(prompt + ':').split() if args == []: raise ArgError if len(args) < 2: raise ArgError, 'Not enough data.' #Check that args are valid integers for s in args: if not validint(s): raise ArgError, '[%s] is not a valid integer.' % s #Convert arguments to integer args = [int(s) for s in args] for i in args: if i<=0: raise ArgError, 'Data must be > 0, not %d!' % i except ArgError, s: g.warn('%s\n\n%s' % (s, usage)) else: break return args def main(): selrect = golly.getselrect() if len(selrect) == 0: golly.exit("No selection") ss = pattern(golly.getcells(selrect)) #Get input data period, num = getargs() all = pattern() for i in xrange(1, num): all += ss[i * period] all.put() main()
That doesn't tell me much -- what error message do you get? I've tested the script with 2.3 and 2.7 without any problems. I can't see anything in the script that would prevent it working with your python.137ben wrote:It doesn't work for me with python 2.5.4.
Useful clue. There was a line calling "g.warn" rather than "golly.warn" so I've fixed that in the code below. (I guess it didn't cause any problems for me because I must have already run another script that did an "import golly as g".)137ben wrote:It is telling me "NameError: global name 'g' is not defined"
Code: Select all
# Golly python script.
# Written by PM 2Ring, April 2009
# Create a spaceship stream of any period.
# The pattern is built from the current selection, which is assumed to be a spaceship.
import golly
from glife import validint, pattern
class ArgError(Exception): pass
def getargs():
''' Get input data '''
prompt = 'Enter period and number of spaceships'
usage = 'Create a stream of the selected spaceship.\n' + prompt + ', separated by spaces.'
#Loop until we get correct args, or are aborted by ESC
while True:
try:
args = golly.getstring(prompt + ':').split()
if args == []:
raise ArgError
if len(args) < 2:
raise ArgError, 'Not enough data.'
#Check that args are valid integers
for s in args:
if not validint(s):
raise ArgError, '[%s] is not a valid integer.' % s
#Convert arguments to integer
args = [int(s) for s in args]
for i in args:
if i<=0:
raise ArgError, 'Data must be > 0, not %d!' % i
except ArgError, s:
golly.warn('%s\n\n%s' % (s, usage))
else:
break
return args
def main():
selrect = golly.getselrect()
if len(selrect) == 0: golly.exit("No selection")
ss = pattern(golly.getcells(selrect))
#Get input data
period, num = getargs()
all = pattern()
for i in xrange(1, num):
all += ss[i * period]
all.put()
main()
Code: Select all
x = 7, y = 6, rule = B245/S3H
obo$4bo$2bo$bo2bobo$3bo$5bo!
Could someone change the script so it works for selections?Andrew wrote: hexrot.py -- Rotates a hexagonal neighborhood pattern by 60 degrees clockwise. Here's a glider in a hexagonal rule for testing the script:
See histogram.py in the Golly Scripts Database.Something I think would be useful is a script to count how many of each color cell there is, rather than just the total number of live cells.
It would be nice if it could also change 1-D and hexagonal neighbourhoodsedwardfanboy wrote:I have no experience writing scripts, but I have figured out a way to turn Von Neumann neighbourhood ruletables into Moore neighbourhood ruletables. I may need this script for a project I am working on.
Code: Select all
# Change the selected area from N states to Life states:
# s -> 1+2s (if in the top-left of the partition)
# s -> 2+2s (if not)
from glife import rect
import golly as g
r = rect( g.getselrect() )
if r.empty: g.exit("There is no selection.")
for row in xrange(r.top, r.top + r.height):
for col in xrange(r.left, r.left + r.width):
g.setcell(col, row, g.getcell(col,row)%2)Code: Select all
import golly as g
from copy import copy
def FindActive():
rect = g.getrect()
for i in xrange(rect[0], rect[0] + rect[2] + 1):
if g.getcell(i, rect[1]) > 0:
return (i, rect[1])
def FindConnected(listXY):
result = copy(listXY)
for xy in listXY:
x = xy[0]
y = xy[1]
for i in xrange(-1, 2):
for j in xrange(-1, 2):
if g.getcell(x + i, y + j) > 0 and len([i for t in listXY if (t[0] == x + i and t[1] == y + j)]) == 0:
result.append((x + i, y + j))
return result
def RemoveList(listXY):
for xy in listXY:
x = xy[0]
y = xy[1]
g.setcell(x, y, 0)
total = 0
g.run(1)
while int(g.getpop()) > 0:
xy = [FindActive()]
while True:
newXY = FindConnected(xy)
#g.getstring(str(xy) + " : " + str(newXY) )
if len(newXY) == len(xy):
break
xy = newXY
RemoveList(xy)
g.show(str(total))
g.update()
total += 1
g.show("The total Objects cound is: " + str(total))
g.reset()
Code: Select all
# Change the LifeHistory N states to Life states and set the rule to Life
# s -> 1+2s (if in the top-left of the partition)
# s -> 2+2s (if not)
import golly as g
cells = g.getcells(g.getrect())
g.new("")
g.setrule("B3/S23")
i = 0
while i + 2 < len(cells):
x = cells[i]
y = cells[i + 1]
val = cells[i + 2]
if val % 2 == 1:
g.setcell(x, y, 1)
i+=3Code: Select all
# Change the LifeHistory N states to Life states and stay in LifeHistory
# s -> 1+2s (if in the top-left of the partition)
# s -> 2+2s (if not)
import golly as g
cells = g.getcells(g.getrect())
g.select(g.getrect())
g.clear(0)
i = 0
while i + 2 < len(cells):
x = cells[i]
y = cells[i + 1]
val = cells[i + 2]
if val % 2 == 1:
g.setcell(x, y, 1)
i+=3
g.select([])
Code: Select all
#Smart past -> sets the rule of golly to the one in clipboard and paste it in the location of the x,y in the rle format
import golly as g
g.new("")
s = g.getclipstr()
s = s.split()
while len(s) > 0 and not s[0].startswith("x"):
s.pop(0)
if len(s) < 10:
g.exit("wrong length" + ":" + str(len(s))+ ":" + str((s)))
if s[0] == 'x' and s[1] == "=":
x = int(s[2].split(",")[0])
else:
g.exit("no pattern selected there is no x")
if s[3] == 'y' and s[4] == "=":
y = int(s[5].split(",")[0])
else:
g.exit("no pattern selected there is no y")
if s[6] == 'rule' and s[7] == "=":
rule = s[8]
pat = ""
for i in xrange(9,len(s)):
pat += s[i]
else:
g.exit("no pattern selected there is no rule or pattern")
cells = g.parse(pat)
g.setrule(rule)
g.putcells(cells, x, y)
g.fit()Code: Select all
# Rotate selection in hexagonal neighborhood by 60 degrees,
# with rotated pattern placed in new layer.
import golly as g
if g.empty(): g.exit("There is no pattern.")
rect = g.getselrect()
if len(rect) == 0: g.exit("There is no selection.")
x = rect[0]
y = rect[1]
clL=g.getcells(rect)
lngth = len(clL)
nl = g.addlayer()
indx=0
incr=2
if lngth % 2 == 1: incr = 3
st = 1
while indx < lngth:
i=clL[indx]
j=clL[indx+1]
if incr == 3: st = clL[indx+2]
g.setcell(x+j,y-i+j,st) # (x-j,y+i+j) before inverting y values
indx = indx + incrCode: Select all
# Rotate selection in hexagonal neighborhood by 60 degrees,
# with rotated pattern placed in new layer.
import golly as g
if g.empty(): g.exit("There is no pattern.")
rect = g.getselrect()
if len(rect) == 0: g.exit("There is no selection.")
#x = rect[0] y = rect[1]
wd = rect[2]
ht = rect[3]
clL=g.getcells(rect)
lngth = len(clL)
if lngth == 0: g.exit("You selected nothing.")
nl = g.addlayer()
indx=0
if lngth % 2 == 1:
lngth = lngth - 1
while indx < lngth:
i=clL[indx]
j=clL[indx+1]
#g.setcell(i,j,clL[indx+2])
g.setcell(j,-i+j,clL[indx+2]) # (-j,i+j) before inverting y value
indx = indx + 3
else:
while indx < lngth:
i=clL[indx]
j=clL[indx+1]
g.setcell(j,-i+j,1) # (-j,i+j) before inverting y value
indx = indx + 2
g.setpos(`j`, `-i+j`) In a regular square neighborhood rule, not hexagonal?codeholic wrote:Is there an easy way to rotate clockwise/counterclockwise a cell array?
Code: Select all
import golly as g
all=g.getcells(g.getrect())
g.putcells(all,100,0,0,-1,1,0) # clockwise turn
g.putcells(all,0,100,0,1,-1,0) # counterclockwise
g.putcells(all,100,100,-1,0,0,-1) # 180 degrees
g.fit()