Skip to content

Commit

Permalink
GUI, background fixes, further octane work, image tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Naxela committed Jan 24, 2021
1 parent 7cdf23f commit 4004816
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 46 deletions.
Binary file added addon/assets/TLM_Overlay.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 49 additions & 1 deletion addon/operators/imagetools.py
@@ -1,4 +1,4 @@
import bpy, os, time, cv2
import bpy, os, time, importlib

class TLM_ImageUpscale(bpy.types.Operator):
bl_idname = "tlm.image_upscale"
Expand All @@ -8,6 +8,14 @@ class TLM_ImageUpscale(bpy.types.Operator):

def invoke(self, context, event):

cv2 = importlib.util.find_spec("cv2")

if cv2 is None:
print("CV2 not found - Ignoring filtering")
return 0
else:
cv2 = importlib.__import__("cv2")

for area in bpy.context.screen.areas:
if area.type == "IMAGE_EDITOR":
active_image = area.spaces.active.image
Expand Down Expand Up @@ -134,4 +142,44 @@ def invoke(self, context, event):

print("Upscale")

return {'RUNNING_MODAL'}

class TLM_ImageSwitchUp(bpy.types.Operator):
bl_idname = "tlm.image_switchup"
bl_label = "Quickswitch Up"
bl_description = "Switches to a cached upscaled image"
bl_options = {'REGISTER', 'UNDO'}

def invoke(self, context, event):

for area in bpy.context.screen.areas:
if area.type == "IMAGE_EDITOR":
active_image = area.spaces.active.image

if active_image.source == "FILE":
img_path = active_image.filepath_raw
filename = os.path.basename(img_path)

print("Switch up")

return {'RUNNING_MODAL'}

class TLM_ImageSwitchDown(bpy.types.Operator):
bl_idname = "tlm.image_switchdown"
bl_label = "Quickswitch Down"
bl_description = "Switches to a cached downscaled image"
bl_options = {'REGISTER', 'UNDO'}

def invoke(self, context, event):

for area in bpy.context.screen.areas:
if area.type == "IMAGE_EDITOR":
active_image = area.spaces.active.image

if active_image.source == "FILE":
img_path = active_image.filepath_raw
filename = os.path.basename(img_path)

print("Switch Down")

return {'RUNNING_MODAL'}
8 changes: 8 additions & 0 deletions addon/panels/image.py
Expand Up @@ -46,9 +46,17 @@ def draw(self, context):
row = layout.row(align=True)
row.prop(activeImg.TLM_ImageProperties, "tlm_image_scale_engine")
row = layout.row(align=True)
row.prop(activeImg.TLM_ImageProperties, "tlm_image_cache_switch")
row = layout.row(align=True)
row.operator("tlm.image_upscale")
if activeImg.TLM_ImageProperties.tlm_image_cache_switch:
row = layout.row(align=True)
row.label(text ="Switch up.")
row = layout.row(align=True)
row.operator("tlm.image_downscale")
if activeImg.TLM_ImageProperties.tlm_image_cache_switch:
row = layout.row(align=True)
row.label(text ="Switch down.")
if activeImg.TLM_ImageProperties.tlm_image_scale_engine == "OpenCV":
row = layout.row(align=True)
row.prop(activeImg.TLM_ImageProperties, "tlm_image_scale_method")
Expand Down
84 changes: 49 additions & 35 deletions addon/panels/scene.py
Expand Up @@ -156,7 +156,9 @@ def draw(self, context):
engineProperties = scene.TLM_Engine3Properties

#LUXCORE SETTINGS HERE
octane_available = False
octane_available = True



row = layout.row(align=True)
row.operator("tlm.build_lightmaps")
Expand Down Expand Up @@ -479,49 +481,59 @@ def draw(self, context):
else:

layout.label(text="Postpacking is unstable.")
rows = 2
if len(atlasList) > 1:
rows = 4
row = layout.row()
row.template_list("TLM_UL_PostAtlasList", "PostList", scene, "TLM_PostAtlasList", scene, "TLM_PostAtlasListItem", rows=rows)
col = row.column(align=True)
col.operator("tlm_postatlaslist.new_item", icon='ADD', text="")
col.operator("tlm_postatlaslist.delete_item", icon='REMOVE', text="")

if postatlasListItem >= 0 and len(postatlasList) > 0:
item = postatlasList[postatlasListItem]
layout.prop(item, "tlm_atlas_lightmap_resolution")
cv2 = importlib.util.find_spec("cv2")

#Below list object counter
amount = 0
utilized = 0
atlasUsedArea = 0
atlasSize = item.tlm_atlas_lightmap_resolution
if cv2 is None:

for obj in bpy.data.objects:
if obj.TLM_ObjectProperties.tlm_mesh_lightmap_use:
if obj.TLM_ObjectProperties.tlm_postpack_object:
if obj.TLM_ObjectProperties.tlm_postatlas_pointer == item.name:
amount = amount + 1

