Utilizes Blender (headless) and Processing to generate images (3D and 2D renderings) for the webshop
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

render_picture_for_webshop.pde 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import java.io.IOException;
  2. import java.awt.*;
  3. import drop.*;
  4. import controlP5.*;
  5. SDrop drop;
  6. ControlP5 cp5;
  7. //SecondApplet gui;
  8. boolean passepartout, detail, framed, livingroom, bedroom;
  9. PGraphics renderer;
  10. PImage img, pacman;
  11. int maxImgLength = 900;
  12. String widthInput, heightInput;
  13. boolean imageLoaded, measuresKnown, detailAreaSet;
  14. String foldername, imagePath;
  15. void resetAll() {
  16. imageLoaded = false;
  17. measuresKnown = false;
  18. detailAreaSet = false;
  19. foldername = "";
  20. imagePath = "";
  21. }
  22. void dropEvent(DropEvent theDropEvent) {
  23. imagePath = theDropEvent.filePath();
  24. img = loadImage(imagePath);
  25. imgw = img.width >= img.height ? maxImgLength : (int)map(img.width, 0, img.height, 0, maxImgLength);
  26. imgh = img.height >= img.width ? maxImgLength : (int)map(img.height, 0, img.width, 0, maxImgLength);
  27. foldername = day() + "." + month() + "." + year() + " " + hour() + "-" + minute() + "-" + second();
  28. measuresKnown = false;
  29. widthInput = "";
  30. heightInput = "";
  31. cp5.get(Textfield.class, "width").show();
  32. cp5.get(Textfield.class, "height").show();
  33. cp5.get(Textfield.class, "width").setFocus(true);
  34. cp5.get(Toggle.class, "passepartout").show();
  35. cp5.get(Toggle.class, "detail").show();
  36. cp5.get(Toggle.class, "framed").show();
  37. cp5.get(Toggle.class, "livingroom").show();
  38. cp5.get(Toggle.class, "bedroom").show();
  39. imageLoaded = true;
  40. }
  41. void settings() {
  42. size(200, 200);
  43. }
  44. void setup() {
  45. //size(200, 200);
  46. OS = osSetup();
  47. println(OS);
  48. renderer = createGraphics(1024, 1024);
  49. detailGraphic = createGraphics(1024, 1024);
  50. pacman = loadImage(dataPath("pacman.png"));
  51. textSize(30);
  52. drop = new SDrop(this);
  53. cp5 = new ControlP5(this);
  54. Textfield wInput = cp5.addTextfield("width")
  55. .setPosition(20, 50)
  56. .setColorValue(color(0, 0, 0))
  57. .setColorBackground(color(200, 200, 200))
  58. .setSize(70, 30)
  59. .setFocus(true);
  60. wInput.getCaptionLabel().setColor(color(0, 0, 0));
  61. Textfield hInput = cp5.addTextfield("height")
  62. .setPosition(110, 50)
  63. .setColorValue(color(0, 0, 0))
  64. .setColorBackground(color(200, 200, 200))
  65. .setSize(70, 30);
  66. hInput.getCaptionLabel().setColor(color(0, 0, 0));
  67. Toggle renderPassepartout = cp5.addToggle("passepartout")
  68. .setColorValue(color(0, 0, 0))
  69. .setColorBackground(color(200, 200, 200))
  70. .setSize(20, 20)
  71. .setPosition(10, 110);
  72. renderPassepartout.getCaptionLabel().setColor(color(0, 0, 0));
  73. Toggle renderDetail = cp5.addToggle("detail")
  74. .setColorValue(color(0, 0, 0))
  75. .setColorBackground(color(200, 200, 200))
  76. .setSize(20, 20)
  77. .setPosition(45, 120);
  78. renderDetail.getCaptionLabel().setColor(color(0, 0, 0));
  79. Toggle renderPictureOnly = cp5.addToggle("framed")
  80. .setColorValue(color(0, 0, 0))
  81. .setColorBackground(color(200, 200, 200))
  82. .setSize(20, 20)
  83. .setPosition(80, 130);
  84. renderPictureOnly.getCaptionLabel().setColor(color(0, 0, 0));
  85. Toggle renderLivingroom = cp5.addToggle("livingroom")
  86. .setColorValue(color(0, 0, 0))
  87. .setColorBackground(color(200, 200, 200))
  88. .setSize(20, 20)
  89. .setPosition(115, 140);
  90. renderLivingroom.getCaptionLabel().setColor(color(0, 0, 0));
  91. Toggle renderBedroom = cp5.addToggle("bedroom")
  92. .setColorValue(color(0, 0, 0))
  93. .setColorBackground(color(200, 200, 200))
  94. .setSize(20, 20)
  95. .setPosition(150, 150);
  96. renderBedroom.getCaptionLabel().setColor(color(0, 0, 0));
  97. cp5.get(Textfield.class, "width").hide();
  98. cp5.get(Textfield.class, "height").hide();
  99. cp5.get(Toggle.class, "passepartout").hide();
  100. cp5.get(Toggle.class, "detail").hide();
  101. cp5.get(Toggle.class, "framed").hide();
  102. cp5.get(Toggle.class, "livingroom").hide();
  103. cp5.get(Toggle.class, "bedroom").hide();
  104. }
  105. int imgw, imgh;
  106. void draw() {
  107. background(255);
  108. if (imageLoaded && !measuresKnown) {
  109. text("SAG MAßE", 20, 30);
  110. widthInput = cp5.get(Textfield.class, "width").getText();
  111. heightInput = cp5.get(Textfield.class, "height").getText();
  112. } else if (imageLoaded && measuresKnown) {
  113. println();
  114. renderer.noSmooth();
  115. renderer.beginDraw();
  116. renderer.imageMode(CENTER);
  117. //renderer.clear();
  118. renderer.background(255);
  119. renderer.image(img, renderer.width/2, renderer.height/2, imgw, imgh);
  120. renderer.endDraw();
  121. image(renderer, 0, 0, width, height);
  122. if (!detailAreaSet && detail) {
  123. noFill();
  124. stroke(255, 0, 0);
  125. strokeWeight(2);
  126. rect(mouseX-detailAreaSize/5/2, mouseY-detailAreaSize/5/2, detailAreaSize/5, detailAreaSize/5);
  127. fill(0);
  128. noStroke();
  129. text("ZEIG DETAIL", 20, 30);
  130. }
  131. if (renderNow) {
  132. renderNow = false;
  133. if (passepartout || detail || framed || livingroom || bedroom) {
  134. createOutput(dataPath("") + "/" + foldername + "/.");
  135. openRenderFolder();
  136. }
  137. if (imageLoaded) {
  138. if (passepartout) {
  139. println("Saving passepartout image");
  140. renderer.save(dataPath("") + "/" + foldername + "/passepartout.png");
  141. }
  142. if (measuresKnown) {
  143. if (detail) {
  144. println("Saving detail image");
  145. detailGraphic.noSmooth();
  146. detailGraphic.beginDraw();
  147. detailGraphic.image(temp, 0, 0, detailGraphic.width, detailGraphic.height);
  148. detailGraphic.save(dataPath("") + "/" + foldername + "/detail.png");
  149. detailGraphic.endDraw();
  150. }
  151. if (framed) {
  152. println("Rendering framed image in blender (picture only)");
  153. String pictureonlycmd[] = {"blender -b \"" + dataPath("") + "/HEADLESS_PICTUREONLY.blend\" --python \"" + dataPath("") + "/render_pictureonly.py\" -- " + widthInput + " " + heightInput + " \"" + imagePath + "\" \"" + dataPath("") + "/" + foldername + "/pictureonly.png\""};
  154. if (OS == "mac") {
  155. saveStrings(dataPath("") + "/pictureonly.command", pictureonlycmd);
  156. launch(dataPath("") + "/pictureonly.command");
  157. } else if (OS == "windows") {
  158. saveStrings(dataPath("") + "/pictureonly.cmd", pictureonlycmd);
  159. launch(dataPath("") + "/pictureonly.cmd");
  160. }
  161. }
  162. if (bedroom) {
  163. println("Rendering framed image in blender (bedroom)");
  164. String bedroomcmd[] = {"blender -b \"" + dataPath("") + "/HEADLESS_BEDROOM.blend\" --python \"" + dataPath("") + "/render.py\" -- " + widthInput + " " + heightInput + " \"" + imagePath + "\" \"" + dataPath("") + "/" + foldername + "/bedroom.png\""};
  165. if (OS == "mac") {
  166. saveStrings(dataPath("") + "/bedroom.command", bedroomcmd);
  167. launch(dataPath("") + "/bedroom.command");
  168. } else if (OS == "windows") {
  169. saveStrings(dataPath("") + "/bedroom.cmd", bedroomcmd);
  170. launch(dataPath("") + "/bedroom.cmd");
  171. }
  172. }
  173. if (livingroom) {
  174. println("Rendering framed image in blender (livingroom)");
  175. String livingroomcmd[] = {"blender -b \"" + dataPath("") + "/HEADLESS_LIVINGROOM.blend\" --python \"" + dataPath("") + "/render.py\" -- " + widthInput + " " + heightInput + " \"" + imagePath + "\" \"" + dataPath("") + "/" + foldername + "/livingroom.png\""};
  176. if (OS == "mac") {
  177. saveStrings(dataPath("") + "/livingroom.command", livingroomcmd);
  178. launch(dataPath("") + "/livingroom.command");
  179. } else if (OS == "windows") {
  180. saveStrings(dataPath("") + "/livingroom.cmd", livingroomcmd);
  181. launch(dataPath("") + "/livingroom.cmd");
  182. }
  183. }
  184. }
  185. }
  186. println("done");
  187. resetAll();
  188. }
  189. } else {
  190. image(pacman, 0, 0, width, height);
  191. fill(0);
  192. text("GIB BILD", 20, 30);
  193. }
  194. }
  195. int detailAreaSize = 256;
  196. int detailAreaX, detailAreaY;
  197. int imgClickX, imgClickY, imgBiggerFactor, imgDetailSize;
  198. PImage temp;
  199. PGraphics detailGraphic;
  200. void setDetailArea() {
  201. detailAreaSet = true;
  202. println("rendering detailed view");
  203. imgClickX = (int)map(map(mouseX, 0, width, 0, 1024), 512-imgw/2, 512+imgw/2, 0, img.width);
  204. imgClickY = (int)map(map(mouseY, 0, height, 0, 1024), 512-imgh/2, 512+imgh/2, 0, img.height);
  205. imgBiggerFactor = max(img.width, img.height)/max(imgw, imgh);
  206. imgDetailSize = detailAreaSize * imgBiggerFactor;
  207. temp = img.get(imgClickX - imgDetailSize/2, imgClickY - imgDetailSize/2, imgDetailSize, imgDetailSize);
  208. }
  209. String OS;
  210. void mouseClicked() {
  211. if (mouseButton == LEFT && imageLoaded && measuresKnown && !detailAreaSet && detail) {
  212. setDetailArea();
  213. renderNow = true;
  214. }
  215. }
  216. boolean renderNow;
  217. void openRenderFolder() {
  218. String folderpath = sketchPath("") + "data/" + foldername + "/";
  219. Desktop desktop = Desktop.getDesktop();
  220. File dirToOpen = null;
  221. try {
  222. dirToOpen = new File(folderpath);
  223. desktop.open(dirToOpen);
  224. }
  225. catch (IOException iae) {
  226. System.out.println("File Not Found");
  227. }
  228. }
  229. String osSetup () {
  230. String os = System.getProperty("os.name");
  231. if (os.contains("Windows")) {
  232. return "windows";
  233. } else if (os.contains("Mac")) {
  234. return "mac";
  235. } else if (os.contains("Linux")) {
  236. return "linux";
  237. } else {
  238. return "other";
  239. }
  240. }
  241. boolean focusHeight;
  242. void keyPressed() {
  243. if (key == ENTER) {
  244. println(widthInput, heightInput);
  245. if (imageLoaded && !measuresKnown && widthInput != "" && heightInput != "") {
  246. if (int(widthInput) < 10 || int(heightInput) < 10 || int(widthInput) > 120 || int(heightInput) > 120) {
  247. println("Nope");
  248. } else {
  249. measuresKnown = true;
  250. cp5.get(Textfield.class, "width").hide();
  251. cp5.get(Textfield.class, "height").hide();
  252. cp5.get(Toggle.class, "passepartout").hide();
  253. cp5.get(Toggle.class, "detail").hide();
  254. cp5.get(Toggle.class, "framed").hide();
  255. cp5.get(Toggle.class, "livingroom").hide();
  256. cp5.get(Toggle.class, "bedroom").hide();
  257. if (!detail) {
  258. renderNow = true;
  259. }
  260. }
  261. }
  262. }
  263. if (key == TAB) {
  264. if (!measuresKnown && imageLoaded) {
  265. if (!focusHeight) {
  266. cp5.get(Textfield.class, "height").setFocus(true);
  267. cp5.get(Textfield.class, "width").setFocus(false);
  268. } else {
  269. cp5.get(Textfield.class, "height").setFocus(false);
  270. cp5.get(Textfield.class, "width").setFocus(true);
  271. }
  272. focusHeight = !focusHeight;
  273. }
  274. }
  275. }
  276. //working: blender -b "HEADLESS.blend" --python "render.py" -- 80 80 "/Users/giers/Documents/Render Image for Webshop/55.png" "/Users/giers/Documents/Render Image for Webshop/"