Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2008
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | None | 0 0
  1. import os
  2. from math import sqrt
  3.  
  4. from Blender import Mathutils
  5.  
  6. from Blender import Node
  7. from Blender import Registry
  8. from Blender import Scene
  9.  
  10. REG_NAME    = 'pyNodeMatBlur'
  11.  
  12. # Container for pixel
  13. class Pixel():
  14.     def __init__(self, color, loc):
  15.         self.color = color
  16.         self.loc = loc
  17.  
  18.     def getBlurredCopy (self, r):
  19.         # Copy only alpha info from current pixel.
  20.         blurredColor = 4*[1.0] # self.color
  21.         blurredColor[3] = self.color[3]
  22.    
  23.         rdict = Registry.GetKey(REG_NAME, False) # True to check on disk also
  24.         if rdict: # if found, get the values saved there
  25.             if len(rdict.keys()) <= 0:
  26.                 print "Pixel.getBlurredCopy: Empty list"
  27.             try:
  28.                 pointCount = 0
  29.                 for pixel in rdict.keys():
  30.                     pxl = rdict[pixel]
  31.  
  32.                     #dist = sqrt((loc.x - pxl.loc.x)**2 + (loc.y - pxl.loc.y)**2 + (loc.z - pxl.loc.z)**2)
  33.                     dist = (pxl.loc - self.loc).length
  34.  
  35.                     #print pixel + ' ' + str(dist)
  36.                     if dist <= r:
  37.                         # Pixel within distance
  38.                         # Extremly simple linear bluring - just for testing.
  39.                         blurredColor[0] += (r - dist) * pxl.color[0]
  40.                         blurredColor[1] += (r - dist) * pxl.color[1]
  41.                         blurredColor[2] += (r - dist) * pxl.color[2]
  42.                         pointCount += 1
  43.  
  44.                 blurredColor[0] /= pointCount
  45.                 blurredColor[1] /= pointCount
  46.                 blurredColor[2] /= pointCount
  47.  
  48.             except:
  49.                 # no previous data stored
  50.                 print "Pixel.getBlurredCopy: No data stored"
  51.                 #print rdict
  52.                 blurredColor = self.color
  53.  
  54.         return blurredColor
  55.  
  56. # Stores color and location info
  57. class BufferImage():
  58.     def __init__(self):
  59.         self.pixels = []
  60.  
  61.     def addPixel(self, newPixel):
  62.         if isinstance(newPixel, Pixel):
  63.             add = True
  64.  
  65.             # Check that pixel with same location is not added twice
  66.             #for pixel in self.pixels:
  67.             #   if pixel.x == newPixel.x and pixel.y == newPixel.y:
  68.             #       add = False
  69.             #       break
  70.  
  71.             if add:
  72.                 self.pixels.append(newPixel)
  73.         else:
  74.             print "BufferImage.addPixel: No Pixel instance."
  75.  
  76. def calc3Dcoordinates (viewNormal):
  77.     scene = Scene.GetCurrent()  # Get current scene.
  78.  
  79.     camera = scene.objects.camera       # Get camera object.
  80.     #camLoc = Mathutils.Vector(camera.loc)  # Get location of the camera (as a vector for easier calculations)
  81.  
  82.     return Mathutils.Vector(camera.loc) + viewNormal
  83.  
  84. class BlurNode(Node.Scripted):
  85.     def __init__(self, sockets):
  86.         # Init sockets here.
  87.  
  88.         #The input color
  89.         col = Node.Socket('Color', val = 4*[1.0])
  90.  
  91.         # Blur radius
  92.         blurRad = Node.Socket('Radius', val=3.0, min=0.0, max=50.0)
  93.  
  94.         # Flag to determine whether or not to init this time.
  95.         init = Node.Socket('Init', val=2.0, min=0.0, max=2.0)
  96.         # "2.0" means that nothing is done at all.
  97.         # "1.0" means the data is initialised.
  98.         # "0.0" means the initialised data is used for the final step (blurring).
  99.  
  100.         sockets.input = [col, blurRad, init]
  101.         sockets.output = [col]
  102.  
  103.     def __call__(self):
  104.         if (self.input.Radius == 0.0 or self.input.Init == 2.0):
  105.             # No radius means no blurring
  106.             # We also ignore the default setting of Init
  107.             Registry.SetKey(REG_NAME, {}, False)
  108.             self.output.Color = self.input.Color
  109.         else:
  110.             if self.input.Init == 0.0:
  111.                 # 2. pass - Actually blur the input and output it.
  112.                 pxl = Pixel(self.input.Color, calc3Dcoordinates(self.shi.viewNormal))
  113.  
  114.                 # Calculate blurred color/pixel.
  115.                 self.output.Color = pxl.getBlurredCopy(self.input.Radius)
  116.  
  117.             else: # self.input.Init == 1.0:
  118.                 # 1. pass - Store data
  119.                 rdict = Registry.GetKey(REG_NAME, False) # True to check on disk also
  120.                 if not rdict:
  121.                     rdict = {}
  122.  
  123.                 pxlLoc = calc3Dcoordinates(self.shi.viewNormal)
  124.                 pxl = Pixel(self.input.Color, pxlLoc)
  125.                 if pxl == None:
  126.                     print "'None' pixel found"
  127.  
  128.                 # Store color & pixel (1. pass) for later use in 2. pass
  129.                 regKeyName = 'p_' + str(self.shi.pixel[0]) + '_' + str(self.shi.pixel[1])
  130.                 rdict[regKeyName] = pxl
  131.  
  132.                 #print regKeyName
  133.  
  134.                 # Store registry key.
  135.                 Registry.SetKey(REG_NAME, rdict, False)
  136.  
  137.                 # Dummy function
  138.                 self.output.Color = self.input.Color
  139.  
  140. __node__ = BlurNode
  141.  
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement