Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from math import sqrt
- from Blender import Mathutils
- from Blender import Node
- from Blender import Registry
- from Blender import Scene
- REG_NAME = 'pyNodeMatBlur'
- # Container for pixel
- class Pixel():
- def __init__(self, color, loc):
- self.color = color
- self.loc = loc
- def getBlurredCopy (self, r):
- # Copy only alpha info from current pixel.
- blurredColor = 4*[1.0] # self.color
- blurredColor[3] = self.color[3]
- rdict = Registry.GetKey(REG_NAME, False) # True to check on disk also
- if rdict: # if found, get the values saved there
- if len(rdict.keys()) <= 0:
- print "Pixel.getBlurredCopy: Empty list"
- try:
- pointCount = 0
- for pixel in rdict.keys():
- pxl = rdict[pixel]
- #dist = sqrt((loc.x - pxl.loc.x)**2 + (loc.y - pxl.loc.y)**2 + (loc.z - pxl.loc.z)**2)
- dist = (pxl.loc - self.loc).length
- #print pixel + ' ' + str(dist)
- if dist <= r:
- # Pixel within distance
- # Extremly simple linear bluring - just for testing.
- blurredColor[0] += (r - dist) * pxl.color[0]
- blurredColor[1] += (r - dist) * pxl.color[1]
- blurredColor[2] += (r - dist) * pxl.color[2]
- pointCount += 1
- blurredColor[0] /= pointCount
- blurredColor[1] /= pointCount
- blurredColor[2] /= pointCount
- except:
- # no previous data stored
- print "Pixel.getBlurredCopy: No data stored"
- #print rdict
- blurredColor = self.color
- return blurredColor
- # Stores color and location info
- class BufferImage():
- def __init__(self):
- self.pixels = []
- def addPixel(self, newPixel):
- if isinstance(newPixel, Pixel):
- add = True
- # Check that pixel with same location is not added twice
- #for pixel in self.pixels:
- # if pixel.x == newPixel.x and pixel.y == newPixel.y:
- # add = False
- # break
- if add:
- self.pixels.append(newPixel)
- else:
- print "BufferImage.addPixel: No Pixel instance."
- def calc3Dcoordinates (viewNormal):
- scene = Scene.GetCurrent() # Get current scene.
- camera = scene.objects.camera # Get camera object.
- #camLoc = Mathutils.Vector(camera.loc) # Get location of the camera (as a vector for easier calculations)
- return Mathutils.Vector(camera.loc) + viewNormal
- class BlurNode(Node.Scripted):
- def __init__(self, sockets):
- # Init sockets here.
- #The input color
- col = Node.Socket('Color', val = 4*[1.0])
- # Blur radius
- blurRad = Node.Socket('Radius', val=3.0, min=0.0, max=50.0)
- # Flag to determine whether or not to init this time.
- init = Node.Socket('Init', val=2.0, min=0.0, max=2.0)
- # "2.0" means that nothing is done at all.
- # "1.0" means the data is initialised.
- # "0.0" means the initialised data is used for the final step (blurring).
- sockets.input = [col, blurRad, init]
- sockets.output = [col]
- def __call__(self):
- if (self.input.Radius == 0.0 or self.input.Init == 2.0):
- # No radius means no blurring
- # We also ignore the default setting of Init
- Registry.SetKey(REG_NAME, {}, False)
- self.output.Color = self.input.Color
- else:
- if self.input.Init == 0.0:
- # 2. pass - Actually blur the input and output it.
- pxl = Pixel(self.input.Color, calc3Dcoordinates(self.shi.viewNormal))
- # Calculate blurred color/pixel.
- self.output.Color = pxl.getBlurredCopy(self.input.Radius)
- else: # self.input.Init == 1.0:
- # 1. pass - Store data
- rdict = Registry.GetKey(REG_NAME, False) # True to check on disk also
- if not rdict:
- rdict = {}
- pxlLoc = calc3Dcoordinates(self.shi.viewNormal)
- pxl = Pixel(self.input.Color, pxlLoc)
- if pxl == None:
- print "'None' pixel found"
- # Store color & pixel (1. pass) for later use in 2. pass
- regKeyName = 'p_' + str(self.shi.pixel[0]) + '_' + str(self.shi.pixel[1])
- rdict[regKeyName] = pxl
- #print regKeyName
- # Store registry key.
- Registry.SetKey(REG_NAME, rdict, False)
- # Dummy function
- self.output.Color = self.input.Color
- __node__ = BlurNode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement