From bd6a181984f1b4b75995542644d8fff925130e68 Mon Sep 17 00:00:00 2001 From: Elie Michel Date: Sat, 24 Oct 2020 11:32:07 +0200 Subject: [PATCH] Handle captures with missing textures --- blender/MapsModelsImporter/google_maps.py | 17 ++++++---- blender/MapsModelsImporter/google_maps_rd.py | 33 +++++++++++--------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/blender/MapsModelsImporter/google_maps.py b/blender/MapsModelsImporter/google_maps.py index 136cfa9..ce9384a 100644 --- a/blender/MapsModelsImporter/google_maps.py +++ b/blender/MapsModelsImporter/google_maps.py @@ -30,7 +30,7 @@ SCRIPT_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "google_maps_rd.py") MSG_INCORRECT_RDC = """Invalid RDC capture file. Please make sure that: -1. You are importing from Google Maps (NOT Google Earth) +1. You are importing from Google Maps or Google Earth web 2. You were MOVING in the 3D view while taking the capture (you can use the Capture after delay button in RenderDoc). Please report to MapsModelsImporter developers providing the .rdc file as well as the full console log. Console log is accessible in Windows > Toggle System Console (right click to copy).""" @@ -166,10 +166,11 @@ def addImageMaterial(name, obj, img): principled = nodes["Principled BSDF"] principled.inputs["Specular"].default_value = 0.0 principled.inputs["Roughness"].default_value = 1.0 - texture_node = nodes.new(type="ShaderNodeTexImage") - texture_node.image = img - links = mat.node_tree.links - link = links.new(texture_node.outputs[0], principled.inputs[0]) + if img is not None: + texture_node = nodes.new(type="ShaderNodeTexImage") + texture_node.image = img + links = mat.node_tree.links + link = links.new(texture_node.outputs[0], principled.inputs[0]) def loadData(prefix, drawcall_id): with open("{}{:05d}-indices.bin".format(prefix, drawcall_id), 'rb') as file: @@ -181,7 +182,11 @@ def loadData(prefix, drawcall_id): with open("{}{:05d}-uv.bin".format(prefix, drawcall_id), 'rb') as file: uvs = pickle.load(file) - img = bpy.data.images.load("{}{:05d}-texture.png".format(prefix, drawcall_id)) + texture_filename = "{}{:05d}-texture.png".format(prefix, drawcall_id) + if os.path.isfile(texture_filename): + img = bpy.data.images.load(texture_filename) + else: + img = None with open("{}{:05d}-constants.bin".format(prefix, drawcall_id), 'rb') as file: constants = pickle.load(file) diff --git a/blender/MapsModelsImporter/google_maps_rd.py b/blender/MapsModelsImporter/google_maps_rd.py index a8cb198..1c4139b 100644 --- a/blender/MapsModelsImporter/google_maps_rd.py +++ b/blender/MapsModelsImporter/google_maps_rd.py @@ -233,20 +233,25 @@ def run(self): with open("{}{:05d}-constants.bin".format(FILEPREFIX, drawcallId), 'wb') as file: pickle.dump(constants, file) - # Texture - # dirty - bindpoints = state.GetBindpointMapping(rd.ShaderStage.Fragment) - texture_bind = bindpoints.samplers[-1].bind - resources = state.GetReadOnlyResources(rd.ShaderStage.Fragment) - rid = resources[texture_bind].resources[0].resourceId - - texsave = rd.TextureSave() - texsave.resourceId = rid - texsave.mip = 0 - texsave.slice.sliceIndex = 0 - texsave.alpha = rd.AlphaMapping.Preserve - texsave.destType = rd.FileType.PNG - controller.SaveTexture(texsave, "{}{:05d}-texture.png".format(FILEPREFIX, drawcallId)) + self.extractTexture(drawcallId, state) + + def extractTexture(self, drawcallId, state): + """Save the texture in a png file (A bit dirty)""" + bindpoints = state.GetBindpointMapping(rd.ShaderStage.Fragment) + if not bindpoints.samplers: + print(f"Warning: No texture found for drawcall {drawcallId}") + return + texture_bind = bindpoints.samplers[-1].bind + resources = state.GetReadOnlyResources(rd.ShaderStage.Fragment) + rid = resources[texture_bind].resources[0].resourceId + + texsave = rd.TextureSave() + texsave.resourceId = rid + texsave.mip = 0 + texsave.slice.sliceIndex = 0 + texsave.alpha = rd.AlphaMapping.Preserve + texsave.destType = rd.FileType.PNG + controller.SaveTexture(texsave, "{}{:05d}-texture.png".format(FILEPREFIX, drawcallId)) def main(controller): scraper = CaptureScraper(controller)