diff --git a/constants.py b/constants.py index bdd0ec6..c329af4 100644 --- a/constants.py +++ b/constants.py @@ -29,7 +29,7 @@ class Global: COLOR_ID = "color" EMISSIVE_ID = "emissive" ROUGHNESS_ID = "roughness" - METALNESS_ID = "metalness" + METALLIC_ID = "metallic" NORMAL_NAME = NORMAL_ID.capitalize() CURVATURE_NAME = CURVATURE_ID.capitalize() @@ -40,7 +40,7 @@ class Global: COLOR_NAME = "Base Color" EMISSIVE_NAME = EMISSIVE_ID.capitalize() ROUGHNESS_NAME = ROUGHNESS_ID.capitalize() - METALNESS_NAME = METALNESS_ID.capitalize() + METALLIC_NAME = METALLIC_ID.capitalize() NORMAL_NODE = PREFIX + NORMAL_NAME CURVATURE_NODE = PREFIX + CURVATURE_NAME @@ -50,7 +50,7 @@ class Global: COLOR_NODE = PREFIX + COLOR_NAME EMISSIVE_NODE = PREFIX + EMISSIVE_NAME ROUGHNESS_NODE = PREFIX + ROUGHNESS_NAME - METALNESS_NODE = PREFIX + METALNESS_NAME + METALLIC_NODE = PREFIX + METALLIC_NAME ALL_MAP_IDS = ( NORMAL_ID, @@ -62,7 +62,7 @@ class Global: COLOR_ID, EMISSIVE_ID, ROUGHNESS_ID, - METALNESS_ID + METALLIC_ID ) ALL_MAP_NAMES = ( @@ -75,16 +75,17 @@ class Global: COLOR_NAME, EMISSIVE_NAME, ROUGHNESS_NAME, - METALNESS_NAME + METALLIC_NAME ) SHADER_MAP_NAMES = ( NORMAL_NODE, CURVATURE_NODE, + ALPHA_NODE, COLOR_NODE, EMISSIVE_NODE, ROUGHNESS_NODE, - METALNESS_NODE + METALLIC_NODE ) INVALID_BAKE_TYPES = ( diff --git a/operators/operators.py b/operators/operators.py index 1077368..d5ec41b 100644 --- a/operators/operators.py +++ b/operators/operators.py @@ -29,8 +29,6 @@ ) - - ################################################ # MISC ################################################ @@ -587,7 +585,7 @@ def draw(self, _context: Context): ################################################ -# CHANNEL PACKING +# TODO: CHANNEL PACKING ################################################ @@ -626,7 +624,7 @@ def draw(self, _context: Context): GRABDOC_OT_map_preview, GRABDOC_OT_leave_map_preview, GRABDOC_OT_export_current_preview, - GRABDOC_OT_config_maps + GRABDOC_OT_config_maps, #GRABDOC_OT_map_pack_info ) diff --git a/preferences.py b/preferences.py index fe1898b..eacc178 100644 --- a/preferences.py +++ b/preferences.py @@ -34,7 +34,7 @@ Curvature, Height, Id, - Metalness, + Metallic, Normals, Occlusion, Emissive, @@ -113,7 +113,7 @@ class GRABDOC_OT_add_preset(AddPresetBase, Operator): "gd.color", "gd.emissive", "gd.roughness", - "gd.metalness", + "gd.metallic", ] # Where to store the preset @@ -121,27 +121,13 @@ class GRABDOC_OT_add_preset(AddPresetBase, Operator): ############################################################ -# USER PREFERENCES +# PROPERTY GROUP ############################################################ -class GRABDOC_OT_check_for_update(Operator): - """Check GitHub releases page for newer version""" - bl_idname = "grab_doc.check_for_update" - bl_label = "" - bl_options = {'REGISTER', 'UNDO', 'INTERNAL'} - - def execute(self, _context: Context): - updater.check_for_update_now() - return {'FINISHED'} - - class GRABDOC_AP_preferences(AddonPreferences): bl_idname = __package__ - # NOTE: Special properties stored - # here are saved in User Preferences - marmo_executable: StringProperty( name="", description="Path to Marmoset Toolbag 3 or 4 executable", @@ -150,11 +136,6 @@ class GRABDOC_AP_preferences(AddonPreferences): ) -############################################################ -# PROPERTY GROUP -############################################################ - - class GRABDOC_property_group(PropertyGroup): MAP_TYPES = ( ('none', "None", ""), @@ -167,7 +148,7 @@ class GRABDOC_property_group(PropertyGroup): ('color', "Base Color", ""), ('emissive', "Emissive", ""), ('roughness', "Roughness", ""), - ('metalness', "Metalness", "") + ('metallic', "Metallic", "") ) def update_export_name(self, _context: Context): @@ -375,7 +356,7 @@ def update_scale(self, context: Context): color: CollectionProperty(type=Color) emissive: CollectionProperty(type=Emissive) roughness: CollectionProperty(type=Roughness) - metalness: CollectionProperty(type=Metalness) + metallic: CollectionProperty(type=Metallic) # Marmoset baking marmo_auto_bake: BoolProperty(name="Auto bake", default=True) @@ -425,7 +406,7 @@ def update_scale(self, context: Context): #) #channel_B: EnumProperty( # items=MAP_TYPES, - # default="metalness", + # default="metallic", # name='B' #) #channel_A: EnumProperty( @@ -453,10 +434,9 @@ def update_scale(self, context: Context): Color, Emissive, Roughness, - Metalness, + Metallic, GRABDOC_property_group, - GRABDOC_AP_preferences, - GRABDOC_OT_check_for_update + GRABDOC_AP_preferences ) def register(): diff --git a/ui.py b/ui.py index 054046e..dd5fc6f 100644 --- a/ui.py +++ b/ui.py @@ -397,9 +397,9 @@ class GRABDOC_PT_roughness(BakerPanel, PanelInfo, Panel): NAME = Global.ROUGHNESS_NAME -class GRABDOC_PT_metalness(BakerPanel, PanelInfo, Panel): - ID = Global.METALNESS_ID - NAME = Global.METALNESS_NAME +class GRABDOC_PT_metallic(BakerPanel, PanelInfo, Panel): + ID = Global.METALLIC_ID + NAME = Global.METALLIC_NAME ################################################ @@ -420,7 +420,7 @@ class GRABDOC_PT_metalness(BakerPanel, PanelInfo, Panel): GRABDOC_PT_color, GRABDOC_PT_emissive, GRABDOC_PT_roughness, - GRABDOC_PT_metalness + GRABDOC_PT_metallic ) # GRABDOC_PT_pack_maps diff --git a/utils/baker.py b/utils/baker.py index 4930d2b..ce96133 100644 --- a/utils/baker.py +++ b/utils/baker.py @@ -347,7 +347,7 @@ def update_curvature(self, context: Context): scene_shading.curvature_ridge_factor = self.ridge scene_shading.curvature_valley_factor = self.valley - def update_range(self, context: Context): + def update_range(self, _context: Context): color_ramp = \ bpy.data.node_groups[self.NODE].nodes.get("Color Ramp") color_ramp.color_ramp.elements[0].position = \ @@ -554,47 +554,6 @@ def update_guide(self, context: Context): ) -class Alpha(Baker, PropertyGroup): - ID = Global.ALPHA_ID - NAME = Global.ALPHA_NAME - NODE = Global.ALPHA_NODE - COLOR_SPACE = "sRGB" - VIEW_TRANSFORM = "Raw" - VIEW_TRANSFORM = "Standard" - MARMOSET_COMPATIBLE = False - SUPPORTED_ENGINES = ( - ('blender_eevee', "Eevee", ""), - ('cycles', "Cycles", "") - ) - - def draw_properties(self, context: Context, layout: UILayout): - col = layout.column() - if context.scene.gd.baker_type == 'blender': - col.prop(self, 'invert', text="Invert") - - def update_alpha(self, context: Context): - gd_camera_ob_z = bpy.data.objects.get( - Global.TRIM_CAMERA_NAME - ).location[2] - map_range = \ - bpy.data.node_groups[self.NODE].nodes.get('Map Range') - map_range.inputs[1].default_value = gd_camera_ob_z - .00001 - map_range.inputs[2].default_value = gd_camera_ob_z - invert = \ - bpy.data.node_groups[self.NODE].nodes.get('Invert') - invert.inputs[0].default_value = 0 if self.invert else 1 - - invert: BoolProperty( - description="Invert the Alpha mask", - update=update_alpha - ) - engine: EnumProperty( - items=SUPPORTED_ENGINES, - name='Render Engine', - update=Baker.apply_render_settings - ) - - class Id(Baker, PropertyGroup): ID = Global.MATERIAL_ID NAME = Global.MATERIAL_NAME @@ -671,12 +630,57 @@ def update_method(self, context: Context): ) +class Alpha(Baker, PropertyGroup): + ID = Global.ALPHA_ID + NAME = Global.ALPHA_NAME + NODE = Global.ALPHA_NODE + COLOR_SPACE = "sRGB" + VIEW_TRANSFORM = "Raw" + VIEW_TRANSFORM = "Standard" + MARMOSET_COMPATIBLE = False + SUPPORTED_ENGINES = ( + ('blender_eevee', "Eevee", ""), + ('cycles', "Cycles", "") + ) + + def draw_properties(self, context: Context, layout: UILayout): + col = layout.column() + if context.scene.gd.baker_type == 'blender': + col.prop(self, 'invert_depth', text="Invert Depth") + col.prop(self, 'invert_mask', text="Invert Mask") + + def update_alpha(self, _context: Context): + gd_camera_ob_z = \ + bpy.data.objects.get(Global.TRIM_CAMERA_NAME).location[2] + map_range = bpy.data.node_groups[self.NODE].nodes.get('Map Range') + map_range.inputs[1].default_value = gd_camera_ob_z - .00001 + map_range.inputs[2].default_value = gd_camera_ob_z + invert_depth = bpy.data.node_groups[self.NODE].nodes.get('Invert Depth') + invert_depth.inputs[0].default_value = 0 if self.invert_depth else 1 + invert_mask = bpy.data.node_groups[self.NODE].nodes.get('Invert Mask') + invert_mask.inputs[0].default_value = 0 if self.invert_mask else 1 + + invert_depth: BoolProperty( + description="Invert the global depth mask", + update=update_alpha + ) + invert_mask: BoolProperty( + description="Invert the alpha mask", + update=update_alpha + ) + engine: EnumProperty( + items=SUPPORTED_ENGINES, + name='Render Engine', + update=Baker.apply_render_settings + ) + + class Color(Baker, PropertyGroup): ID = Global.COLOR_ID NAME = Global.COLOR_NAME NODE = Global.COLOR_NODE COLOR_SPACE = "sRGB" - VIEW_TRANSFORM = "Raw" + VIEW_TRANSFORM = "Standard" MARMOSET_COMPATIBLE = False SUPPORTED_ENGINES = ( ('blender_eevee', "Eevee", ""), @@ -695,7 +699,7 @@ class Emissive(Baker, PropertyGroup): NAME = Global.EMISSIVE_NAME NODE = Global.EMISSIVE_NODE COLOR_SPACE = "sRGB" - VIEW_TRANSFORM = "Raw" + VIEW_TRANSFORM = "Standard" MARMOSET_COMPATIBLE = False SUPPORTED_ENGINES = ( ('blender_eevee', "Eevee", ""), @@ -727,8 +731,7 @@ def draw_properties(self, context: Context, layout: UILayout): col.prop(self, 'invert', text="Invert") def update_roughness(self, _context: Context): - invert = \ - bpy.data.node_groups[self.NODE].nodes.get('Invert') + invert = bpy.data.node_groups[self.NODE].nodes.get('Invert') invert.inputs[0].default_value = 1 if self.invert else 0 invert: BoolProperty( @@ -742,10 +745,10 @@ def update_roughness(self, _context: Context): ) -class Metalness(Baker, PropertyGroup): - ID = Global.METALNESS_ID - NAME = Global.METALNESS_NAME - NODE = Global.METALNESS_NODE +class Metallic(Baker, PropertyGroup): + ID = Global.METALLIC_ID + NAME = Global.METALLIC_NAME + NODE = Global.METALLIC_NODE COLOR_SPACE = "sRGB" VIEW_TRANSFORM = "Raw" MARMOSET_COMPATIBLE = False @@ -843,7 +846,7 @@ def reimport_as_material(map_names: list[str]) -> None: continue image.image = bpy.data.images.load(export_path, check_existing=True) - # NOTE: Unique exceptions for specific bake maps + # NOTE: Unique bake map exceptions if name not in (Global.COLOR_ID, Global.EMISSIVE_ID): image.image.colorspace_settings.name = 'Non-Color' if name == Global.NORMAL_ID: @@ -859,12 +862,12 @@ def reimport_as_material(map_names: list[str]) -> None: links.new(bsdf.inputs["Base Color"], image.outputs["Color"]) elif name == Global.EMISSIVE_ID: links.new(bsdf.inputs["Emission Color"], image.outputs["Color"]) - elif name == Global.METALNESS_ID: + elif name == Global.METALLIC_ID: links.new(bsdf.inputs["Metallic"], image.outputs["Color"]) elif name == Global.CURVATURE_ID: continue elif name == Global.OCCLUSION_ID: - # TODO: Could mix AO with Base Color in the future, not PBR + # TODO: Could mix AO with Base Color in the future continue elif name == Global.HEIGHT_ID: continue diff --git a/utils/generic.py b/utils/generic.py index e1b3367..d6f971b 100644 --- a/utils/generic.py +++ b/utils/generic.py @@ -2,7 +2,6 @@ import re from pathlib import Path from inspect import getframeinfo, stack -from pathlib import Path import bpy from bpy.types import Context, Operator @@ -184,7 +183,7 @@ def bad_setup_check( gd.color[0].enabled, gd.emissive[0].enabled, gd.roughness[0].enabled, - gd.metalness[0].enabled + gd.metallic[0].enabled ) bake_map_vis = ( gd.normals[0].visibility, @@ -196,7 +195,7 @@ def bad_setup_check( gd.color[0].visibility, gd.emissive[0].visibility, gd.roughness[0].visibility, - gd.metalness[0].visibility + gd.metallic[0].visibility ) if True not in bake_maps or True not in bake_map_vis: diff --git a/utils/node.py b/utils/node.py index 0e59e00..700e1e2 100644 --- a/utils/node.py +++ b/utils/node.py @@ -90,10 +90,10 @@ def node_init() -> None: bevel.inputs[0].default_value = 0 bevel.location = (-1000,0) - bevel_node_2 = tree.nodes.new('ShaderNodeBevel') - bevel_node_2.name = "Bevel.001" - bevel_node_2.location = (-1000,-200) - bevel_node_2.inputs[0].default_value = 0 + bevel_2 = tree.nodes.new('ShaderNodeBevel') + bevel_2.name = "Bevel.001" + bevel_2.location = (-1000,-200) + bevel_2.inputs[0].default_value = 0 vec_transform = tree.nodes.new('ShaderNodeVectorTransform') vec_transform.name = "Vector Transform" @@ -138,15 +138,13 @@ def node_init() -> None: # Link nodes links = tree.links - # NOTE: Branch 1 + links.new(bevel.inputs["Normal"], group_input.outputs["Normal"]) - links.new( - vec_transform.inputs["Vector"], bevel_node_2.outputs["Normal"] - ) + links.new(vec_transform.inputs["Vector"], bevel_2.outputs["Normal"]) links.new(vec_mult.inputs["Vector"], vec_transform.outputs["Vector"]) links.new(vec_add.inputs["Vector"], vec_mult.outputs["Vector"]) links.new(group_output.inputs["Shader"], vec_add.outputs["Vector"]) - # NOTE: Branch 2 + links.new(invert.inputs['Color'], group_input.outputs['Alpha']) links.new(subtract.inputs['Color2'], invert.outputs['Color']) links.new(mix_shader.inputs['Fac'], subtract.outputs['Color']) @@ -154,9 +152,7 @@ def node_init() -> None: links.new(mix_shader.inputs[2], vec_add.outputs['Vector']) if not Global.CURVATURE_NODE in bpy.data.node_groups: - tree = bpy.data.node_groups.new( - Global.CURVATURE_NODE, 'ShaderNodeTree' - ) + tree = bpy.data.node_groups.new(Global.CURVATURE_NODE, 'ShaderNodeTree') tree.use_fake_user = True # Create sockets @@ -260,27 +256,45 @@ def node_init() -> None: # Create sockets generate_shader_interface(tree, inputs) + alpha = tree.interface.new_socket( + name=Global.ALPHA_NAME, + socket_type='NodeSocketFloat' + ) + alpha.default_value = 1 # Create nodes group_output = tree.nodes.new('NodeGroupOutput') group_output.name = "Group Output" + group_input = tree.nodes.new('NodeGroupInput') + group_input.name = "Group Input" + group_input.location = (-1000, 200) camera = tree.nodes.new('ShaderNodeCameraData') camera.name = "Camera Data" - camera.location = (-800,0) + camera.location = (-1000, 0) camera_object_z = \ bpy.data.objects.get(Global.TRIM_CAMERA_NAME).location[2] map_range = tree.nodes.new('ShaderNodeMapRange') map_range.name = "Map Range" - map_range.location = (-600,0) + map_range.location = (-800, 0) map_range.inputs[1].default_value = camera_object_z - .00001 map_range.inputs[2].default_value = camera_object_z - invert = tree.nodes.new('ShaderNodeInvert') - invert.name = "Invert" - invert.location = (-400,0) + invert_mask = tree.nodes.new('ShaderNodeInvert') + invert_mask.name = "Invert Mask" + invert_mask.location = (-600, 200) + + invert_depth = tree.nodes.new('ShaderNodeInvert') + invert_depth.name = "Invert Depth" + invert_depth.location = (-600, 0) + + mix = tree.nodes.new('ShaderNodeMix') + mix.name = "Invert Mask" + mix.data_type = "RGBA" + mix.inputs["B"].default_value = (0, 0, 0, 1) + mix.location = (-400, 0) emission = tree.nodes.new('ShaderNodeEmission') emission.name = "Emission" @@ -288,9 +302,14 @@ def node_init() -> None: # Link nodes links = tree.links + links.new(invert_mask.inputs["Color"], group_input.outputs["Alpha"]) + links.new(mix.inputs["Factor"], invert_mask.outputs["Color"]) + links.new(map_range.inputs["Value"], camera.outputs["View Z Depth"]) - links.new(invert.inputs["Color"], map_range.outputs["Result"]) - links.new(emission.inputs["Color"], invert.outputs["Color"]) + links.new(invert_depth.inputs["Color"], map_range.outputs["Result"]) + links.new(mix.inputs["A"], invert_depth.outputs["Color"]) + + links.new(emission.inputs["Color"], mix.outputs["Result"]) links.new(group_output.inputs["Shader"], emission.outputs["Emission"]) if not Global.COLOR_NODE in bpy.data.node_groups: @@ -409,9 +428,9 @@ def node_init() -> None: emission.outputs["Emission"] ) - if not Global.METALNESS_NODE in bpy.data.node_groups: + if not Global.METALLIC_NODE in bpy.data.node_groups: tree = bpy.data.node_groups.new( - Global.METALNESS_NODE, 'ShaderNodeTree' + Global.METALLIC_NODE, 'ShaderNodeTree' ) tree.use_fake_user = True @@ -602,8 +621,8 @@ def apply_node_to_objects(name: str, objects: Iterable[Object]) -> bool: original_input=original_input, material=material ) - elif name == Global.METALNESS_NODE \ - and original_input.name == "Metallic": + elif name == Global.METALLIC_NODE \ + and original_input.name == Global.METALLIC_NAME: node_found = create_node_links( input_name=original_input.name, node_group=passthrough, @@ -619,7 +638,6 @@ def apply_node_to_objects(name: str, objects: Iterable[Object]) -> bool: material=material ) if original_input.name == 'Alpha' \ - and gd.normals[0].use_texture \ and material.blend_method == 'OPAQUE' \ and len(original_input.links): material.blend_method = 'CLIP' @@ -673,30 +691,29 @@ def node_cleanup(setup_type: str) -> None: mat for mat in nodes if mat.name.startswith(setup_type) ] for node in grabdoc_nodes: - output = None + output_node = None for output in node.outputs: for link in output.links: if link.to_node.type == 'OUTPUT_MATERIAL': - output = link.to_node + output_node = link.to_node break - if output is not None: + if output_node is not None: break - if output is None: + if output_node is None: nodes.remove(node) continue for node_input in node.inputs: for link in node_input.links: - original_node_connection = \ - nodes.get(link.from_node.name) - original_node_socket = link.from_socket.name if node_input.name.split(' ')[-1] not in inputs: continue + original_node_connection = nodes.get(link.from_node.name) + original_node_socket = link.from_socket.name for connection_name in inputs: if node_input.name != connection_name: continue mat.node_tree.links.new( - output.inputs[connection_name], + output_node.inputs[connection_name], original_node_connection.outputs[ original_node_socket ]