Advertisement
Guest User

InsaneBump.py

a guest
Aug 24th, 2013
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.42 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. from gimpfu import *
  4. import commands
  5. import glob
  6. import os
  7. import string
  8. import subprocess
  9.  
  10. suffixes = (['diffuse', '_d'], ['specular', '_s'], ['normal', '_n'], ['height', '_h'], \
  11. ['ambient', '_a'], ['highdetail', '_hn'], ['shaperecog', '_sn'], ['lowdetail', '_ln'], \
  12. ['mediumdetail', '_mn'])
  13.  
  14. def genFilenames(filename):
  15. (root, ext)=os.path.splitext(filename)
  16. filenames = {}
  17. for suffix in suffixes:
  18. filenames[suffix[0]] = root + suffix[1] + ext
  19. return filenames
  20.  
  21. def saveLastOperation(image, filename):
  22. layer = pdb.gimp_image_get_active_layer(image)
  23. pdb.gimp_file_save(image, layer, filename, filename)
  24.  
  25. def specularEdge(image, filename, level, enhance):
  26. drawable = pdb.gimp_image_get_active_layer(image)
  27. newlayer = pdb.gimp_layer_copy (drawable, 1)
  28. pdb.gimp_image_add_layer(image, newlayer, -1)
  29. pdb.gimp_image_set_active_layer(image, newlayer)
  30. pdb.gimp_desaturate(newlayer)
  31.  
  32. drawable = pdb.gimp_image_get_active_layer(image)
  33. newlayer = pdb.gimp_layer_copy (drawable, 1)
  34. pdb.gimp_image_add_layer(image, newlayer, -1)
  35. pdb.gimp_image_set_active_layer(image, newlayer)
  36. pdb.gimp_desaturate(newlayer)
  37.  
  38. if enhance:
  39. pdb.plug_in_dog(image, newlayer, 1.0, 8.0, 1, 0)
  40. else:
  41. pdb.plug_in_dog(image, newlayer, 8.0, 1.0, 1, 0)
  42. #pdb.plug_in_vinvert(image,newlayer)
  43.  
  44. pdb.gimp_layer_set_mode(newlayer, 7)
  45. pdb.gimp_image_merge_down(image, newlayer, 0)
  46.  
  47. drawable = pdb.gimp_image_get_active_layer(image)
  48. pdb.gimp_brightness_contrast(drawable, 0, 64)
  49. pdb.gimp_levels(drawable, 0, level, 255, 1, 0, 255)
  50. if not enhance:
  51. pdb.gimp_image_raise_layer_to_top(image, drawable)
  52. pdb.gimp_file_save(image, drawable, filename, filename)
  53. pdb.gimp_edit_clear(drawable)
  54.  
  55. def removeShading(image):
  56. drawable = pdb.gimp_image_get_active_layer(image)
  57. newlayer = pdb.gimp_layer_copy (drawable, 1)
  58. pdb.gimp_image_add_layer(image, newlayer, -1)
  59. pdb.gimp_image_set_active_layer(image, newlayer)
  60. pdb.plug_in_gauss(image, newlayer, 20.0, 20.0, 0)
  61. pdb.plug_in_vinvert(image, newlayer)
  62. pdb.gimp_layer_set_mode(newlayer, 5)
  63. pdb.gimp_image_merge_visible_layers(image, 0)
  64.  
  65. def blur(image, diffuse, width, height, passes, normal):
  66. desatdiffuse = pdb.gimp_layer_copy (diffuse, 1)
  67. pdb.gimp_image_add_layer(image, desatdiffuse, -1)
  68. pdb.gimp_image_set_active_layer(image, desatdiffuse)
  69. if normal == 0:
  70. pdb.gimp_desaturate(desatdiffuse)
  71.  
  72. for i in range(passes):
  73. drawable = pdb.gimp_image_get_active_layer(image)
  74. newlayer = pdb.gimp_layer_copy (drawable, 1)
  75. pdb.gimp_image_add_layer(image, newlayer, -1)
  76. pdb.gimp_image_set_active_layer(image, newlayer)
  77. pdb.plug_in_gauss(image, newlayer, width * 0.05, height * 0.05, 0)
  78. pdb.gimp_layer_set_mode(newlayer, 5)
  79. pdb.gimp_image_merge_down(image, newlayer, 0)
  80. drawable = pdb.gimp_image_get_active_layer(image)
  81. if normal == 1:
  82. pdb.plug_in_normalmap(image, drawable, 0, 0.0, 1.0, 0, 0, 0, 8, 0, 0, 0, 0, 0.0, drawable)
  83. pdb.gimp_layer_set_mode(drawable, 5)
  84. pdb.gimp_image_merge_down(image, drawable, 0)
  85.  
  86. def sharpen(image, diffuse, depth, filterSize, strength):
  87. sharpnormal = pdb.gimp_layer_copy (diffuse, 1)
  88. pdb.gimp_image_add_layer(image, sharpnormal, -1)
  89. pdb.gimp_image_set_active_layer(image, sharpnormal)
  90.  
  91. pdb.plug_in_normalmap(image, sharpnormal, 0, 0.0, depth, filterSize, 0, 0, 0, 0, 0, 1, 0, 0.0, sharpnormal)
  92. #pdb.gimp_levels_stretch(sharpnormal)
  93. pdb.gimp_image_set_active_layer(image, sharpnormal)
  94. pdb.gimp_layer_set_mode(sharpnormal, 5)
  95. pdb.gimp_layer_set_opacity(sharpnormal, strength)
  96. pdb.gimp_image_raise_layer_to_top(image, sharpnormal)
  97.  
  98. def shapeRecognise(image, normalmap, strength):
  99. blurnormal = pdb.gimp_layer_copy (normalmap, 1)
  100. pdb.gimp_image_add_layer(image, blurnormal, -1)
  101. pdb.gimp_image_set_active_layer(image, blurnormal)
  102. pdb.plug_in_gauss(image, blurnormal, 20, 20, 0)
  103. pdb.plug_in_colors_channel_mixer(image, blurnormal, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -200.0)
  104. pdb.plug_in_normalmap(image, blurnormal, 0, 0.0, 1.0, 0, 0, 0, 8, 0, 0, 0, 0, 0.0, blurnormal)
  105. pdb.plug_in_colors_channel_mixer(image, blurnormal, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -200.0)
  106. pdb.gimp_layer_set_mode(blurnormal, 5)
  107. pdb.gimp_layer_set_opacity(blurnormal, strength)
  108.  
  109. def analyze(layer):
  110. highlights = pdb.gimp_histogram(layer, 0, 179, 256)
  111. midtones = pdb.gimp_histogram(layer, 0, 77, 178)
  112. shadows = pdb.gimp_histogram(layer, 0, 0, 76)
  113. f = open("/tmp/aidata.txt", "w")
  114. f.write(str(int(shadows[0])) + " " + str(int(shadows[1])) + " " + str(int(shadows[5] * 100)) + " ")
  115. f.write(str(int(midtones[0])) + " " + str(int(midtones[1])) + " " + str(int(midtones[5] * 100)) + " ")
  116. f.write(str(int(highlights[0])) + " " + str(int(highlights[1])) + " " + str(int(highlights[5] * 100)) + " ")
  117.  
  118. def doBaseMap(image, diffuse, depth, passes):
  119. for i in range(passes):
  120. newlayer = pdb.gimp_layer_copy (diffuse, 1)
  121. pdb.gimp_image_add_layer(image, newlayer, -1)
  122. pdb.gimp_image_set_active_layer(image, newlayer)
  123. ok = float(i + 1) * (i + 1)
  124. pdb.plug_in_gauss(image, newlayer, ok * 3, ok * 3, 0)
  125. pdb.plug_in_normalmap(image, newlayer, 5, 0.0, depth * ok, 0, 0, 0, 0, 0, 0, 1, 0, 0.0, newlayer)
  126.  
  127. if i > 0:
  128. #pdb.gimp_layer_set_opacity(newlayer,50-(i*10))
  129. pdb.gimp_layer_set_mode(newlayer, 5)
  130. pdb.gimp_image_merge_down(image, newlayer, 0)
  131. newlayer = pdb.gimp_image_get_active_layer(image)
  132. pdb.plug_in_normalmap(image, newlayer, 0, 0.0, 1.0, 0, 0, 0, 8, 0, 0, 0, 0, 0.0, newlayer)
  133. drawable = pdb.gimp_image_get_active_layer(image)
  134. #newlayer=pdb.gimp_layer_copy (drawable, 1)
  135. #pdb.gimp_image_add_layer(image,newlayer,-1)
  136. #pdb.gimp_image_set_active_layer(image,newlayer)
  137. #pdb.gimp_levels_stretch(newlayer)
  138. #pdb.gimp_layer_set_opacity(newlayer,100)
  139. #pdb.gimp_layer_set_mode(newlayer,5)
  140. #pdb.gimp_image_merge_down(image,newlayer,0)
  141.  
  142.  
  143.  
  144. def batchnr(img, layer, rmlight, resize, seamless, rszmult, sedge, slevel,
  145. depth, hpasses, mdetail, ldetails, shaperecog, smoothstep, invheight):
  146.  
  147. filename = pdb.gimp_image_get_filename(img)
  148. mapnames = genFilenames(filename)
  149. image = img
  150. size = pdb.gimp_image_width(image)
  151.  
  152. if resize:
  153. hiresimg = pdb.gimp_image_resize(image, rszmult * size, rszmult * size, 0, 0)
  154. #hiresimg = pdb.gimp_file_load( getFilename(filename,"_hd"), filename)
  155. drawable = pdb.gimp_image_get_active_layer(hiresimg)
  156. noiselayer = pdb.gimp_layer_copy (drawable, 1)
  157. pdb.gimp_image_add_layer(hiresimg, noiselayer, -1)
  158. pdb.gimp_image_set_active_layer(hiresimg, noiselayer)
  159. pdb.plug_in_rgb_noise(hiresimg, noiselayer, 1, 1, 0.20, 0.20, 0.20, 0)
  160. #pdb.plug_in_blur(hiresimg,noiselayer)
  161. pdb.gimp_layer_set_mode(noiselayer, 14)
  162. pdb.gimp_image_merge_down(hiresimg, noiselayer, 0)
  163. image = hiresimg
  164. drawable = pdb.gimp_image_get_active_layer(image)
  165. ###########################analyze(drawable)
  166.  
  167. if rmlight:
  168. removeShading(image)
  169.  
  170. diffuse = pdb.gimp_image_get_active_layer(image)
  171. pdb.gimp_levels_stretch(diffuse)
  172. if(seamless):
  173. pdb.plug_in_make_seamless(image, diffuse)
  174. pdb.gimp_file_save(image, diffuse, mapnames['diffuse'], mapnames['diffuse'])
  175.  
  176. #drawable = pdb.gimp_image_get_active_layer(image)
  177. #pdb.gimp_levels(drawable,0,64,128,1,0,255)
  178. wsize = pdb.gimp_image_width(image)
  179. hsize = pdb.gimp_image_width(image)
  180. blur(image, diffuse, wsize, hsize, hpasses, 0)
  181.  
  182. normalmap = pdb.gimp_image_get_active_layer(image)
  183. if smoothstep:
  184. pdb.plug_in_blur(image, normalmap)
  185. if invheight:
  186. pdb.plug_in_vinvert(image, normalmap)
  187.  
  188.  
  189. pdb.gimp_file_save(image, normalmap, mapnames['height'], mapnames['height'])
  190.  
  191.  
  192. #lfnormal=pdb.gimp_layer_copy (diffuse, 1)
  193. #pdb.gimp_image_add_layer(image,lfnormal,-1)
  194. #pdb.gimp_image_raise_layer_to_top(image,lfnormal)
  195. #pdb.plug_in_normalmap(image, lfnormal,8,0.0,depth,0,0,0,0,0,0,0,0,0.0,normalmap)
  196. #blur(image,lfnormal,hpasses,1)
  197. #normalmap = pdb.gimp_image_get_active_layer(image)
  198. doBaseMap(image, diffuse, depth, hpasses)
  199. normalmap = pdb.gimp_image_get_active_layer(image)
  200. pdb.gimp_file_save(image, normalmap, mapnames['highdetail'], mapnames['highdetail'])
  201.  
  202.  
  203. shapeRecognise(image, normalmap, shaperecog)
  204. if smoothstep:
  205. normalmap = pdb.gimp_image_get_active_layer(image)
  206. pdb.plug_in_blur(image, normalmap)
  207.  
  208. saveLastOperation(image, mapnames['shaperecog'])
  209.  
  210. sharpen(image, diffuse, depth, 0, ldetails)
  211. normalmap = pdb.gimp_image_get_active_layer(image)
  212. pdb.plug_in_sharpen(image, normalmap, 20)
  213. saveLastOperation(image, mapnames['lowdetail'])
  214.  
  215. sharpen(image, diffuse, depth, 6, mdetail)
  216. normalmap = pdb.gimp_image_get_active_layer(image)
  217. pdb.plug_in_blur(image, normalmap)
  218. saveLastOperation(image, mapnames['mediumdetail'])
  219.  
  220. pdb.gimp_drawable_set_visible(diffuse, 0)
  221. pdb.gimp_image_merge_visible_layers(image, 0)
  222.  
  223. drawable = pdb.gimp_image_get_active_layer(image)
  224. pdb.plug_in_normalmap(image, drawable, 0, 0.0, 1.0, 0, 0, 0, 8, 0, 0, 0, 0, 0.0, drawable)
  225. pdb.gimp_file_save(image, drawable, mapnames['normal'], mapnames['normal'])
  226. pdb.plug_in_colors_channel_mixer(image, drawable, 0.0, -200.0, 0.0, 0.0, 0.0, -200.0, 0.0, 0.0, 0.0, 1.0)
  227. pdb.gimp_desaturate(drawable)
  228. pdb.gimp_levels_stretch(drawable)
  229. pdb.gimp_file_save(image, drawable, mapnames['ambient'], mapnames['ambient'])
  230. pdb.gimp_drawable_set_visible(diffuse, 1)
  231. pdb.gimp_image_set_active_layer(image, diffuse)
  232.  
  233. specularEdge(image, mapnames['specular'], slevel, sedge)
  234.  
  235.  
  236. register(
  237. "InsaneBump", "", "", "", "", "",
  238. "<Image>/Filters/Map/Insane...", "",
  239. [
  240. (PF_TOGGLE, "rmlight", "Remove Lighting:", FALSE),
  241. (PF_TOGGLE, "resize", "Resize:", FALSE),
  242. (PF_TOGGLE, "seamless", "Make Seamless:", TRUE),
  243. (PF_INT32, "rszmult", "Resize Multiplier:", 2),
  244. (PF_TOGGLE, "sedge", "Edge Enhance Specular:", TRUE),
  245. (PF_SPINNER, "slevel", "Specular Level:", 64, (0, 255, 1)),
  246. (PF_FLOAT, "depth", "Depth (>0):", 20),
  247. (PF_INT32, "hpasses", "Detail Pass Count:", 3),
  248. (PF_SPINNER, "mdetail", "Medium Detail Intensity:", 50, (0, 100, 1)),
  249. (PF_SPINNER, "ldetails", "Low Detail Intensity:", 50, (0, 100, 1)),
  250. (PF_SPINNER, "shaperecog", "Shape Recognition:", 50, (0, 100, 1)),
  251. (PF_TOGGLE, "smoothstep", "Smooth Step:", TRUE),
  252. (PF_TOGGLE, "invheight", "Invert Height Map:", FALSE),
  253. ],
  254. [],
  255. batchnr
  256. )
  257.  
  258. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement