From 9a6f333f23b64cc4f1a73235ab9dabe7bd13aacf Mon Sep 17 00:00:00 2001 From: teachmain Date: Mon, 11 Dec 2023 17:49:20 +0800 Subject: [PATCH 1/7] update: --- projects/FBX/scripts/blender.py | 56 +++++++++++++-------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/projects/FBX/scripts/blender.py b/projects/FBX/scripts/blender.py index 6f8c998769..967d72bd27 100644 --- a/projects/FBX/scripts/blender.py +++ b/projects/FBX/scripts/blender.py @@ -23,14 +23,11 @@ assert os.path.exists(args.input) -export_all = False -input = args.input -output = args.output -output_json = os.path.join(output, "info.json") -abc_output_dir = os.path.join(output, "abc") -if not os.path.exists(abc_output_dir): - os.makedirs(abc_output_dir) +blender_file = args.input +output_directory = args.output +json_output = os.path.join(output_directory, "info.json") +abc_output = os.path.join(output_directory, "output.abc") """ { @@ -73,27 +70,21 @@ } } """ -info = {} -obj2mat = {} +material_info = {} + + -# Replace 'your_file_path.blend' with the path to your .blend file -blend_file_path = input # Open the .blend file -bpy.ops.wm.open_mainfile(filepath=blend_file_path) +bpy.ops.wm.open_mainfile(filepath=blender_file) -# Replace 'your_output_path.abc' with the desired output Alembic file path -export_path = output -# Export Alembic with animation and hierarchy -if export_all: - bpy.ops.wm.alembic_export(filepath=export_path) +bpy.ops.wm.alembic_export(filepath=abc_output,face_sets=True,use_instancing=False) + -for obj in bpy.data.objects: - obj.select_set(False) # Iterate through all objects in the scene and find meshes for obj in bpy.context.scene.objects: - gc.collect() + if obj.type == 'MESH': material_params = {} material_textures = {} @@ -102,20 +93,12 @@ obj_name = obj.name_full print(" Mesh Full:", obj_name) - # Select the current object and deselect all others - bpy.context.view_layer.objects.active = obj - #for other_obj in bpy.data.objects: - # other_obj.select_set(other_obj == obj) - obj.select_set(True) + matName = "Default" if obj.material_slots: for material_slot in obj.material_slots: matName = material_slot.material.name - if not os.path.exists(abc_output_dir + '/' + matName): - os.makedirs(abc_output_dir + '/' + matName) - abc_file_path = os.path.join(abc_output_dir, f"{matName}/{obj_name}.abc") - bpy.ops.wm.alembic_export(filepath=abc_file_path, selected=True) # Check if the object has a material if obj.material_slots: @@ -126,6 +109,9 @@ # Material name print(" Material Name:", material.name) + matkey = material.name + if (matkey in material_info): + continue # Iterate through material nodes if material.use_nodes: @@ -255,16 +241,16 @@ material_params["normal"] = {"value": normal[:], "name": "Normal"} material_params["clearcoat_normal"] = {"value": clearcoat_normal[:], "name": "Clearcoat Normal"} material_params["tangent"] = {"value": tangent[:], "name": "Tangent"} - matkey = material.name + '/' + obj_name - if not (matkey in info) : - info[matkey] = material_params + + material_info[matkey] = material_params print("-----") - obj.select_set(False) + gc.collect() +print(json_output) # Serializing json -json_object = json.dumps(info, indent=4) +json_object = json.dumps(material_info, indent=4) # Writing to sample.json -with open(output_json, "w") as outfile: +with open(json_output, "w") as outfile: outfile.write(json_object) From 117918385510ff0b8e99214a38711d24f8c414eb Mon Sep 17 00:00:00 2001 From: teachmain Date: Thu, 14 Dec 2023 19:42:42 +0800 Subject: [PATCH 2/7] update --- projects/FBX/scripts/test.py | 159 +++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 projects/FBX/scripts/test.py diff --git a/projects/FBX/scripts/test.py b/projects/FBX/scripts/test.py new file mode 100644 index 0000000000..0048db9df1 --- /dev/null +++ b/projects/FBX/scripts/test.py @@ -0,0 +1,159 @@ +# Open a .blender file and export abc mesh and material infos. +# +# To use this script, you need install bpy module first or build blender python module from source. +# python -m pip install --user bpy +# +# https://pypi.org/project/bpy/#history +# https://builder.blender.org/download/bpy/ + +import gc +import os +import bpy +import json +import argparse +from pprint import pprint + +parser = argparse.ArgumentParser() +parser.add_argument("-i", "--input", help="blender file full path") +parser.add_argument("-o", "--output", help="output alembic full path") +args = parser.parse_args() + +print("---- Input Path", args.input) +print("---- Output Path", args.output) + +assert os.path.exists(args.input) + + +blender_file = args.input +output_directory = args.output +json_output = os.path.join(output_directory, "info.json") +abc_output = os.path.join(output_directory, "output.abc") + +material_info = {} + + + + +# Open the .blend file +def BlenderToZenoMap(): + mat_map = {} + mat_map["Base Color"] = "basecolor" + mat_map["Subsurface"] = "subsurface" + mat_map["Metallic"] = "metallic" + mat_map["Specular"] = "specular" + mat_map["Roughness"] = "roughness" + mat_map["Anisotropic"] = "anisotropic" + mat_map["Anisotropic Rotation"] = "anisoRotation" + mat_map["IOR"] = "ior" + mat_map["Sheen"] = "sheen" + mat_map["Sheen Tint"] = "sheenTint" + mat_map["Alpha"] = "opacity" + mat_map["Normal"] = "normal" + mat_map["Transmission"] = "specTrans" + mat_map["Emission"] = "emission" + mat_map["Emission Strength"] = "emissionIntensity" + mat_map["Clearcoat"] = "clearcoat" + mat_map["Clearcoat Roughness"] = "clearcoatRoughness" + return mat_map + + +def FindNodeToSocket(socket,node_bl_label): + if (not socket.is_linked): + return None,None + else: + links = socket.links + for link in links: + if(link.from_node.bl_label == node_bl_label): + return link.from_node, link + + return None,None + +def EvalTextureForSocket(socket): + path = "" + postprocess = "srgb" + channel = "vec3" + scale = [1.0,1.0] + PP_node=None + image_node = None + link = None + + P_node,link = FindNodeToSocket(socket,"Separate Color") + if (not(P_node is None)): + channel = link.from_socket.name + PP_node = P_node + + P_node,link = FindNodeToSocket(socket,"Normal Map") + if (not(P_node is None)): + postprocess = "normal_map" + PP_node = P_node + + if (PP_node is None): + image_node,link = FindNodeToSocket(socket,"Image Texture") + else: + image_node,link = FindNodeToSocket(PP_node.inputs["Color"],"Image Texture") + + if (not (image_node is None)): + if (not (image_node.image is None)): + path = image_node.image.filepath + + return path,postprocess,channel,scale + + + +def EvalBSDFNode(bsdf_node): + mat = {} + for socket in bsdf_node.inputs: + mat[socket.name]={} + if isinstance(socket.default_value,(float,int)): #it is not a bpy_prop_array + mat[socket.name]["value"] = socket.default_value + else: + mat[socket.name]["value"] = socket.default_value[:] + + if(socket.is_linked): + path,postpro,chennal,scale = EvalTextureForSocket(socket) + mat[socket.name]["path"] = path + mat[socket.name]["postpro"] = postpro + mat[socket.name]["chennal"] = chennal + mat[socket.name]["scale"] = scale + + return mat + + +def Mat2Json(material): + mat_name = material.name + print("Material Name is {}".format(mat_name)) + if (mat.use_nodes): + mat_ouput_node = None + mat_info = None + + for node in mat.node_tree.nodes: + if (node.bl_label=="Material Output"): + mat_ouput_node = node + + if (mat_ouput_node is None): + print("Can not found \"Materail Ouput\" node in {}".format(mat_name)) + return None,None + + bsdf_node,link = FindNodeToSocket(mat_ouput_node.inputs["Surface"],"Principled BSDF") + + if (bsdf_node is None): + print("Can not found \"Principled BSDF\" node in {}".format(mat_name) ) + else: + mat_info = EvalBSDFNode(bsdf_node) + + return mat_name,mat_info + + return None,None + +bpy.ops.wm.open_mainfile(filepath=blender_file) + +for mat in bpy.data.materials: + mat_name,mat_info = Mat2Json(mat) + if(not (mat_name is None)): + material_info[mat_name] = mat_info + +js = json.dumps(material_info,indent=4) +print(js) + + + From 0d9b09c32a58d40088e877f5f94546a4c2b80798 Mon Sep 17 00:00:00 2001 From: teachmain Date: Tue, 19 Dec 2023 19:42:34 +0800 Subject: [PATCH 3/7] update blender import --- projects/FBX/scripts/blender.py | 349 +++++++++--------------- ui/zenoedit/nodesys/readfbxprim.cpp | 406 ++++++---------------------- 2 files changed, 212 insertions(+), 543 deletions(-) diff --git a/projects/FBX/scripts/blender.py b/projects/FBX/scripts/blender.py index 967d72bd27..929df29853 100644 --- a/projects/FBX/scripts/blender.py +++ b/projects/FBX/scripts/blender.py @@ -26,231 +26,152 @@ blender_file = args.input output_directory = args.output -json_output = os.path.join(output_directory, "info.json") +info_output = os.path.join(output_directory, "info.json") +map_ouput = os.path.join(output_directory, "map.json") abc_output = os.path.join(output_directory, "output.abc") -""" -{ - "": { # name corresponding .abc file - # value - float,vec3 - "base_color": { name: "Base Color", value: "" } - "subsurface": { name: "Subsurface", value: "" } - "subsurface_radius": { name: "Subsurface Radius", value: "" } - "subsurface_color": { name: "Subsurface Color", value: "" } - "subsurface_ior": { name: "Subsurface IOR", value: "" } - "subsurface_anisotropy":{ name: "Subsurface Anisotropy", value: "" } - "metallic": { name: "Metallic", value: "" } - "specular": { name: "Specular", value: "" } - "specular_tint": { name: "Specular Tint", value: "" } - "roughness": { name: "Roughness", value: "" } - "anisotropic": { name: "Anisotropic", value: "" } - "anisotropic_rotation": { name: "Anisotropic Rotation", value: "" } - "sheen": { name: "Sheen", value: "" } - "sheen_roughness": { name: "Sheen Roughness", value: "" } - "sheen_tint": { name: "Sheen Tint", value: "" } - "clearcoat": { name: "Clearcoat", value: "" } - "clearcoat_roughness": { name: "Clearcoat Roughness", value: "" } - "ior": { name: "IOR", value: "" } - "transmission": { name: "Transmission", value: "" } - "emission": { name: "Emission", value: "" } - "emission_strength": { name: "Emission Strength", value: "" } - "alpha": { name: "Alpha", value: "" } - "normal": { name: "Normal", value: "" } - "clearcoat_normal": { name: "Clearcoat Normal", value: "" } - "tangent": { name: "Tangent", value: "" } - "_textures": { - "": { - "": { - "channel_name": # material parameter name - "separate_name": "" # Red Green Blue - "mapping_scale": # vec3 - } - } - } - } -} -""" material_info = {} +def BlenderToZenoMap(path): + mat_map = {} + mat_map["Base Color"] = "basecolor" + mat_map["Subsurface"] = "subsurface" + mat_map["Metallic"] = "metallic" + mat_map["Specular"] = "specular" + mat_map["Roughness"] = "roughness" + mat_map["Anisotropic"] = "anisotropic" + mat_map["Anisotropic Rotation"] = "anisoRotation" + mat_map["IOR"] = "ior" + mat_map["Sheen"] = "sheen" + mat_map["Sheen Tint"] = "sheenTint" + mat_map["Opacity"] = "opacity" + mat_map["Normal"] = "normal" + mat_map["Transmission"] = "specTrans" + mat_map["Emission"] = "emission" + mat_map["Emission Strength"] = "emissionIntensity" + mat_map["Clearcoat"] = "clearcoat" + mat_map["Clearcoat Roughness"] = "clearcoatRoughness" + js = json.dumps(mat_map,indent=4) + with open(path,"w") as ouput: + ouput.write(js) + + + +def FindNodeToSocket(socket,node_bl_label): + if (not socket.is_linked): + return None,None + else: + links = socket.links + for link in links: + if(link.from_node.bl_label == node_bl_label): + return link.from_node, link + + return None,None + +def EvalTextureForSocket(socket): + path = "" + postprocess = "srgb" + channel = "vec3" + scale = [1.0,1.0] + PP_node=None + image_node = None + link = None + + P_node,link = FindNodeToSocket(socket,"Separate Color") + if (not(P_node is None)): + channel = link.from_socket.name + if (channel == "Red"): + channel = "R" + elif (channel == "Green"): + channel = "G" + elif (channel == "Blue"): + channel = "B" + PP_node = P_node + + P_node,link = FindNodeToSocket(socket,"Normal Map") + if (not(P_node is None)): + postprocess = "normal_map" + PP_node = P_node + + if (PP_node is None): + image_node,link = FindNodeToSocket(socket,"Image Texture") + else: + image_node,link = FindNodeToSocket(PP_node.inputs["Color"],"Image Texture") + + if (not (image_node is None)): + if (not (image_node.image is None)): + path =bpy.path.abspath(image_node.image.filepath).replace("\\", "/") + + + return path,postprocess,channel,scale + + + +def EvalBSDFNode(bsdf_node): + mat = {} + for socket in bsdf_node.inputs: + socket_name = socket.name + if (socket_name == "Alpha"): + socket_name = "Opacity" + mat[socket_name]={} + if isinstance(socket.default_value,(float,int)): #it is not a bpy_prop_array + mat[socket_name]["value"] = socket.default_value + if(socket_name == "Opacity"): + mat[socket_name]["value"] = 1 - socket.default_value + else: + mat[socket_name]["value"] = socket.default_value[:] + + if(socket.is_linked): + path,postpro,chennal,scale = EvalTextureForSocket(socket) + mat[socket_name]["path"] = path + mat[socket_name]["postpro"] = postpro + mat[socket_name]["channel"] = chennal + mat[socket_name]["scale"] = scale + + return mat + + +def Mat2Json(material): + mat_name = material.name + print("Material Name is {}".format(mat_name)) + if (mat.use_nodes): + mat_ouput_node = None + mat_info = None + + for node in mat.node_tree.nodes: + if (node.bl_label=="Material Output"): + mat_ouput_node = node + + if (mat_ouput_node is None): + print("Can not found \"Materail Ouput\" node in {}".format(mat_name)) + return None,None + + bsdf_node,link = FindNodeToSocket(mat_ouput_node.inputs["Surface"],"Principled BSDF") + + if (bsdf_node is None): + print("Can not found \"Principled BSDF\" node in {}".format(mat_name) ) + else: + mat_info = EvalBSDFNode(bsdf_node) + if (not (mat_info is None)): + return mat_name,mat_info + + + return None,None -# Open the .blend file bpy.ops.wm.open_mainfile(filepath=blender_file) - - bpy.ops.wm.alembic_export(filepath=abc_output,face_sets=True,use_instancing=False) +BlenderToZenoMap(map_ouput) -# Iterate through all objects in the scene and find meshes -for obj in bpy.context.scene.objects: - - if obj.type == 'MESH': - material_params = {} - material_textures = {} - - # In blender, every object has a uniqe id - obj_name = obj.name_full - print(" Mesh Full:", obj_name) - - - matName = "Default" - if obj.material_slots: - for material_slot in obj.material_slots: - matName = material_slot.material.name - - - # Check if the object has a material - if obj.material_slots: - - # Iterate through material slots - for material_slot in obj.material_slots: - material = material_slot.material - - # Material name - print(" Material Name:", material.name) - matkey = material.name - if (matkey in material_info): - continue - - # Iterate through material nodes - if material.use_nodes: - for node in material.node_tree.nodes: - # Connections: Ref - https://cdn.staticaly.com/gh/aaronmack/image-hosting@master/e/image.66eprfiya4w0.webp - # - # Texture Coordinate (UV) -> (Vector) Mapping (Vector) -> (Vector) Image Texture (Color) -> (Color) Separate (Red, Green, Blue) -> (Color, Float) Principled BSDF - # - # [Start From Here] -> (Color) Principled BSDF - # - # -> (Color) Normal Map (Normal) -> (Color) Principled BSDF - # - # Check if the node is an image texture node - if node.type == 'TEX_IMAGE': - texture = node.image - texture_record = [] - - # Image texture filepath - img_full_path = bpy.path.abspath(texture.filepath).replace("\\", "/") - print(" Texture Filepath:", texture.filepath, "Full:", img_full_path) - - for node_out in node.outputs: - if node_out.name == "Color": - for link in node_out.links: - if link.to_node.type == "BSDF_PRINCIPLED": # .name = "Principled BSDF": - channel_name = link.to_socket.name - print(" --> channel:", channel_name) - texture_detail = {} - texture_detail[img_full_path] = { "channel_name": channel_name } - texture_record.append(channel_name) - material_textures[channel_name] = texture_detail - - if link.to_node.type == "SEPARATE_COLOR": # .name = "Separate Color": - for sep_color_out in link.to_node.outputs: - for sep_link in sep_color_out.links: - if sep_link.to_node.type == "BSDF_PRINCIPLED": - channel_name = sep_link.to_socket.name - print(" --> sep channel:", channel_name, sep_color_out.name) - texture_detail = {} - texture_detail[img_full_path] = {"channel_name": channel_name, "separate_name": sep_color_out.name} - texture_record.append(channel_name) - material_textures[channel_name] = texture_detail - - if link.to_node.type == "NORMAL_MAP": # .name = "Normal Map": - for normal_map_out in link.to_node.outputs: - for normal_link in normal_map_out.links: - channel_name = normal_link.to_socket.name - print(" --> normal channel:", normal_link.to_socket.name) - texture_detail = {} - texture_detail[img_full_path] = {"channel_name": channel_name} - texture_record.append(channel_name) - material_textures[channel_name] = texture_detail - - for node_in in node.inputs: - if node_in.name == "Vector": - for link in node_in.links: - if link.from_node.type == "MAPPING": # .name = "Mapping" - mapping_scale = link.from_node.inputs['Scale'].default_value - print(" --> mapping scale:", mapping_scale) - for record in texture_record: - material_textures[record][img_full_path]["mapping_scale"] = mapping_scale[:] - - # fill default value - for key in material_textures: - for _key in material_textures[key]: - value = material_textures[key][_key] - if not "separate_name" in value: - value["separate_name"] = "" - if not "mapping_scale" in value: - value["mapping_scale"] = [1.0, 1.0, 1.0] - - - material_params["_textures"] = material_textures - - if node.type == 'BSDF_PRINCIPLED': - base_color = node.inputs['Base Color'].default_value - subsurface = node.inputs['Subsurface'].default_value - subsurface_radius = node.inputs['Subsurface Radius'].default_value - subsurface_color = node.inputs['Subsurface Color'].default_value - subsurface_ior = node.inputs['Subsurface IOR'].default_value - subsurface_anisotropy = node.inputs['Subsurface Anisotropy'].default_value - metallic = node.inputs['Metallic'].default_value - specular = node.inputs['Specular'].default_value - specular_tint = node.inputs['Specular Tint'].default_value - roughness = node.inputs['Roughness'].default_value - anisotropic = node.inputs['Anisotropic'].default_value - anisotropic_rotation = node.inputs['Anisotropic Rotation'].default_value - sheen = node.inputs['Sheen'].default_value - #sheen_roughness = node.inputs['Sheen Roughness'].default_value # KeyError: 'bpy_prop_collection[key]: key "Sheen Roughness" not found' for blender 3.6 - sheen_roughness = 0.5 - sheen_tint = node.inputs['Sheen Tint'].default_value - clearcoat = node.inputs['Clearcoat'].default_value - clearcoat_roughness = node.inputs['Clearcoat Roughness'].default_value - ior = node.inputs['IOR'].default_value - transmission = node.inputs['Transmission'].default_value - emission = node.inputs['Emission'].default_value - emission_strength = node.inputs['Emission Strength'].default_value - alpha = node.inputs['Alpha'].default_value - normal = node.inputs['Normal'].default_value - clearcoat_normal = node.inputs['Clearcoat Normal'].default_value - tangent = node.inputs['Tangent'].default_value - #weight = node.inputs['Weight'].default_value - print(" Base Color:", base_color[:]) - - material_params["base_color"] = {"value": base_color[:], "name": "Base Color"} - material_params["subsurface"] = {"value": subsurface, "name": "Subsurface"} - material_params["subsurface_radius"] = {"value": subsurface_radius[:], "name": "Subsurface Radius"} - material_params["subsurface_color"] = {"value": subsurface_color[:], "name": "Subsurface Color"} - material_params["subsurface_ior"] = {"value": subsurface_ior, "name": "Subsurface IOR"} - material_params["subsurface_anisotropy"] = {"value": subsurface_anisotropy,"name": "Subsurface Anisotropy"} - material_params["metallic"] = {"value": metallic, "name": "Metallic"} - material_params["specular"] = {"value": specular, "name": "Specular"} - material_params["specular_tint"] = {"value": specular_tint, "name": "Specular Tint"} - material_params["roughness"] = {"value": roughness, "name": "Roughness"} - material_params["anisotropic"] = {"value": anisotropic, "name": "Anisotropic"} - material_params["anisotropic_rotation"] = {"value": anisotropic_rotation, "name": "Anisotropic Rotation"} - material_params["sheen"] = {"value": sheen, "name": "Sheen"} - material_params["sheen_tint"] = {"value": sheen_tint, "name": "Sheen Roughness"} - material_params["sheen_roughness"] = {"value": sheen_roughness, "name": "Sheen Tint"} - material_params["clearcoat"] = {"value": clearcoat, "name": "Clearcoat"} - material_params["clearcoat_roughness"] = {"value": clearcoat_roughness, "name": "Clearcoat Roughness"} - material_params["ior"] = {"value": ior, "name": "IOR"} - material_params["transmission"] = {"value": transmission, "name": "Transmission"} - material_params["emission"] = {"value": emission[:], "name": "Emission"} - material_params["emission_strength"] = {"value": emission_strength, "name": "Emission Strength"} - material_params["alpha"] = {"value": alpha, "name": "Alpha"} - material_params["normal"] = {"value": normal[:], "name": "Normal"} - material_params["clearcoat_normal"] = {"value": clearcoat_normal[:], "name": "Clearcoat Normal"} - material_params["tangent"] = {"value": tangent[:], "name": "Tangent"} - - material_info[matkey] = material_params - print("-----") - gc.collect() +for mat in bpy.data.materials: + mat_name,mat_info = Mat2Json(mat) + if(not (mat_name is None)): + material_info[mat_name] = mat_info -print(json_output) +js = json.dumps(material_info,indent=4) +with open(info_output,"w") as output: + output.write(js) -# Serializing json -json_object = json.dumps(material_info, indent=4) -# Writing to sample.json -with open(json_output, "w") as outfile: - outfile.write(json_object) diff --git a/ui/zenoedit/nodesys/readfbxprim.cpp b/ui/zenoedit/nodesys/readfbxprim.cpp index ff21f16284..1323ea8abc 100644 --- a/ui/zenoedit/nodesys/readfbxprim.cpp +++ b/ui/zenoedit/nodesys/readfbxprim.cpp @@ -308,361 +308,109 @@ struct MatChannelInfo{ void EvalBlenderFile::onEvalClicked() { ZENO_HANDLE mGraph = Zeno_GetGraph("main"); - ZENO_HANDLE hGraph = Zeno_CreateGraph("BlenderParsed"); - ZENO_HANDLE evalBlenderNode = index().internalId(); - std::pair evalBlenderNodePos; - Zeno_GetPos(hGraph, evalBlenderNode, evalBlenderNodePos); - - // SubGraph Output - int out_view_count = 0; - ZENO_HANDLE suboutput_Node = Zeno_AddNode(hGraph, "SubOutput"); - Zeno_SetPos(hGraph, suboutput_Node, evalBlenderNodePos); - ZENO_HANDLE makelist_Node = Zeno_AddNode(hGraph, "MakeList"); - Zeno_SetPos(hGraph, makelist_Node, {evalBlenderNodePos.first - 500.0f, evalBlenderNodePos.second}); - Zeno_AddLink(hGraph, makelist_Node, "list", suboutput_Node, "port"); auto inputs = GetCurNodeInputs(); std::string output_path = inputs[2]; Path dir (output_path); - Path json_file ("info.json"); + Path info_file_path ("info.json"); + Path map_file_path ("map.json"); Path zsg_file ("parsed.zsg"); - Path out_json = dir / json_file; - Path out_zsg = dir / zsg_file; + info_file_path = dir / info_file_path; + map_file_path = dir / map_file_path; - if(! exists(out_json)){ - std::cout << "error: the info.json that script output doesn't exists\n"; - return; - } - std::ifstream f(out_json.string()); - std::string str; - if(f) { + bool use_map_file = false; + + Json mat_map; + std::ifstream map_file(map_file_path.string()); + std::string map_str; + if(map_file) { std::ostringstream ss; - ss << f.rdbuf(); // reading data - str = ss.str(); + ss << map_file.rdbuf(); // reading data + map_str = ss.str(); + use_map_file = true; + mat_map = Json::parse(map_str); + }else{ + zeno::log_warn("Can not open map.json under folder: {}\n",dir); } - Json parseData = Json::parse(str); - - int abc_count = 0; - // Eval Mesh - for (auto& [key, val] : parseData.items()) - { - Path file = Path(key+".abc"); - Path out_abc = dir / Path("abc") / file; - std::cout << "abc file: " << out_abc.string() << " exists: " << exists(out_abc) << "\n"; - if(exists(out_abc)) - { - std::pair NodePos1 = { evalBlenderNodePos.first + 600.0f, evalBlenderNodePos.second + 300.0f * abc_count}; - std::pair NodePos2 = { evalBlenderNodePos.first + 1100.0f, evalBlenderNodePos.second + 300.0f * abc_count}; - std::pair NodePos3 = { evalBlenderNodePos.first + 1600.0f, evalBlenderNodePos.second + 300.0f * abc_count}; - - // Create nodes - ZENO_HANDLE read_alembic_Node = Zeno_AddNode(hGraph, "ReadAlembic"); - Zeno_SetPos(hGraph, read_alembic_Node, NodePos1); - - ZENO_HANDLE alembic_prim_Node = Zeno_AddNode(hGraph, "AllAlembicPrim"); - Zeno_SetPos(hGraph, alembic_prim_Node, NodePos2); - - ZENO_HANDLE bind_material_Node = Zeno_AddNode(hGraph, "BindMaterial"); - Zeno_SetPos(hGraph, bind_material_Node, NodePos3); - - // Set node inputs - Zeno_SetInputDefl(hGraph, read_alembic_Node, "path", out_abc.string()); - Zeno_SetInputDefl(hGraph, alembic_prim_Node, "use_xform", true); - Zeno_SetInputDefl(hGraph, alembic_prim_Node, "triangulate", true); - Zeno_SetInputDefl(hGraph, bind_material_Node, "mtlid", key.substr(0,key.find('/'))); - - // Link nodes - Zeno_AddLink(hGraph, read_alembic_Node, "abctree", alembic_prim_Node, "abctree"); - Zeno_AddLink(hGraph, alembic_prim_Node, "prim", bind_material_Node, "object"); - - // Set node view - //Zeno_SetView(hGraph, bind_material_Node, true); - - Zeno_AddLink(hGraph, bind_material_Node, "object", makelist_Node, "obj"+std::to_string(out_view_count)); - out_view_count++; - - abc_count++; - }else{ - std::cout << "warn: " << out_abc.string() << " doesn't exists, skip\n"; - } + + std::ifstream info_file(info_file_path.string()); + std::string info_str; + if(info_file) { + std::ostringstream ss; + ss << info_file.rdbuf(); // reading data + info_str = ss.str(); + }else{ + zeno::log_error("Can not open info.json under folder: {}\n",dir); + return; } + Json mat_info = Json::parse(info_str); - int mat_count = 0; - std::map mat_existence; // Eval Material - for (auto& [key, val] : parseData.items()){ + for (auto& [key, val] : mat_info.items()){ - auto matName = key.substr(0,key.find('/')); - - if(mat_existence.find(matName)!=mat_existence.end()){ - continue; - } - mat_existence[matName] = 1; - - std::vector mat_texs{}; - std::map mat_infos{}; - - if(val.find("_textures") != val.end()) - { - for(auto& [_mat_channel, val_] : val["_textures"].items()) - { - MatChannelInfo channel_info{}; - - // only have one element, and we are bound to get into this cycle - for(auto& [_tex_path, val__] : val_.items()){ - auto channel_name = val__["channel_name"]; - auto mapping_scale = val__["mapping_scale"]; - auto separate_name = val__["separate_name"]; - auto scale_value = mapping_scale.get>(); - channel_info.uv_scale_x = scale_value[0]; - channel_info.uv_scale_y = scale_value[1]; - channel_info.uv_scale_z = scale_value[2]; - - auto separate_value = separate_name.get(); - channel_info.separate = separate_value; - channel_info.texture_path = _tex_path; - if (std::find(mat_texs.begin(), mat_texs.end(), _tex_path) == mat_texs.end()) { - channel_info.tex_id = mat_texs.size(); - // If the element is not found, insert it - mat_texs.emplace_back(_tex_path); - }else{ - int index = (int)std::distance(mat_texs.begin(), std::find(mat_texs.begin(), mat_texs.end(), _tex_path)); - channel_info.tex_id = index; - } - break; - } - mat_infos[_mat_channel] = channel_info; - } - } + auto matName = key; - std::pair NodePos2 = { evalBlenderNodePos.first + 3000.0f * (mat_count%50) + 800.0f, evalBlenderNodePos.second + 2000.0f * ( mat_count/50 )}; - ZENO_HANDLE shader_finalize_Node = Zeno_AddNode(hGraph, "ShaderFinalize"); - Zeno_SetPos(hGraph, shader_finalize_Node, NodePos2); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "mtlid", matName); - //Zeno_SetView(hGraph, shader_finalize_Node, true); - - Zeno_AddLink(hGraph, shader_finalize_Node, "mtl", makelist_Node, "obj"+std::to_string(out_view_count)); - out_view_count++; - - if(val.find("base_color") != val.end()){ - auto base_color = val.at("base_color").at("value").get>(); // vec4 - auto subsurface = val.at("subsurface").at("value").get(); - auto subsurface_radius = val.at("subsurface_radius").at("value").get>(); // vec3 - auto subsurface_color = val.at("subsurface_color").at("value").get>(); // vec4 - auto subsurface_ior = val.at("subsurface_ior").at("value").get(); - auto subsurface_anisotropy = val.at("subsurface_anisotropy").at("value").get(); - auto metallic = val.at("metallic").at("value").get(); - auto specular = val.at("specular").at("value").get(); - auto specular_tint = val.at("specular_tint").at("value").get(); - auto roughness = val.at("roughness").at("value").get(); - auto anisotropic = val.at("anisotropic").at("value").get(); - auto anisotropic_rotation = val.at("anisotropic_rotation").at("value").get(); - auto sheen = val.at("sheen").at("value").get(); - auto sheen_tint = val.at("sheen_tint").at("value").get(); - auto sheen_roughness = val.at("sheen_roughness").at("value").get(); - auto clearcoat = val.at("clearcoat").at("value").get(); - auto clearcoat_roughness = val.at("clearcoat_roughness").at("value").get(); - auto ior = val.at("ior").at("value").get(); - auto transmission = val.at("transmission").at("value").get(); - auto emission = val.at("emission").at("value").get>(); // vec4 - auto emission_strength = val.at("emission_strength").at("value").get(); - auto alpha = val.at("alpha").at("value").get(); - auto normal = val.at("normal").at("value").get>(); //vec3 - auto clearcoat_normal = val.at("clearcoat_normal").at("value").get>(); //vec3 - auto tangent = val.at("tangent").at("value").get>(); //vec3 - - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "basecolor", zeno::vec3f(base_color[0],base_color[1],base_color[2])); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "metallic", metallic); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "roughness", roughness); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "specular", specular); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "subsurface", subsurface); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "sssParam", zeno::vec3f(subsurface_radius[0],subsurface_radius[1],subsurface_radius[2])); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "sssColor", zeno::vec3f(subsurface_color[0],subsurface_color[1],subsurface_color[2])); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "specularTint", specular_tint); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "anisotropic", anisotropic); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "anisoRotation", anisotropic_rotation); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "sheen", sheen); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "sheenTint", sheen_tint); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "clearcoat", clearcoat); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "clearcoatRoughness", clearcoat_roughness); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "specTrans", transmission); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "ior", ior); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "emission", zeno::vec3f(emission[0],emission[1],emission[2])); - Zeno_SetInputDefl(hGraph, shader_finalize_Node, "emissionIntensity", emission_strength); - } + ZENO_HANDLE subG = Zeno_CreateGraph(matName,1); + ZENO_HANDLE shader = Zeno_AddNode(subG,"ShaderFinalize"); - if(mat_infos.size()) - { - std::pair NodePos0 = { evalBlenderNodePos.first + 3000.0f * (mat_count%50), evalBlenderNodePos.second + 2000.0f * ( mat_count/50 )}; - std::pair NodePos1 = { evalBlenderNodePos.first + 3000.0f * (mat_count%50), evalBlenderNodePos.second + 2000.0f * ( mat_count/50 ) + 1000.0f}; - - ZENO_HANDLE shader_attr_Node = Zeno_AddNode(hGraph, "ShaderInputAttr"); - Zeno_SetPos(hGraph, shader_attr_Node, NodePos0); - Zeno_SetInputDefl(hGraph, shader_attr_Node, "attr", std::string("uv")); - - int shader_texture_count = 0; - for(auto &[key, value] : mat_infos) { - std::pair NodePos_1 = { NodePos0.first + 600.0f, NodePos0.second + 500.0f * shader_texture_count}; - std::pair NodePos_2 = { NodePos_1.first + 600.0f, NodePos_1.second}; - std::pair NodePos_3 = { NodePos_2.first + 600.0f, NodePos_2.second}; - - ZENO_HANDLE uv_transform_Node = Zeno_AddNode(hGraph, "FBXUVTransform"); - Zeno_SetPos(hGraph, uv_transform_Node, NodePos_1); - ZENO_HANDLE shader_texture_Node = Zeno_AddNode(hGraph, "ShaderTexture2D"); - Zeno_SetPos(hGraph, shader_texture_Node, NodePos_2); - - - Zeno_AddLink(hGraph, shader_attr_Node, "out", uv_transform_Node, "uvattr"); - Zeno_AddLink(hGraph, uv_transform_Node, "uvw", shader_texture_Node, "coord"); - - Zeno_SetInputDefl(hGraph, shader_texture_Node, "texId", value.tex_id); - - Zeno_SetInputDefl(hGraph, uv_transform_Node, "uvtransform", zeno::vec4f(value.uv_scale_x, value.uv_scale_y, 0.0f, 0.0f)); -#define PARAM_TYPE_CHECK(to_channel_name) \ -if( \ - #to_channel_name == "metallic" || \ - #to_channel_name == "roughness" || \ - #to_channel_name == "specular" || \ - #to_channel_name == "subsurface" || \ - #to_channel_name == "specularTint" || \ - #to_channel_name == "anisotropic" || \ - #to_channel_name == "anisoRotation" || \ - #to_channel_name == "sheen" || \ - #to_channel_name == "sheenTint" || \ - #to_channel_name == "clearcoat" || \ - #to_channel_name == "clearcoatRoughness" || \ - #to_channel_name == "specTrans" || \ - #to_channel_name == "ior" || \ - #to_channel_name == "emissionIntensity") \ -{ \ - ZENO_HANDLE extrac_vec_Node = Zeno_AddNode(hGraph, "ShaderExtractVec"); \ - Zeno_SetPos(hGraph, extrac_vec_Node, NodePos_3); \ - Zeno_AddLink(hGraph, shader_texture_Node, "out", extrac_vec_Node, "vec"); \ - Zeno_AddLink(hGraph, extrac_vec_Node, "x", shader_finalize_Node, #to_channel_name); \ -}else{ \ - Zeno_AddLink(hGraph, shader_texture_Node, "out", shader_finalize_Node, #to_channel_name); \ -} + ZENO_HANDLE sub_ouput= Zeno_AddNode(subG, "SubOutput"); + Zeno_AddLink(subG, shader, "mtl", sub_ouput, "port"); + Zeno_SetInputDefl(subG,shader,"mtlid",matName); + for (auto& [mat_item,mat_item_info]: val.items()){ -#define PARAM_SEPARATE(to_channel_name) \ -if(value.separate != ""){ \ - ZENO_HANDLE extrac_vec_Node = Zeno_AddNode(hGraph, "ShaderExtractVec"); \ - Zeno_SetPos(hGraph, extrac_vec_Node, NodePos_3); \ - Zeno_AddLink(hGraph, shader_texture_Node, "out", extrac_vec_Node, "vec"); \ - if(value.separate == "Red"){ \ - Zeno_AddLink(hGraph, extrac_vec_Node, "x", shader_finalize_Node, #to_channel_name); \ - }else if(value.separate == "Green"){ \ - Zeno_AddLink(hGraph, extrac_vec_Node, "y", shader_finalize_Node, #to_channel_name); \ - }else if(value.separate == "Blue"){ \ - Zeno_AddLink(hGraph, extrac_vec_Node, "z", shader_finalize_Node, #to_channel_name); \ - } \ -}else{ \ - PARAM_TYPE_CHECK(to_channel_name) \ -} \ - - if(key == "Base Color"){ - PARAM_SEPARATE(basecolor) - } - else if(key == "Subsurface"){ - PARAM_SEPARATE(subsurface) - } - else if(key == "Subsurface Radius"){ - PARAM_SEPARATE(sssParam) - } - else if(key == "Subsurface Color"){ - PARAM_SEPARATE(sssColor) - } - else if(key == "Metallic"){ - PARAM_SEPARATE(metallic) - } - else if(key == "Specular"){ - PARAM_SEPARATE(specular) - } - else if(key == "Specular Tint"){ - PARAM_SEPARATE(specularTint) - } - else if(key == "Roughness"){ - PARAM_SEPARATE(roughness) - } - else if(key == "Anisotropic"){ - PARAM_SEPARATE(anisotropic) - } - else if(key == "Anisotropic Rotation"){ - PARAM_SEPARATE(anisoRotation) - } - else if(key == "Sheen"){ - PARAM_SEPARATE(sheen) - } - else if(key == "Sheen Tint"){ - PARAM_SEPARATE(sheenTint) - } - else if(key == "Clearcoat"){ - PARAM_SEPARATE(clearcoat) - } - else if(key == "Clearcoat Roughness"){ - PARAM_SEPARATE(clearcoatRoughness) - } - else if(key == "IOR"){ - PARAM_SEPARATE(ior) - } - else if(key == "Transmission"){ - PARAM_SEPARATE(specTrans) + std::string item_name = mat_item; + if(use_map_file && mat_map.find(item_name)!=mat_map.end()){ + item_name = mat_map.at(item_name).get(); + } + + ZVARIANT ret; + std::string type = ""; + if(Zeno_GetInputDefl(subG,shader,item_name,ret,type)==ErrorCode::Err_SockNotExist){ //if no such a material item name, jump to next item + zeno::log_warn("No such a material item: {} in zeno, import from material: {}",item_name,matName); + continue; + } + zeno::log_info("item {} has type {}",item_name,type); + + if(mat_item_info.find("path")!=mat_item_info.end()){ //has texture input for this item + ZENO_HANDLE texture_node = Zeno_AddNode(subG,"SmartTexture2D"); + Zeno_SetInputDefl(subG,texture_node,"path",mat_item_info.at("path").get()); + Zeno_SetInputDefl(subG,texture_node,"type",mat_item_info.at("channel").get()); + if(type == "float"&& mat_item_info.at("channel").get() == "vec3"){ + Zeno_SetInputDefl(subG,texture_node,"type","float"); } - else if(key == "Emission"){ - PARAM_SEPARATE(emission) + Zeno_SetInputDefl(subG,texture_node,"post_process",mat_item_info.at("postpro").get()); + if(item_name == "normal"){ + Zeno_SetInputDefl(subG,texture_node,"value",zeno::vec4f(0.0f,0.0f,1.0f,0.0f)); } - else if(key == "Emission Strength"){ - PARAM_SEPARATE(emissionIntensity) + Zeno_AddLink(subG,texture_node,"out",shader,item_name); + }else{ + std::vector value; + if(mat_item_info.at("value").is_array()){ + value = mat_item_info.at("value").get>(); + }else{ + value.push_back(mat_item_info.at("value").get()); } - - if(key == "Normal") { - ZENO_HANDLE normal_texture_Node = Zeno_AddNode(hGraph, "NormalTexture"); - Zeno_SetPos(hGraph, normal_texture_Node, NodePos_2); - Zeno_DeleteNode(hGraph, shader_texture_Node); - - Zeno_SetInputDefl(hGraph, normal_texture_Node, "texId", value.tex_id); - Zeno_AddLink(hGraph, uv_transform_Node, "uvw", normal_texture_Node, "uv"); - Zeno_AddLink(hGraph, normal_texture_Node, "normal", shader_finalize_Node, "normal"); + int input_size = value.size(); + int need_size = 3; + if(type == "vec3f" || type == "colorvec3f"){ + if (input_size >= 3 ){ + Zeno_SetInputDefl(subG,shader,item_name,zeno::vec3f(value[0],value[1],value[2])); + }else{ + Zeno_SetInputDefl(subG,shader,item_name,zeno::vec3f(value[0],value[0],value[0])); + } + }else if(type == "float"){ + Zeno_SetInputDefl(subG,shader,item_name,value[0]); } - shader_texture_count++; - } - - ZENO_HANDLE make_list_Node = Zeno_AddNode(hGraph, "MakeList"); - Zeno_SetPos(hGraph, make_list_Node, NodePos1); - int texture_2d_count = 0; - for(auto& tex_path :mat_texs){ - std::pair NodePos_1 = { NodePos1.first - 700.0f, NodePos1.second + 500.0f * texture_2d_count}; - - ZENO_HANDLE texture_2d_Node = Zeno_AddNode(hGraph, "MakeTexture2D"); - Zeno_SetPos(hGraph, texture_2d_Node, NodePos_1); - - Zeno_AddLink(hGraph, texture_2d_Node, "tex", make_list_Node, "obj"+std::to_string(texture_2d_count)); - - Zeno_SetInputDefl(hGraph, texture_2d_Node, "path", tex_path); - - texture_2d_count++; } - Zeno_AddLink(hGraph, make_list_Node, "list", shader_finalize_Node, "tex2dList"); } + + ZENO_HANDLE mat_subgraph = Zeno_AddNode(mGraph,matName); + Zeno_SetView(mGraph,mat_subgraph,true); - mat_count++; } - - // Create Parsed-SubNode in main - ZENO_HANDLE parsed_blender_Node = Zeno_AddNode(mGraph, "BlenderParsed"); - Zeno_SetPos(mGraph, parsed_blender_Node, {evalBlenderNodePos.first + 500.0f, evalBlenderNodePos.second}); - Zeno_SetView(mGraph, parsed_blender_Node, true); - - // Save zsg file - // IGraphsModel *pModel = GraphsManagment::instance().currentModel(); - // GraphsManagment& gman = GraphsManagment::instance(); - // APP_SETTINGS settings; - // TIMELINE_INFO info; - // settings.timeline = info; - // std::cout << "save zsg file: " << out_zsg.string() << "\n"; - // gman.saveFile(QString::fromStdString(out_zsg.string()), settings); } void EvalBlenderFile::onExecClicked() { From 46de54215a31f0143bfb5977eb36c4d23a7e4631 Mon Sep 17 00:00:00 2001 From: teachmain Date: Tue, 19 Dec 2023 19:45:12 +0800 Subject: [PATCH 4/7] remove test file --- projects/FBX/scripts/test.py | 159 ----------------------------------- 1 file changed, 159 deletions(-) delete mode 100644 projects/FBX/scripts/test.py diff --git a/projects/FBX/scripts/test.py b/projects/FBX/scripts/test.py deleted file mode 100644 index 0048db9df1..0000000000 --- a/projects/FBX/scripts/test.py +++ /dev/null @@ -1,159 +0,0 @@ -# Open a .blender file and export abc mesh and material infos. -# -# To use this script, you need install bpy module first or build blender python module from source. -# python -m pip install --user bpy -# -# https://pypi.org/project/bpy/#history -# https://builder.blender.org/download/bpy/ - -import gc -import os -import bpy -import json -import argparse -from pprint import pprint - -parser = argparse.ArgumentParser() -parser.add_argument("-i", "--input", help="blender file full path") -parser.add_argument("-o", "--output", help="output alembic full path") -args = parser.parse_args() - -print("---- Input Path", args.input) -print("---- Output Path", args.output) - -assert os.path.exists(args.input) - - -blender_file = args.input -output_directory = args.output -json_output = os.path.join(output_directory, "info.json") -abc_output = os.path.join(output_directory, "output.abc") - -material_info = {} - - - - -# Open the .blend file -def BlenderToZenoMap(): - mat_map = {} - mat_map["Base Color"] = "basecolor" - mat_map["Subsurface"] = "subsurface" - mat_map["Metallic"] = "metallic" - mat_map["Specular"] = "specular" - mat_map["Roughness"] = "roughness" - mat_map["Anisotropic"] = "anisotropic" - mat_map["Anisotropic Rotation"] = "anisoRotation" - mat_map["IOR"] = "ior" - mat_map["Sheen"] = "sheen" - mat_map["Sheen Tint"] = "sheenTint" - mat_map["Alpha"] = "opacity" - mat_map["Normal"] = "normal" - mat_map["Transmission"] = "specTrans" - mat_map["Emission"] = "emission" - mat_map["Emission Strength"] = "emissionIntensity" - mat_map["Clearcoat"] = "clearcoat" - mat_map["Clearcoat Roughness"] = "clearcoatRoughness" - return mat_map - - -def FindNodeToSocket(socket,node_bl_label): - if (not socket.is_linked): - return None,None - else: - links = socket.links - for link in links: - if(link.from_node.bl_label == node_bl_label): - return link.from_node, link - - return None,None - -def EvalTextureForSocket(socket): - path = "" - postprocess = "srgb" - channel = "vec3" - scale = [1.0,1.0] - PP_node=None - image_node = None - link = None - - P_node,link = FindNodeToSocket(socket,"Separate Color") - if (not(P_node is None)): - channel = link.from_socket.name - PP_node = P_node - - P_node,link = FindNodeToSocket(socket,"Normal Map") - if (not(P_node is None)): - postprocess = "normal_map" - PP_node = P_node - - if (PP_node is None): - image_node,link = FindNodeToSocket(socket,"Image Texture") - else: - image_node,link = FindNodeToSocket(PP_node.inputs["Color"],"Image Texture") - - if (not (image_node is None)): - if (not (image_node.image is None)): - path = image_node.image.filepath - - return path,postprocess,channel,scale - - - -def EvalBSDFNode(bsdf_node): - mat = {} - for socket in bsdf_node.inputs: - mat[socket.name]={} - if isinstance(socket.default_value,(float,int)): #it is not a bpy_prop_array - mat[socket.name]["value"] = socket.default_value - else: - mat[socket.name]["value"] = socket.default_value[:] - - if(socket.is_linked): - path,postpro,chennal,scale = EvalTextureForSocket(socket) - mat[socket.name]["path"] = path - mat[socket.name]["postpro"] = postpro - mat[socket.name]["chennal"] = chennal - mat[socket.name]["scale"] = scale - - return mat - - -def Mat2Json(material): - mat_name = material.name - print("Material Name is {}".format(mat_name)) - if (mat.use_nodes): - mat_ouput_node = None - mat_info = None - - for node in mat.node_tree.nodes: - if (node.bl_label=="Material Output"): - mat_ouput_node = node - - if (mat_ouput_node is None): - print("Can not found \"Materail Ouput\" node in {}".format(mat_name)) - return None,None - - bsdf_node,link = FindNodeToSocket(mat_ouput_node.inputs["Surface"],"Principled BSDF") - - if (bsdf_node is None): - print("Can not found \"Principled BSDF\" node in {}".format(mat_name) ) - else: - mat_info = EvalBSDFNode(bsdf_node) - - return mat_name,mat_info - - return None,None - -bpy.ops.wm.open_mainfile(filepath=blender_file) - -for mat in bpy.data.materials: - mat_name,mat_info = Mat2Json(mat) - if(not (mat_name is None)): - material_info[mat_name] = mat_info - -js = json.dumps(material_info,indent=4) -print(js) - - - From e6853fc8fc46591f4527e87a45470e8dc80979e1 Mon Sep 17 00:00:00 2001 From: teachmain Date: Tue, 26 Dec 2023 16:47:31 +0800 Subject: [PATCH 5/7] fix normal --- ui/zenoedit/nodesys/readfbxprim.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ui/zenoedit/nodesys/readfbxprim.cpp b/ui/zenoedit/nodesys/readfbxprim.cpp index 1323ea8abc..aa649d44a2 100644 --- a/ui/zenoedit/nodesys/readfbxprim.cpp +++ b/ui/zenoedit/nodesys/readfbxprim.cpp @@ -315,7 +315,6 @@ void EvalBlenderFile::onEvalClicked() { Path dir (output_path); Path info_file_path ("info.json"); Path map_file_path ("map.json"); - Path zsg_file ("parsed.zsg"); info_file_path = dir / info_file_path; map_file_path = dir / map_file_path; @@ -391,6 +390,12 @@ void EvalBlenderFile::onEvalClicked() { }else{ value.push_back(mat_item_info.at("value").get()); } + if(item_name == "normal" && std::inner_product(value.begin(),value.end(),value.begin(),0) == 0 ){ + value[0] = 0.0f; + value[1] = 0.0f; + value[2] = 1.0f; + } + int input_size = value.size(); int need_size = 3; if(type == "vec3f" || type == "colorvec3f"){ From 1431dee759f3b5a21f1ea3a1ba053e7a8dda29a4 Mon Sep 17 00:00:00 2001 From: teachmain Date: Thu, 28 Dec 2023 17:40:47 +0800 Subject: [PATCH 6/7] update --- ui/zenoedit/nodesys/readfbxprim.cpp | 108 ++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 4 deletions(-) diff --git a/ui/zenoedit/nodesys/readfbxprim.cpp b/ui/zenoedit/nodesys/readfbxprim.cpp index aa649d44a2..cb68847626 100644 --- a/ui/zenoedit/nodesys/readfbxprim.cpp +++ b/ui/zenoedit/nodesys/readfbxprim.cpp @@ -315,8 +315,11 @@ void EvalBlenderFile::onEvalClicked() { Path dir (output_path); Path info_file_path ("info.json"); Path map_file_path ("map.json"); + Path abc_file_path ("output.abc"); info_file_path = dir / info_file_path; map_file_path = dir / map_file_path; + abc_file_path = dir / abc_file_path; + bool use_map_file = false; @@ -344,17 +347,113 @@ void EvalBlenderFile::onEvalClicked() { return; } Json mat_info = Json::parse(info_str); + // Eval Geo + std::pair pos; + ZENO_HANDLE GeoSubG = Zeno_GetGraph("AlembicImport"); + if(GeoSubG==0){ + GeoSubG = Zeno_CreateGraph("AlembicImport"); + ZENO_HANDLE BindMat = Zeno_GetGraph("FacesetBindMat"); + if(BindMat==0){ + BindMat = Zeno_CreateGraph("FacesetBindMat"); + + ZENO_HANDLE input = Zeno_AddNode(BindMat,"SubInput"); + Zeno_SetParam(BindMat,input,"name","abcPrim"); + + ZENO_HANDLE AlembicSplitByName = Zeno_AddNode(BindMat,"AlembicSplitByName"); + + ZENO_HANDLE BeginForEach = Zeno_AddNode(BindMat,"BeginForEach"); + + ZENO_HANDLE DictGetItem = Zeno_AddNode(BindMat,"DictGetItem"); + + ZENO_HANDLE PrimitiveTriangulate = Zeno_AddNode(BindMat,"PrimitiveTriangulate"); + Zeno_SetParam(BindMat,PrimitiveTriangulate,"from_poly",true); + Zeno_SetParam(BindMat,PrimitiveTriangulate,"with_uv",true); + + ZENO_HANDLE PrimitiveCalcNormal = Zeno_AddNode(BindMat,"PrimitiveCalcNormal"); + + ZENO_HANDLE BindMaterial = Zeno_AddNode(BindMat,"BindMaterial"); + + ZENO_HANDLE EndForEach = Zeno_AddNode(BindMat,"EndForEach"); + + ZENO_HANDLE ouput = Zeno_AddNode(BindMat,"SubOutput"); + Zeno_SetParam(BindMat,ouput,"name","list"); + + Zeno_AddLink(BindMat,input,"port",AlembicSplitByName,"prim"); + Zeno_AddLink(BindMat,AlembicSplitByName,"dict",DictGetItem,"dict"); + Zeno_AddLink(BindMat,AlembicSplitByName,"namelist",BeginForEach,"list"); + Zeno_AddLink(BindMat,BeginForEach,"object",DictGetItem,"key"); + Zeno_AddLink(BindMat,BeginForEach,"object",BindMaterial,"mtlid"); + Zeno_AddLink(BindMat,BeginForEach,"FOR",EndForEach,"FOR"); + Zeno_AddLink(BindMat,DictGetItem,"object",PrimitiveTriangulate,"prim"); + Zeno_AddLink(BindMat,PrimitiveTriangulate,"prim",PrimitiveCalcNormal,"prim"); + Zeno_AddLink(BindMat,PrimitiveCalcNormal,"prim",BindMaterial,"object"); + Zeno_AddLink(BindMat,BindMaterial,"object",EndForEach,"object"); + Zeno_AddLink(BindMat,EndForEach,"list",ouput,"port"); + + }else{ + zeno::log_error("FacesetBindMat subGraph already exist! Remove it then try again!"); + return; + } + ZENO_HANDLE input = Zeno_AddNode(GeoSubG,"SubInput"); + Zeno_SetParam(GeoSubG,input,"name","abc_file_path"); + Zeno_SetParam(GeoSubG,input,"type","string"); + ZENO_HANDLE ReadAlembic = Zeno_AddNode(GeoSubG,"ReadAlembic"); + Zeno_SetInputDefl(GeoSubG,ReadAlembic,"path",abc_file_path.string()); + Zeno_SetInputDefl(GeoSubG,ReadAlembic,"read_face_set",true); + + ZENO_HANDLE AlembicPrimList = Zeno_AddNode(GeoSubG,"AlembicPrimList"); + Zeno_SetInputDefl(GeoSubG,AlembicPrimList,"use_xform",true); + Zeno_SetOnce(GeoSubG,AlembicPrimList,true); + + ZENO_HANDLE CountAlembicPrims = Zeno_AddNode(GeoSubG,"CountAlembicPrims"); + ZENO_HANDLE BeginFor = Zeno_AddNode(GeoSubG,"BeginFor"); + ZENO_HANDLE ListGetItem = Zeno_AddNode(GeoSubG,"ListGetItem"); + ZENO_HANDLE FacesetBindMat = Zeno_AddNode(GeoSubG,"FacesetBindMat"); + ZENO_HANDLE EndForEach = Zeno_AddNode(GeoSubG,"EndForEach"); + ZENO_HANDLE output = Zeno_AddNode(GeoSubG,"SubOutput"); + + Zeno_AddLink(GeoSubG,input,"port",ReadAlembic,"path"); + Zeno_AddLink(GeoSubG,ReadAlembic,"abctree",AlembicPrimList,"abctree"); + Zeno_AddLink(GeoSubG,ReadAlembic,"abctree",CountAlembicPrims,"abctree"); + Zeno_AddLink(GeoSubG,AlembicPrimList,"prims",ListGetItem,"list"); + Zeno_AddLink(GeoSubG,ListGetItem,"object",FacesetBindMat,"abcPrim"); + Zeno_AddLink(GeoSubG,FacesetBindMat,"list",EndForEach,"list"); + Zeno_AddLink(GeoSubG,CountAlembicPrims,"count",BeginFor,"count"); + Zeno_AddLink(GeoSubG,BeginFor,"index",ListGetItem,"index"); + Zeno_AddLink(GeoSubG,BeginFor,"FOR",EndForEach,"FOR"); + Zeno_AddLink(GeoSubG,EndForEach,"list",output,"port"); + + + }else{ + zeno::log_error("AlembicImport subGraph already exist! Remove it then try again!"); + return; + } + + ZENO_HANDLE import_node = Zeno_AddNode(mGraph,"AlembicImport"); + Zeno_SetInputDefl(mGraph,import_node,"abc_file_path",abc_file_path.string()); + + Zeno_SetView(mGraph,import_node,true); + Zeno_GetPos(mGraph,import_node,pos); + pos.second += 500; + + + // Eval Material for (auto& [key, val] : mat_info.items()){ auto matName = key; - ZENO_HANDLE subG = Zeno_CreateGraph(matName,1); + ZENO_HANDLE subG = Zeno_GetGraph(matName); + if(subG != 0){ + zeno::log_warn("Material SubGraph {} already exist! Please remove it then run again"); + continue; + } + subG = Zeno_CreateGraph(matName,1); ZENO_HANDLE shader = Zeno_AddNode(subG,"ShaderFinalize"); - ZENO_HANDLE sub_ouput= Zeno_AddNode(subG, "SubOutput"); - Zeno_AddLink(subG, shader, "mtl", sub_ouput, "port"); + ZENO_HANDLE sub_output= Zeno_AddNode(subG, "SubOutput"); + Zeno_AddLink(subG, shader, "mtl", sub_output, "port"); Zeno_SetInputDefl(subG,shader,"mtlid",matName); for (auto& [mat_item,mat_item_info]: val.items()){ @@ -369,7 +468,6 @@ void EvalBlenderFile::onEvalClicked() { zeno::log_warn("No such a material item: {} in zeno, import from material: {}",item_name,matName); continue; } - zeno::log_info("item {} has type {}",item_name,type); if(mat_item_info.find("path")!=mat_item_info.end()){ //has texture input for this item ZENO_HANDLE texture_node = Zeno_AddNode(subG,"SmartTexture2D"); @@ -414,6 +512,8 @@ void EvalBlenderFile::onEvalClicked() { ZENO_HANDLE mat_subgraph = Zeno_AddNode(mGraph,matName); Zeno_SetView(mGraph,mat_subgraph,true); + Zeno_SetPos(mGraph,mat_subgraph,pos); + pos.second += 200; } } From d381482fea76ebdb25ef803cb9f9c4298ec11bfa Mon Sep 17 00:00:00 2001 From: teachmain Date: Thu, 28 Dec 2023 20:26:55 +0800 Subject: [PATCH 7/7] update --- ui/zenoedit/nodesys/readfbxprim.cpp | 27 ++++++++++++++++++++------- zenovis/xinxinoptix/DeflMatShader.cu | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ui/zenoedit/nodesys/readfbxprim.cpp b/ui/zenoedit/nodesys/readfbxprim.cpp index cb68847626..91b5c7ac22 100644 --- a/ui/zenoedit/nodesys/readfbxprim.cpp +++ b/ui/zenoedit/nodesys/readfbxprim.cpp @@ -390,9 +390,6 @@ void EvalBlenderFile::onEvalClicked() { Zeno_AddLink(BindMat,BindMaterial,"object",EndForEach,"object"); Zeno_AddLink(BindMat,EndForEach,"list",ouput,"port"); - }else{ - zeno::log_error("FacesetBindMat subGraph already exist! Remove it then try again!"); - return; } ZENO_HANDLE input = Zeno_AddNode(GeoSubG,"SubInput"); Zeno_SetParam(GeoSubG,input,"name","abc_file_path"); @@ -424,9 +421,6 @@ void EvalBlenderFile::onEvalClicked() { Zeno_AddLink(GeoSubG,EndForEach,"list",output,"port"); - }else{ - zeno::log_error("AlembicImport subGraph already exist! Remove it then try again!"); - return; } ZENO_HANDLE import_node = Zeno_AddNode(mGraph,"AlembicImport"); @@ -440,13 +434,33 @@ void EvalBlenderFile::onEvalClicked() { // Eval Material + + int mat_num = 0; + for(auto& [key,val]:mat_info.items()){ + mat_num++; + } + + int col = std::sqrt(mat_num); + int index = 0; + float h_start = pos.first; for (auto& [key, val] : mat_info.items()){ + if(index >= col){ + index = 0; + pos.first = h_start; + pos.second += 200; + }else{ + index ++; + pos.first += 500; + } auto matName = key; ZENO_HANDLE subG = Zeno_GetGraph(matName); if(subG != 0){ zeno::log_warn("Material SubGraph {} already exist! Please remove it then run again"); + ZENO_HANDLE mat_subgraph = Zeno_AddNode(mGraph,matName); + Zeno_SetView(mGraph,mat_subgraph,true); + Zeno_SetPos(mGraph,mat_subgraph,pos); continue; } subG = Zeno_CreateGraph(matName,1); @@ -513,7 +527,6 @@ void EvalBlenderFile::onEvalClicked() { ZENO_HANDLE mat_subgraph = Zeno_AddNode(mGraph,matName); Zeno_SetView(mGraph,mat_subgraph,true); Zeno_SetPos(mGraph,mat_subgraph,pos); - pos.second += 200; } } diff --git a/zenovis/xinxinoptix/DeflMatShader.cu b/zenovis/xinxinoptix/DeflMatShader.cu index b5b360e1d7..b543db39c0 100644 --- a/zenovis/xinxinoptix/DeflMatShader.cu +++ b/zenovis/xinxinoptix/DeflMatShader.cu @@ -884,7 +884,7 @@ extern "C" __global__ void __closesthit__radiance() prd->done = true; } - if(mats.thin<0.5f && mats.doubleSide<0.5f){ + if(mats.thin<0.5f || mats.doubleSide<0.5f){ prd->origin = rtgems::offset_ray(P, (next_ray_is_going_inside)? -prd->geometryNormal : prd->geometryNormal); } else {