#!/usr/bin/python
# lprocedural.py
# One of 5 python scripts that implement a simple L system. 
# Refer to the accompanying "procedural_tester.rib". It
# provides an example of how this script can be called.
#
# Malcolm Kesson
# 18 Jan 2012, 26 Jan 2012
  
import sys
import lscriptreader, lgenerator, lrib
  
# Block until a line of text is received from prman
args = sys.stdin.readline()
  
while args:
    # First, read the number of pixels covered by the bounding box of
    # the procedural primitive and the path to the L script .dat file.
    arg_list = args.split()
    pixels = float(arg_list[0])
    script_path = arg_list[1].rstrip(',')
    
    script_text = lscriptreader.read(script_path)
    database = lscriptreader.parse(script_text, lscriptreader.defaults)
    # Second, read the any additional data that may have been passed 
    # to the script. The data will be in name/value pairs separated 
    # by commas.
    if len(arg_list) > 2:
        arg_list = args.split(',')     # tokenize 
        arg_list = arg_list[1:]        # ignore pixels and script_path
        # Modify the database.
        database = lscriptreader.parse(arg_list, database)
    lstr = lgenerator.write_lstring(database)
                            
    print 'AttributeBegin'
    # Passing an empty string to lrib.render() prevents it from writing 
    # a rib file. Instead, it returns the rib statements.
    print lrib.render(lstr, '', database)
    print 'AttributeEnd'
  
    sys.stdout.write('\377')
    sys.stdout.flush()
    
    # Block until the next line is received from prman
    args = sys.stdin.readline()