2
$\begingroup$

I would like to know how to change the default Blend Mode and Shadow Mode for any newly created material, because Opaque mode is useless to me. I wish Blender always gave my new materials Hashed mode because I use lots of transparent textures and want them to instantly look realistic.

I think the script responsible for material properties is:

C:\Program Files\Blender Foundation\Blender 2.90\2.90\scripts\startup\bl_ui\properties_material.py

But I have no coding skills, so I'm only capable of swapping some parameter in source code if someone tells me exactly where to do it - I hope it's that simple.

I also went to Online Python Reference for the Blend Mode setting:

https://docs.blender.org/api/2.90/bpy.types.Material.html#bpy.types.Material.blend_method

And I found there a phrase that I guess will do the trick (with the right context), but I don't know how to use it. Maybe I could use it in an empty file, execute the script and save the file as default startup file so that any new file would use the typed blend/ shadow mode as default. I don't know if it all works this way. If so, this would be quite satisfying solution.

<< enum in [‘OPAQUE’, ‘CLIP’, ‘HASHED’, ‘BLEND’], default ‘HASHED’ >>

I'm on Windows 10, Blender 2.90.

$\endgroup$
4
+50
$\begingroup$

You may not like the answer, but I'm afraid the way you possibly imagine it is not meaningful to implement. Basically there are two ways to expand Blender programmatically: Either with an add-on (via pyhton API) or by hacking the C++ files. The latter means that you have to compile your own version of Blender every time a new official update appears. Gone are the days when you simply pressed the download button and installed it. That also means that you have to mess around with things like compiler choice and dependency management (which can be very painfull in C environments). If you are not familiar with it, then I would rather advise against this.

The far more convenient way would be to write a small addon and use the Pyhton API provided for this use-cases. However, this has the disadvantage that you have to press your own button for your adapted material, similar to what you are used to from other addons. But, as long as the Pyhton API doesn't change much, you can still download and install new Blender versions as usual and nothing will break.

Take a look at the following script and run it in Blender to test whether you would like to go the path of an add-on. If you need further help with the adaptation, feel free to ask me, but in principle you only have to expand or add a few lines to adapt it to your needs.

You can easily test it by pressing "Shift - F11", creating a new file and inserting the script. Then press the play button. Then, as with many other add-ons, another menu should be available in which you can create your material with a mouse-click (don't forget to select a mesh). I have tested the script but it is far from "bullet-proof" so do not test it in your project that you have to hand in tomorrow ;). Hope that helps.


Example


Useful resources:

https://www.youtube.com/watch?v=eo7UjKFiagk

https://docs.blender.org/api/2.90/bpy.types.Material.html#bpy.types.Material.blend_method

https://docs.blender.org/api/2.90/info_quickstart.html


import bpy
 
 
class ADDONNAME_PT_main_panel(bpy.types.Panel):
    
    bl_label = "Add Hashed Material Panel"
    bl_idname = "MYADDON_PT_main_panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'My Default Material'
 
    def draw(self, context):
        layout = self.layout
        layout.operator("addonname.addbasic_operator")
 
 
 
class ADDONNAME_OT_add_basic(bpy.types.Operator):
    bl_label = "Add My Material"
    bl_idname = "addonname.addbasic_operator"
    
    
    def execute(self, context):
        
        
        material_basic = bpy.data.materials.new(name= "Basic")
        material_basic.use_nodes = True
        
        material_basic.blend_method = "HASHED"
        material_basic.shadow_method = "HASHED"
        
        bpy.context.object.active_material = material_basic
    
        return {'FINISHED'}
    
    def invoke(self, context, event):
        return context.window_manager.invoke_props_dialog(self)
 
 
 
 
classes = [ADDONNAME_PT_main_panel, ADDONNAME_OT_add_basic]
 
 
 
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
 
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
 
 
 
if __name__ == "__main__":
    register()
$\endgroup$
3
  • $\begingroup$ Thank you for your explanation and the shared code :) It surely helps with creating new materials with desired properties. However, it doesn't resolve another situation which I haven't actually described - when I import a model from other formats, like OBJ, and it has many transparent materials already (like a complex tree, for instance), they get converted to Blender materials with Blend Mode and Shadow Mode Opaque. Then I need to change them all manually. $\endgroup$
    – michalpe
    May 1 at 20:49
  • $\begingroup$ Actually I once stumbled upon an addon that can possibly alter the properties of many materials in batch. I just need to find it again. Anyway, thank you very much for your help :) For sure I'm going to be using your script. $\endgroup$
    – michalpe
    May 1 at 20:50
  • $\begingroup$ You're welcome! I think this script can be also a good starting point for your other problem of modfiying whole models. Create a new script with the same structure and adapt the execute method to your needs. I would suggest that you load the model in an empty Blender project. In this way you don't have to mess with other objects and you can simply iterate over all materials in that context. $\endgroup$
    – eltitano
    May 2 at 1:53
