#! /usr/bin/python from __future__ import print_function, division from visual import * import os # needed for catching ^C import traceback # needed for catching ^C import sys import Image def make_svg(): ouch = open('/tmp/mag.svg', 'w') # some svg graphics: model = ''' ''' denom = 10 # number of parallels of B per hemisphere, including equator seq = 0 page = 512 # size of image, in pixels Blinewidth = 2 # width of B-field line, in pixels #### for ii in range(-(denom-1), denom): for ii in range(0, denom): where = page/2 - asin(ii/denom)*page/pi where -= Blinewidth/2 # line is /centered/ on desired position svg = model svg = svg.replace("WHERE", "%8.4f" %where) svg = svg.replace("XSIZE2", "%8.4f" %(page/2)) svg = svg.replace("BLINEW", "%8.4f" %Blinewidth) svg = svg.replace("SEQUENCE", "mag%d" % seq) seq += 1 print (svg, file=ouch) # next: # :; inkscape --export-png=spherical-field.png \ # --export-area-page spherical-field.svg # # :; xwd -nobdrs -out /tmp/foo.xwd # :; convert /tmp/foo.xwd img48/transverse-spherical-field.png def make_wavefront(): # open the graphics window: scene = display(title='whatever', x=0, y=0, width=400, height=400, range=1.3, background=(1,1,1)) sun1 = distant_light(direction=(1, 0, 0), color=color.white) sun2 = distant_light(direction=(0, 1, 0), color=color.white) sun3 = distant_light(direction=(0, 0, 1), color=color.white) scene.lights = [sun1, sun2, sun3] im = Image.open('spherical-field.png') # size must be power of 2, ie 128 x 128 tx = materials.texture(data=im, mapping='spherical') center = sphere(pos=(0, 0, 0), radius=.03, color=color.black) main = sphere(pos=(0, 0, 0), radius=1, material=tx) main.rotate(angle=-.80, axis=(0,1,0)) main.rotate(angle=.7, axis=(1,0,0)) # some sort of while loop is required, if you want to catch ^C: counter = 0; while 1: rate(30) def doit(): try: ## make_svg() # useful during development make_wavefront() except KeyboardInterrupt as e: print ('...Interrupt...') os._exit(1) except: #xxx print("Unexpected error:\n", sys.exc_info()) print("Unexpected error... ", end='') sys.stdout.flush() # do whatever other cleanup is needed .... # Use print_exc() instead of raise, because we want to retain control, # so we can force an exit that actually exits, # rather than hanging around with a graphics window open. traceback.print_exc() os._exit(1) # idiomatic way of figuring out if this is being used as a # main program, as opposed to a library: if __name__ == '__main__': doit()