def load_room(file_path: str) -> Tuple[dict, List[Command]]: from mixer.broadcaster.common import bytes_to_int, int_to_message_type import json # todo factorize file reading with network reading room_medata = None commands = [] with open(file_path, "rb") as f: data = f.read(4) string_length = bytes_to_int(data) attributes_string = f.read(string_length).decode() room_medata = json.loads(attributes_string) while True: prefix_size = 14 msg = f.read(prefix_size) if not msg: break frame_size = bytes_to_int(msg[:8]) command_id = bytes_to_int(msg[8:12]) message_type = bytes_to_int(msg[12:]) msg = f.read(frame_size) commands.append( Command(int_to_message_type(message_type), msg, command_id)) assert room_medata is not None return room_medata, commands
def build_material(data): material_name_length = common.bytes_to_int(data[:4]) start = 4 end = start + material_name_length material_name = data[start:end].decode() start = end material = get_or_create_material(material_name) nodes = material.node_tree.nodes # Get a principled node principled = None if nodes: for n in nodes: if n.type == "BSDF_PRINCIPLED": principled = n break if not principled: logger.error("Cannot find Principled BSDF node") return index = start # Transmission ( 1 - opacity) transmission, index = common.decode_float(data, index) transmission = 1 - transmission principled.inputs["Transmission"].default_value = transmission file_name, index = common.decode_string(data, index) if len(file_name) > 0: invert = material.node_tree.nodes.new("ShaderNodeInvert") material.node_tree.links.new(principled.inputs["Transmission"], invert.outputs["Color"]) tex_image = material.node_tree.nodes.new("ShaderNodeTexImage") try: tex_image.image = bpy.data.images.load(get_resolved_file_path(file_name)) tex_image.image.colorspace_settings.name = "Non-Color" except Exception as e: logger.error("could not load file %s ...", get_resolved_file_path(file_name)) logger.error("... %s", e) material.node_tree.links.new(invert.inputs["Color"], tex_image.outputs["Color"]) # Base Color base_color, index = common.decode_color(data, index) material.diffuse_color = (base_color[0], base_color[1], base_color[2], 1) principled.inputs["Base Color"].default_value = material.diffuse_color index = build_texture(principled, material, "Base Color", True, data, index) # Metallic material.metallic, index = common.decode_float(data, index) principled.inputs["Metallic"].default_value = material.metallic index = build_texture(principled, material, "Metallic", False, data, index) # Roughness material.roughness, index = common.decode_float(data, index) principled.inputs["Roughness"].default_value = material.roughness index = build_texture(principled, material, "Roughness", False, data, index) # Normal file_name, index = common.decode_string(data, index) if len(file_name) > 0: normal_map = material.node_tree.nodes.new("ShaderNodeNormalMap") material.node_tree.links.new(principled.inputs["Normal"], normal_map.outputs["Normal"]) tex_image = material.node_tree.nodes.new("ShaderNodeTexImage") try: tex_image.image = bpy.data.images.load(get_resolved_file_path(file_name)) tex_image.image.colorspace_settings.name = "Non-Color" except Exception as e: logger.error("could not load file %s ...", get_resolved_file_path(file_name)) logger.error("... %s", e) material.node_tree.links.new(normal_map.inputs["Color"], tex_image.outputs["Color"]) # Emission emission, index = common.decode_color(data, index) principled.inputs["Emission"].default_value = emission index = build_texture(principled, material, "Emission", False, data, index)