import prman
import random
  
class Rif(prman.Rif):
    def __init__(self, ri, scale):
        self.default_blobsize = float(scale)
        prman.Rif.__init__(self, ri)            
    def PointsGeneralPolygons(self, nloops, nverts, verts, params):
        # Look for primvars 
        #    'constant float blobby' either 0 or 1 (ignore blobby/convert to blobby),
        #    'constant float blob_min' minimum random blob size,
        #    'constant float blob_max' maximum random blob size.
        if params.has_key('constant float blobby'):
            if params['constant float blobby'][0] <= 0:
                self.m_ri.PointsGeneralPolygons(nloops, nverts, verts, params)
                return
        blobMin = self.default_blobsize
        blobMax = self.default_blobsize 
        if params.has_key('constant float blob_min'):
            blobMin = params['constant float blob_min'][0]
        if params.has_key('constant float blob_max'):
            blobMax = params['constant float blob_max'][0]
                
        opcodes = []
        numblobs = len(params['vertex point P'])/3
        for n in range(numblobs):
            opcodes.append(1001)
            opcodes.append(n * 16)
        opcodes.append(0)        # blending code
        opcodes.append(numblobs)# blend all blobs
        for n in range(numblobs):
            opcodes.append(n)    # indices of the blobs to blend
        #common = (self.scale,0,0,0,0,self.scale,0,0,0,0,self.scale,0)
        #transforms = (self.scale,0,0,0,0,self.scale,0,0,0,0,self.scale,0)
        transforms = ''
        xyz = params['vertex point P']
        numxyz = len(xyz)
        random.seed(numblobs)
        for n in range(0, numxyz, 3):
            pos = (xyz[n], xyz[n+1], xyz[n+2])
            size = random.uniform(blobMin, blobMax)
            common = (size,0,0,0,0,size,0,0,0,0,size,0)
            if n == 0:
                transforms = common + pos + (1,)
            else:
                transforms = transforms + common + pos + (1,)
        params = {}
        strs = ('',)
        self.m_ri.Blobby(numblobs,opcodes,transforms, strs, params)