Browse Source

fixed outline occlusion issue

pull/1/head
Youen Toupin 3 years ago
parent
commit
c59bfba1d1
  1. 44
      ahb_cmd_render.py

44
ahb_cmd_render.py

@ -27,6 +27,8 @@ class AHB_Render:
obj.ViewObject.LineColor = line_color
obj.ViewObject.DisplayMode = 'Flat Lines' if not masked else 'Shaded'
obj.ViewObject.ShapeMaterial.AmbientColor = (0.0, 0.0, 0.0, 0.0)
obj.ViewObject.ShapeMaterial.DiffuseColor = (0.0, 0.0, 0.0, 0.0)
obj.ViewObject.ShapeMaterial.SpecularColor = (0.0, 0.0, 0.0, 0.0)
obj.ViewObject.ShapeMaterial.EmissiveColor = background_color if not masked else mask_color
def set_render_outlines(self, mask_stages_below: Optional[int] = None):
@ -43,7 +45,10 @@ class AHB_Render:
obj.ViewObject.ShapeMaterial.AmbientColor = (0.0, 0.0, 0.0, 0.0)
obj.ViewObject.ShapeMaterial.DiffuseColor = (0.0, 0.0, 0.0, 0.0)
obj.ViewObject.ShapeMaterial.SpecularColor = (0.0, 0.0, 0.0, 0.0)
obj.ViewObject.ShapeMaterial.EmissiveColor = (r/255.0, g/255.0, b/255.0, 0.0) if not masked else (1.0,1.0,1.0,0.0)
#obj.ViewObject.ShapeMaterial.EmissiveColor = (r/255.0, g/255.0, b/255.0, 0.0) if not masked else (1.0,1.0,1.0,0.0)
obj.ViewObject.ShapeMaterial.EmissiveColor = (r/255.0, g/255.0, b/255.0, 0.0)
if masked:
obj.ViewObject.Visibility = False
r = r + step
if r >= 256 - step:
@ -79,14 +84,17 @@ class AHB_Render:
temp_lines_file_name = tempfile.gettempdir() + "/ahb_temp_lines.png"
temp_shapes_file_name = tempfile.gettempdir() + "/ahb_temp_shapes.png"
self.set_render_outlines(mask_stages_below=mask_stages_below)
Gui.ActiveDocument.ActiveView.saveImage(temp_shapes_file_name, resolution[0] * 2, resolution[1] * 2, "#ffffff")
#temp_lines_file_name = filename + "-lines.png"
#temp_shapes_file_name = filename + "-shapes.png"
# render lines in black, background in red, fill shapes in green
# the green band contains the lines images, the red band contains the inverted alpha layer
self.set_render_lines((0.0,0.0,0.0), (0.0,1.0,0.0), mask_stages_below=mask_stages_below, mask_color=(1.0,0.0,0.0))
self.set_render_lines((0.0, 0.0, 0.0), (0.0, 1.0, 0.0), mask_stages_below=mask_stages_below, mask_color=(1.0, 0.0, 1.0))
Gui.ActiveDocument.ActiveView.saveImage(temp_lines_file_name, resolution[0], resolution[1], "#ff0000")
self.set_render_outlines(mask_stages_below=mask_stages_below)
Gui.ActiveDocument.ActiveView.saveImage(temp_shapes_file_name, resolution[0] * 2, resolution[1] * 2, "#ffffff")
self.reset_display()
lines_bands = Image.open(temp_lines_file_name).split()
@ -111,17 +119,20 @@ class AHB_Render:
else:
outlines.paste(partial_outlines, None, partial_outlines.point(lambda p: 0 if p == 255 else 255))
# erase masked outlines
outlines.paste(outlines.point(lambda p: 255), None, lines_bands[2].resize(outlines.size).point(lambda p: 255 if p == 255 else 0))
# outlines.save("/home/youen/dev_linux/vhelio-render/vhelio-outlines.png")
# outlines = outlines.resize(lines.size, Image.BILINEAR)
# lines.paste(outlines, None, outlines.point(lambda p: 255 - p))
lines_fullres = lines.resize(outlines.size, Image.NEAREST)
lines_fullres.paste(outlines, None, outlines.point(lambda p: 0 if p == 255 else 255))
lines_fullres.paste(outlines, None, outlines.point(lambda p: 255 if p == 0 else 0))
lines = lines_fullres.resize(lines.size, Image.BILINEAR)
alpha_band_fullres = alpha_band.resize(outlines.size, Image.NEAREST)
alpha_band_fullres.paste(outlines.point(lambda p: 255), None, outlines.point(lambda p: 0 if p == 255 else 255))
alpha_band_fullres.paste(outlines.point(lambda p: 255), None, outlines.point(lambda p: 255 if p == 0 else 0))
alpha_band = alpha_band_fullres.resize(lines.size, Image.BILINEAR)
outlines_end_time = time.perf_counter()
@ -154,8 +165,13 @@ class AHB_Render:
filename = os.path.splitext(doc_file_name)[0] + ".png"
dir = os.path.dirname(filename)
resolution = (2000,2000)
workbench.context.setAllStagesVisible(True)
self.render((2000, 2000), filename)
self.render(resolution, filename)
img_full = Image.new('RGB', resolution, (255, 255, 255))
img = Image.open(filename)
img_full.paste(img, None, img.getchannel('A'))
img_full.save(filename)
render_stages = True
if render_stages:
@ -171,17 +187,21 @@ class AHB_Render:
if prev_stage_id is not None:
workbench.context.setActiveStage(prev_stage_id)
workbench.context.setAllStagesVisible(False)
self.render(resolution, dir + "/stages/" + stage_name + "-bg.png", (0.5, 0.5, 0.5), (0.75,0.75,0.75))
self.render(resolution, dir + "/stages/" + stage_name + "-bg.png", (0.7, 0.7, 0.7), (1,1,1))
workbench.context.setActiveStage(stage_id)
workbench.context.setAllStagesVisible(False)
self.render(resolution, dir + "/stages/" + stage_name + ".png", mask_stages_below=stage_id)
# merge images
bg = Image.new('RGB', resolution, (255,255,255))
if prev_stage_id is not None:
# merge previous stages background with new stage
bg = Image.open(dir + "/stages/" + stage_name + "-bg.png")
prev_stage = Image.open(dir + "/stages/" + stage_name + "-bg.png")
os.remove(dir + "/stages/" + stage_name + "-bg.png")
new = Image.open(dir + "/stages/" + stage_name + ".png")
bg.paste(new, None, new.getchannel('A'))
bg.paste(prev_stage, None, prev_stage.getchannel('A'))
fg = Image.open(dir + "/stages/" + stage_name + ".png")
bg.paste(fg, None, fg.getchannel('A'))
bg.save(dir + "/stages/" + stage_name + ".png")
if prev_stage_id is not None:
pass
prev_stage_id = stage_id

Loading…
Cancel
Save