def test_material_values(self, out_dir): out_path = os.path.join(out_dir, 'pbr_material_values.usda') stage = usd.create_stage(out_path) tests = { 'Default': {}, 'Diffuse': { 'diffuse_color': (0., 1., 0.) }, 'Roughness': { 'roughness_value': 0.1 }, 'Metallic': { 'metallic_value': 1. }, 'Specular': { 'specular_color': (1., 0., 0.), 'is_specular_workflow': True }, } for test_name, params in tests.items(): prim = stage.DefinePrim(f'/World/{test_name}', 'Sphere') mat = materials.PBRMaterial(**params) mat.write_to_usd(out_path, f'/World/Looks/{test_name}', bound_prims=[prim], time=0) stage.Save() # Confirm exported USD matches golden file # TODO(jlafleche) Render the two mesh for visual comparison golden = os.path.join(out_dir, os.pardir, os.pardir, os.pardir, os.pardir, 'samples/golden/pbr_material_values.usda') assert open(golden).read() == open(out_path).read()
def test_colorspace(self, out_dir, mesh, material_textures): out_path = os.path.join(out_dir, 'colorspace_auto.usda') stage = usd.create_stage(out_path) def _create_checkerboard(val1, val2): channels = len(val1) checkerboard = torch.ones( (channels, 2, 2)) * torch.tensor(val1)[:, None, None] checkerboard[:, 0, 0] = torch.tensor(val2) checkerboard[:, 1, 1] = torch.tensor(val2) checkerboard = torch.nn.functional.interpolate(checkerboard[None, ...], scale_factor=128)[0] return checkerboard single_channel_texture = _create_checkerboard((0.2, ), (0.8, )) rgb_texture = _create_checkerboard((0., 0.4, 0.), (0., 0., 0.4)) texture = { 'metallic_texture': single_channel_texture, 'metallic_colorspace': 'auto', 'roughness_texture': single_channel_texture, 'roughness_colorspace': 'raw', 'diffuse_texture': rgb_texture, 'diffuse_colorspace': 'sRGB' } material = materials.PBRMaterial(**texture) prim = usd.add_mesh( stage, '/World/colorspace_test', mesh.vertices, mesh.faces, uvs=mesh.uvs, face_uvs_idx=mesh.face_uvs_idx, face_normals=mesh.vertex_normals[mesh.face_normals].view(-1, 3)) material.write_to_usd(out_path, '/World/Looks/colorspace_test', bound_prims=[prim]) material_in = materials.PBRMaterial().read_from_usd( out_path, '/World/Looks/colorspace_test') assert material_in.diffuse_colorspace == 'sRGB' assert material_in.metallic_colorspace == 'auto' assert material_in.roughness_colorspace == 'raw'
def write_to_usd(self, file_path, scene_path, bound_prims=None, time=None, texture_dir='', texture_file_prefix='', shader='UsdPreviewSurface'): r"""Write material to USD. Textures will be written to disk in with filename in the form: `{usd_file_path}/{texture_dir}/{texture_file_prefix}{attr}.png` where `attr` is one of [`diffuse`, `roughness`, `metallic`, `specular`, `normals`]. Args: file_path (str): Path to usd file (\*.usd, \*.usda). scene_path (str): Absolute path of material within the USD file scene. Must be a valid ``Sdf.Path``. bound_prims (list of Usd.Prim, optional): If provided, bind material to each prim. time (convertible to float, optional): Positive integer defining the time at which the supplied parameters correspond to. texture_dir (str, optional): Subdirectory to store texture files. If not provided, texture files will be saved in the same directory as the USD file specified by `file_path`. texture_file_prefix (str, optional): String to be prepended to the filename of each texture file. shader (str, optional): Name of shader to write. If not provided, use UsdPreviewSurface. """ assert os.path.splitext(file_path)[1] in [ '.usd', '.usda' ], f'Invalid file path "{file_path}".' assert shader in self.shaders, f'Shader {shader} is not support. Choose from {list(self.shaders.keys())}.' if os.path.exists(file_path): stage = Usd.Stage.Open(file_path) else: stage = usd.create_stage(file_path) if time is None: time = Usd.TimeCode.Default() writer = self.shaders[shader]['writer'] return writer(stage, scene_path, bound_prims, time, texture_dir, texture_file_prefix)
def test_material_textures(self, out_dir, mesh, material_textures): def _create_checkerboard(val1, val2): channels = len(val1) checkerboard = torch.ones( (channels, 2, 2)) * torch.tensor(val1)[:, None, None] checkerboard[:, 0, 0] = torch.tensor(val2) checkerboard[:, 1, 1] = torch.tensor(val2) checkerboard = torch.nn.functional.interpolate(checkerboard[None, ...], scale_factor=128)[0] return checkerboard out_path = os.path.join(out_dir, 'pbr_material_textures.usda') stage = usd.create_stage(out_path) tests = { 'Default': {}, 'Diffuse': { 'diffuse_texture': _create_checkerboard((0., 1., 0.), (0., 0., 1.)), 'diffuse_colorspace': 'sRGB' }, 'Roughness': { 'roughness_texture': _create_checkerboard((0.1, ), (0.9, )), 'roughness_colorspace': 'raw' }, 'Metallic': { 'metallic_texture': _create_checkerboard((0.1, ), (0.9, )), 'metallic_colorspace': 'raw' }, 'Clearcoat': { 'clearcoat_texture': _create_checkerboard((0.01, ), (0.9, )), 'metallic_colorspace': 'raw' }, 'ClearcoatRoughness': { 'clearcoat_roughness_texture': _create_checkerboard((0.1, ), (0.9, )), 'metallic_colorspace': 'raw' }, 'Opacity': { 'opacity_texture': _create_checkerboard((0.1, ), (0.9, )), 'metallic_colorspace': 'raw', 'opacity_threshold': 0.5 }, 'Ior': { 'ior_texture': _create_checkerboard((0.1, ), (0.9, )), 'metallic_colorspace': 'raw' }, 'Normal': { 'normals_texture': _create_checkerboard(( 0., 0., 1., ), (0., 0.5, 0.5)), 'normals_colorspace': 'raw' }, 'Specular': { 'specular_texture': _create_checkerboard((1., 0., 0.), (0., 0., 1.)), 'is_specular_workflow': True, 'specular_colorspace': 'raw' }, 'Displacement': { 'displacement_texture': _create_checkerboard((0.1, ), (0.9, )), 'specular_colorspace': 'raw' }, } for test_name, params in tests.items(): material_textures = materials.PBRMaterial(**params) prim = usd.add_mesh( stage, f'/World/{test_name}', mesh.vertices, mesh.faces, uvs=mesh.uvs, face_uvs_idx=mesh.face_uvs_idx, face_normals=mesh.vertex_normals[mesh.face_normals].view( -1, 3)) material_textures.write_to_usd(out_path, f'/World/Looks/{test_name}', bound_prims=[prim]) stage.Save() # Confirm exported USD matches golden file # TODO(jlafleche) Render the two mesh for visual comparison golden = os.path.join(out_dir, os.pardir, os.pardir, os.pardir, os.pardir, 'samples/golden/pbr_material_textures.usda') assert open(golden).read() == open(out_path).read()