atlasUsedArea += int(obj.TLM_ObjectProperties.tlm_mesh_lightmap_resolution) ** 2
row = layout.row(align=True)
row.label(text="OpenCV is not installed. Install it through preferences.")

row = layout.row()
row.prop(item, "tlm_atlas_repack_on_cleanup")
else:

#TODO SET A CHECK FOR THIS! ADD A CV2 CHECK TO UTILITY!
cv2 = True
rows = 2
if len(atlasList) > 1:
rows = 4
row = layout.row()
row.template_list("TLM_UL_PostAtlasList", "PostList", scene, "TLM_PostAtlasList", scene, "TLM_PostAtlasListItem", rows=rows)
col = row.column(align=True)
col.operator("tlm_postatlaslist.new_item", icon='ADD', text="")
col.operator("tlm_postatlaslist.delete_item", icon='REMOVE', text="")

if postatlasListItem >= 0 and len(postatlasList) > 0:
item = postatlasList[postatlasListItem]
layout.prop(item, "tlm_atlas_lightmap_resolution")

#Below list object counter
amount = 0
utilized = 0
atlasUsedArea = 0
atlasSize = item.tlm_atlas_lightmap_resolution

for obj in bpy.data.objects:
if obj.TLM_ObjectProperties.tlm_mesh_lightmap_use:
if obj.TLM_ObjectProperties.tlm_postpack_object:
if obj.TLM_ObjectProperties.tlm_postatlas_pointer == item.name:
amount = amount + 1

atlasUsedArea += int(obj.TLM_ObjectProperties.tlm_mesh_lightmap_resolution) ** 2

if cv2:
row = layout.row()
row.prop(item, "tlm_atlas_dilation")
layout.label(text="Objects: " + str(amount))
row.prop(item, "tlm_atlas_repack_on_cleanup")

#TODO SET A CHECK FOR THIS! ADD A CV2 CHECK TO UTILITY!
cv2 = True

utilized = atlasUsedArea / (int(atlasSize) ** 2)
layout.label(text="Utilized: " + str(utilized * 100) + "%")
if cv2:
row = layout.row()
row.prop(item, "tlm_atlas_dilation")
layout.label(text="Objects: " + str(amount))

if (utilized * 100) > 100:
layout.label(text="Warning! Overflow not yet supported")
utilized = atlasUsedArea / (int(atlasSize) ** 2)
layout.label(text="Utilized: " + str(utilized * 100) + "%")

if (utilized * 100) > 100:
layout.label(text="Warning! Overflow not yet supported")

row = layout.row()
row.label(text="Build Environment Probes")
Expand All @@ -537,7 +549,9 @@ def draw(self, context):
row.prop(sceneProperties, "tlm_environment_probe_resolution")
row = layout.row()
row.prop(sceneProperties, "tlm_create_spherical")

if sceneProperties.tlm_create_spherical:

row = layout.row()
row.prop(sceneProperties, "tlm_invert_direction")
row = layout.row()
Expand Down
4 changes: 4 additions & 0 deletions addon/properties/image.py
Expand Up @@ -20,3 +20,7 @@ class TLM_ImageProperties(bpy.types.PropertyGroup):
description="TODO",
default='Lanczos')

tlm_image_cache_switch : BoolProperty(
name="Cache for quickswitch",
description="Caches scaled images for quick switching",
default=True)
6 changes: 5 additions & 1 deletion addon/properties/renderer/octanerender.py
Expand Up @@ -3,4 +3,8 @@

class TLM_OctanerenderSceneProperties(bpy.types.PropertyGroup):

pass
tlm_lightmap_savedir : StringProperty(
name="Lightmap Directory",
description="TODO",
default="Lightmaps",
subtype="FILE_PATH")
52 changes: 44 additions & 8 deletions addon/utility/build.py
@@ -1,12 +1,14 @@
import bpy, os, subprocess, sys, platform, aud, json, datetime, socket
import threading

from . import encoding, pack
from . cycles import lightmap, prepare, nodes, cache
from . luxcore import setup
from . octane import configure
from . octane import configure, lightmap2
from . denoiser import integrated, oidn, optix
from . filtering import opencv
from . gui import Viewport
from .. network import client

from os import listdir
from os.path import isfile, join
from time import time, sleep
Expand All @@ -31,6 +33,9 @@ def prepare_build(self=0, background_mode=False, shutdown_after_build=False):
scene = bpy.context.scene
sceneProperties = scene.TLM_SceneProperties

if not background_mode:
setGui(1)

#Timer start here bound to global

if check_save():
Expand Down Expand Up @@ -186,12 +191,15 @@ def distribute_building():
json.dump(process_status, file, indent=2)

