Popular Post Atom Posted February 24, 2018 Popular Post Share Posted February 24, 2018 (edited) I put together a simple script to read the .mtl file, typically associated with a .obj, and create a Redshift material for each entry it finds in the .mtl with a map_Kd token. A Redshift material is created and a texture map is linked to the diffuse color with the filename for the map populated with what is found in the .mtl entry. This is useful when importing architectural .obj files that typically have a large number of materials associated with them. Expected .mtl format of supported tokens. #newmtl _4_2_ <- Material name. #Ns 96.078431 <- Specular intensity #Kd 0.640000 0.640000 0.640000 <- Diffuse color. #Ks 0.500000 0.500000 0.500000 <- Specular color. #Ni 1.000000 <- Index of refraction. #d 1.000000 <- Opacity. #map_Kd 21_budova/_4_2_.jpg <- Map name. #map_Kn 21_budova/_4_2_n.jpg <- Map name. The result of one-click texture mapping. Here is the Colosseum auto texture mapped. The path to the .mtl file and texture path is hard coded. Place the code in a shelf tool button and adjust the path to point to your .mtl file. mtl_to_redshift_061618.zip Edited June 17, 2018 by Atom 7 4 Quote Link to comment Share on other sites More sharing options...
Atom Posted February 24, 2018 Author Share Posted February 24, 2018 (edited) I added a new feature where a rsColorCorrector is placed after the rsTexture node. I reduce the saturation to zero and connect that to a rsBumpMap. The bump map is routed to the final output. The end result is that each material gains a slight bit a relief in the final render, based upon the greyscale value of the diffuse map. Here is the new Redshift network that is generated. Here is the same Colosseum, pictured above, with the new bump mapping applied. Edited February 24, 2018 by Atom 1 Quote Link to comment Share on other sites More sharing options...
Atom Posted February 24, 2018 Author Share Posted February 24, 2018 (edited) I made a change to the script when I imported a new model. This model was making use of cards with alpha channels which were not rendering correctly. It also had some materials without image maps at all, just colors. Here is the new Redshift network to support textures that have alphas (detected via filename extension) Here are the before and after images for alpha support. Before, card backgrounds are not "knocked out". rsSprite node removes or "knocks out" card background. Edited February 24, 2018 by Atom 1 Quote Link to comment Share on other sites More sharing options...
Atom Posted February 26, 2018 Author Share Posted February 26, 2018 (edited) If you find that the script crashes on certain .MTL files, it may be that the names used for materials violate the Houdini naming convention. Most notably, any material that starts with a number. i.e. 03, for example. These files may have been exported from 3DSMax. An example of .OBJ files with number based materials can be found at this link. I have written a short fixup script that will scan a folder of .OBJ files and examine the .OBJ and .MTL for Houdini naming violations. If it finds them, it will re-write the .OBJ and .MTL file with a revised name that meets the Houdini naming convention. # Rewrite the .OBJ and .MTL files to make material names Houdini compliant. # Most notably, 3DSMax exported .OBJ files with a material that starts with a number i.e. 03, for example. # Like the OBJ files found at this link. https://www.turbosquid.com/3d-models/free-3ds-mode-building-ready-city/690329 import os def returnFilesLike(passedFolderName, passedFileExtension = ".obj"): result = [] for file in os.listdir(passedFolderName): if file.endswith(passedFileExtension): result.append(os.path.join(passedFolderName,file)) return result def rewriteOBJFiles(passedPath): lst_files = returnFilesLike(passedPath, ".obj") if len(lst_files): for i,file in enumerate(lst_files): # Scan each .OBJ looking for illegal material names. the_path = os.path.dirname(file) obj_name = os.path.basename(file) just_name = os.path.splitext(obj_name)[0] mtl_name = "%s.mtl" % just_name mtl_filename = os.path.join(the_path,mtl_name) if os.path.isfile(mtl_filename): # A companion .MTL file exists, let's read the .OBJ file. with open(file, 'r') as f: lines = f.read().splitlines() # Remove slash n character at the end of each line. f.close() # Let's scan the .OBJ for illegal material names. needs_repair = False new_prefix = "%s_" % just_name new_lines = [] for line in lines: ary = line.split(' ') if ary[0] == 'usemtl': if ary[1][0].isdigit(): # This material name violates Houdini naming condition. First letter can not be a number. # Add a prefix now. ary[1] = "%s%s" % (new_prefix, ary[1]) new_lines.append("%s %s" % (ary[0],ary[1])) needs_repair = True else: new_lines.append(line) if needs_repair: # This .OBJ needs re-written to correct bad material names. f = open(file, 'w') for line in new_lines: f.write("%s\n" % line) f.close() # Time to read .MTL file. with open(mtl_filename, 'r') as f: lines = f.read().splitlines() # Remove slash n character at the end of each line. f.close() # Let's scan the .MTL for illegal material names. new_lines = [] for line in lines: ary = line.split(' ') if ary[0] == 'newmtl': if ary[1][0].isdigit(): # This material name violates Houdini naming condition. First letter can not be a number. # Add a prefix now. ary[1] = "%s%s" % (new_prefix, ary[1]) new_lines.append("%s %s" % (ary[0],ary[1])) else: new_lines.append(line) # This .MTL needs re-written to correct bad material names. f = open(mtl_filename, 'w') for line in new_lines: f.write("%s\n" % line) f.close() else: print "No [.OBJ] files found at folder [%s]." % (passedFolderName) # NOTE: Remember to make a backup of your original .OBJ/.MTL files before you run this script. rewriteOBJFiles ("C:\Users\Admin\Documents\Models\Blendswap\cc0_14_building_set\obj") Edited February 26, 2018 by Atom 1 1 Quote Link to comment Share on other sites More sharing options...
art3mis Posted May 13, 2018 Share Posted May 13, 2018 This is brilliant. Thanks for sharing! Quote Link to comment Share on other sites More sharing options...
Atom Posted June 16, 2018 Author Share Posted June 16, 2018 (edited) I have added support for an additional .MTL token, the map_Kn token. I came across a game based .OBJ/.MTL file that referenced companion normal maps to the diffuse maps. In this particular mesh, the normal map definition always followed the diffuse map definition. The extension to the material takes the form of a new rsTexture node being plugged into the rsBumpMap node. This disconnects the previous attempt to "auto-bump" a map by using a color correct node to create a greyscale image from the diffuse. The color correct node is still left in place, but the new rsTexture simply takes it's place for supplying the input of the rsBump map node. If no map_Kn token is found, the rsColorCorrect will remain in place. NOTE: For this to work you must have ObjParms applied to the object using this material and the checkbox for Compute Vertex Tangents From the object UV Map must be checked. The script does create the materials, but it does not auto-check the box on your object. The bump results can be a bit nicer using a normal map instead of a faked out greyscale of a diffuse. Edited June 16, 2018 by Atom Quote Link to comment Share on other sites More sharing options...
3di Posted August 7, 2018 Share Posted August 7, 2018 (edited) Briliant! I'd love to see a 3ds max script that exports redshift materials as a bunch of parameters to an xml file etc, and then suffixes each object name with a material identifier. Then this script could import the obj file and create like for like materials and assign them to the imported geometry based on the suffix on the objects name. Edited August 7, 2018 by 3di Quote Link to comment Share on other sites More sharing options...
emils.vfx Posted October 11, 2018 Share Posted October 11, 2018 (edited) Excellent tool. Edited October 11, 2018 by emils.vfx Quote Link to comment Share on other sites More sharing options...
Neon Junkyard Posted October 18, 2018 Share Posted October 18, 2018 this is brilliant and works great, thanks for making this I was using this to bring in some archviz models that had like 30-40 materials each so I modified it slightly to automatically promote all the RS_material parameters so you can mass change them all at once without having to dive in and out of materials hundreds of times. Also a lot of the material settings in other DCC apps do not translate at all to redshift settings, so this helps solve that as well pretty much just 1 line of code per vop- rs_mat.insertParmGeneratorsForAllInputs(hou.vopParmGenType.Parameter,True) Quote Link to comment Share on other sites More sharing options...
psp631 Posted November 27, 2018 Share Posted November 27, 2018 On 2018/2/24 at 10:53 PM, Atom said: I put together a simple script to read the .mtl file, typically associated with a .obj, and create a Redshift material for each entry it finds in the .mtl with a map_Kd token. A Redshift material is created and a texture map is linked to the diffuse color with the filename for the map populated with what is found in the .mtl entry. This is useful when importing architectural .obj files that typically have a large number of materials associated with them. Expected .mtl format of supported tokens. #newmtl _4_2_ <- Material name. #Ns 96.078431 <- Specular intensity #Kd 0.640000 0.640000 0.640000 <- Diffuse color. #Ks 0.500000 0.500000 0.500000 <- Specular color. #Ni 1.000000 <- Index of refraction. #d 1.000000 <- Opacity. #map_Kd 21_budova/_4_2_.jpg <- Map name. #map_Kn 21_budova/_4_2_n.jpg <- Map name. The result of one-click texture mapping. Here is the Colosseum auto texture mapped. The path to the .mtl file and texture path is hard coded. Place the code in a shelf tool button and adjust the path to point to your .mtl file. mtl_to_redshift_061618.zip you are great ! any possible mtl to mantra tex map auto link ? Quote Link to comment Share on other sites More sharing options...
ncub Posted March 13, 2019 Share Posted March 13, 2019 Thanks for making this. When I run the script, it brings in all the materials into shop but the obj links to the materials in mat. I just updated the three instances of "shop" in the code and changed to "mat" and it worked. Quote Link to comment Share on other sites More sharing options...
lexxkb Posted March 25, 2019 Share Posted March 25, 2019 Hello guys! This is a very useful thing for me. But unfortunately I'm still a beginner in Houdini. I do not understand how to use phyton scripts. Can I get a quick guide on what I'm doing wrong. Quote Link to comment Share on other sites More sharing options...
flcc Posted March 28, 2019 Share Posted March 28, 2019 (edited) Place the code in a shelf tool button or you can just open the python source editor (Windows menu) and paste the script into. Find the line #select MTL file to process Adjust one of the path to point to your .mtl file. Click apply (or click your new shelf button). It's done You find the materials in shop. Super Cool ! However, it seems that it is better to leave the textures at the same level as the obj file. When the textures are in a subfolder, the path in the texture is not good. At least for me. Spaces appear or they should not. Or maybe avoid spaces in the path. Edited March 28, 2019 by flcc Quote Link to comment Share on other sites More sharing options...
xndr Posted May 4, 2020 Share Posted May 4, 2020 This isnt working for me, When i press my new shelf tool nothing happens? Quote Link to comment Share on other sites More sharing options...
FinalFeliz Posted August 15, 2020 Share Posted August 15, 2020 On 3/12/2019 at 11:31 PM, ncub said: Thanks for making this. When I run the script, it brings in all the materials into shop but the obj links to the materials in mat. I just updated the three instances of "shop" in the code and changed to "mat" and it worked. I'm having the same issue, but there's actually 5 instances of "shop" in the script. Changed that to "mat" but only the first two materials get created and then it quits with error: "problem detecting redshift_material1 automatic closure." how did you get it to work? cheers, Quote Link to comment Share on other sites More sharing options...
ThomasEnglish Posted October 7, 2020 Share Posted October 7, 2020 (edited) Sorry are there any tips on how to do the path change? I'm guessing I've got it completely wrong here? Sorry I am a bit of a noobie Edited October 7, 2020 by ThomasEnglish Quote Link to comment Share on other sites More sharing options...
Atom Posted October 8, 2020 Author Share Posted October 8, 2020 Make sure you set n=0, if you modify the paths under that IF statement. You need to modify both variables, they are matched. One path points to the folder where the textures reside and the other points directly to the . MTL you want to process. Quote Link to comment Share on other sites More sharing options...
ThomasEnglish Posted October 12, 2020 Share Posted October 12, 2020 Thanks Atom, your insanely helpful on this forum! Your answer implies there is a better place to modify the path rather than that IF statement? This part of the script is the only example of #select MTL Is there a way of finding the MTL path in a material network? I only find the Houdini path for a material network i.e. /obj/KB3D_NeoCities/ So I need to set n = 0 because I only want it to process a single material networks Where do I find the file name to point to the material network I want to process everything inside? Again I really apologise for being really incomprehensed by this Quote Link to comment Share on other sites More sharing options...
Mark42 Posted November 26, 2020 Share Posted November 26, 2020 Hi, An error occurred after I changed the path according to the script,i can make sure the file path is correct. QAQ Quote Link to comment Share on other sites More sharing options...
Qwa7 Posted November 29, 2020 Share Posted November 29, 2020 Recently there was 1 more handy tool for material extraction, please note https://gumroad.com/l/matex Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.