0
$\begingroup$

e.g. for the sake of a being basic but thorough...

mat_g3 = bpy.data.materials.new("mat_g3")
mat_g3.use_backface_culling = False
mat_g3.blend_method = ("BLEND")
mat_g3.shadow_method = ("OPAQUE")
mat_g3.show_transparent_back = True
mat_g3.use_screen_refraction = True
mat_g3.use_nodes = True
input_g3 = mat_g3.node_tree.nodes["Principled BSDF"].inputs
input_g3["Base Color"].default_value = (1.0,0.0,0.0,0.333)
input_g3["Subsurface"].default_value = (0.0)
input_g3["Subsurface Radius"].default_value = (1.0, 0.0, 0.0)
input_g3["Subsurface Color"].default_value = (1.0,0.0,0.0,0.333)
input_g3["Metallic"].default_value = (0.666)
input_g3["Specular"].default_value = (0.0)
input_g3["Specular Tint"].default_value = (0.0)
input_g3["Roughness"].default_value = (0.0)
input_g3["Anisotropic"].default_value = (0.0)
input_g3["Anisotropic Rotation"].default_value = (0.0)
input_g3["Sheen"].default_value = (0.0)
input_g3["Sheen Tint"].default_value = (0.0)
input_g3["Clearcoat"].default_value = (0.0)
input_g3["Clearcoat Roughness"].default_value = (0.0)
input_g3["IOR"].default_value = (1.333)
input_g3["Transmission"].default_value = (0.0)
input_g3["Transmission Roughness"].default_value = (0.0)
input_g3["Emission"].default_value = (0.0,0.0,0.0,1.0)
input_g3["Emission Strength"].default_value = (1.0)
input_g3["Alpha"].default_value = (0.333)
mesh_g3.materials.clear()
mesh_g3.materials.append(mat_g3)
$\endgroup$
6
  • $\begingroup$ Is this a part of the Blender source code or is it a script to input in text editor and save the file as default template? $\endgroup$
    – michalpe
    Apr 26 at 8:11
  • $\begingroup$ In other words, how to use these lines? $\endgroup$
    – michalpe
    Apr 27 at 7:05
  • $\begingroup$ @michalpe You can open a text editor view in blender, create a new text item and paste the code and run the script. See stackoverflow.com/questions/11604548/… $\endgroup$
    – Gorgious
    Apr 29 at 12:15
  • $\begingroup$ @michalpe I'm afraid you can't change the default values unless you are ready to dive into the source code, which is more complicated than changing the python (.py) files in the installation folder. $\endgroup$
    – Gorgious
    Apr 29 at 12:23
  • $\begingroup$ I tried pasting the code into text editor but it doesn't look like a valid python script for Blender. Even for me, while I know next to zero about coding. There should always be that "import bpy" phrase at the beginning. Obviously this script gives an error in the first line when trying to run it. Honestly, Mick Saunders was not very helpful because he only threw some lines of code, not even explaining how to use them. He knew he deals with somebody without scripting knowledge. $\endgroup$
    – michalpe
    Apr 29 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.