if (2, 91, 0) > bpy.app.version:
bpy.app.driver_namespace["tlm_process"] = subprocess.Popen([sys.executable,"-b", blendPath,"--python-expr",'import bpy; import thelightmapper; thelightmapper.addon.utility.build.prepare_build(0, True);'], shell=False, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
bpy.app.driver_namespace["tlm_process"] = subprocess.Popen([sys.executable,"-b", blendPath,"--python-expr",'import bpy; import thelightmapper; thelightmapper.addon.utility.build.prepare_build(0, True);'], shell=False, stdout=subprocess.PIPE)
else:
bpy.app.driver_namespace["tlm_process"] = subprocess.Popen([sys.executable,"-b", blendPath,"--python-expr",'import bpy; import thelightmapper; thelightmapper.addon.utility.build.prepare_build(0, True);'], shell=False, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
else:
bpy.app.driver_namespace["tlm_process"] = subprocess.Popen([bpy.app.binary_path,"-b", blendPath,"--python-expr",'import bpy; import thelightmapper; thelightmapper.addon.utility.build.prepare_build(0, True);'], shell=False, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)



if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
bpy.app.driver_namespace["tlm_process"] = subprocess.Popen([bpy.app.binary_path,"-b", blendPath,"--python-expr",'import bpy; import thelightmapper; thelightmapper.addon.utility.build.prepare_build(0, True);'], shell=False, stdout=subprocess.PIPE)
else:
bpy.app.driver_namespace["tlm_process"] = subprocess.Popen([bpy.app.binary_path,"-b", blendPath,"--python-expr",'import bpy; import thelightmapper; thelightmapper.addon.utility.build.prepare_build(0, True);'], shell=False, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
print("Started process: " + str(bpy.app.driver_namespace["tlm_process"]) + " at " + str(datetime.datetime.now()))

Expand Down Expand Up @@ -267,7 +275,8 @@ def begin_build():
pass

if sceneProperties.tlm_lightmap_engine == "OctaneRender":
pass

lightmap2.bake()

#Denoiser
if sceneProperties.tlm_denoise_use:
Expand Down Expand Up @@ -704,6 +713,9 @@ def manage_build(background_pass=False):

print("Lightmap building finished")

if not background_pass:
setGui(0)

if bpy.app.background:

if bpy.context.scene.TLM_SceneProperties.tlm_verbose:
Expand Down Expand Up @@ -852,10 +864,34 @@ def sec_to_hours(seconds):
return d

def setMode():

obj = bpy.context.scene.objects[0]
bpy.context.view_layer.objects.active = obj
obj.select_set(True)

bpy.ops.object.mode_set(mode='OBJECT')

#TODO Make some checks that returns to previous selection

def setGui(mode):

if mode == 0:

context = bpy.context
driver = bpy.app.driver_namespace

if "TLM_UI" in driver:
driver["TLM_UI"].remove_handle()

if mode == 1:

bpy.context.area.tag_redraw()
context = bpy.context
driver = bpy.app.driver_namespace
driver["TLM_UI"] = Viewport.ViewportDraw(context, "Building Lightmaps")

bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)

def checkAtlasSize():

overflow = False
Expand Down
66 changes: 66 additions & 0 deletions addon/utility/gui/Viewport.py
@@ -0,0 +1,66 @@
import bpy, blf, bgl, os, gpu
from gpu_extras.batch import batch_for_shader

class ViewportDraw:

def __init__(self, context, text):

bakefile = "TLM_Overlay.png"
scriptDir = os.path.dirname(os.path.realpath(__file__))
bakefile_path = os.path.abspath(os.path.join(scriptDir, '..', '..', 'assets/' + bakefile))

bpy.ops.image.open(filepath=bakefile_path)

#print("Self path: " + bakefile_path)

image_name = "TLM_Overlay.png"

image = bpy.data.images[image_name]

x = 15
y = 15
w = 400
h = 200

self.shader = gpu.shader.from_builtin('2D_IMAGE')
self.batch = batch_for_shader(
self.shader, 'TRI_FAN',
{
"pos": ((x, y), (x+w, y), (x+w, y+h), (x, y+h)),
"texCoord": ((0, 0), (1, 0), (1, 1), (0, 1)),
},
)

if image.gl_load():
raise Exception()

self.text = text
self.image = image
#self.handle = bpy.types.SpaceView3D.draw_handler_add(self.draw_text_callback, (context,), 'WINDOW', 'POST_PIXEL')
self.handle2 = bpy.types.SpaceView3D.draw_handler_add(self.draw_image_callback, (context,), 'WINDOW', 'POST_PIXEL')

def draw_text_callback(self, context):

font_id = 0
blf.position(font_id, 15, 15, 0)
blf.size(font_id, 20, 72)
blf.draw(font_id, "%s" % (self.text))

def draw_image_callback(self, context):

bgl.glEnable(bgl.GL_BLEND)
bgl.glActiveTexture(bgl.GL_TEXTURE0)
bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.image.bindcode)

self.shader.bind()
self.shader.uniform_int("image", 0)
self.batch.draw(self.shader)
bgl.glDisable(bgl.GL_BLEND)

def update_text(self, text):

self.text = text

def remove_handle(self):
#bpy.types.SpaceView3D.draw_handler_remove(self.handle, 'WINDOW')
bpy.types.SpaceView3D.draw_handler_remove(self.handle2, 'WINDOW')

0 comments on commit 4004816

Please sign in to comment.