Hi everyone.
I'd like to create a simple script in blender(Python) which creates a glossy shader from script, loading Diff, Rough, Normal textures from specified folder.
In Cycles I'd start with "import bpy ... bpy.data.materials.new(name="NewMaterial"), how can I do for a LuxCore shader?
Simple shaders from python script
Forum rules
Please upload a testscene that allows developers to reproduce the problem, and attach some images.
Please upload a testscene that allows developers to reproduce the problem, and attach some images.
Re: Simple shaders from python script
Here's an example:
Code: Select all
import bpy
mat = bpy.data.materials.new(name="Material")
tree_name = "Nodes_" + mat.name
node_tree = bpy.data.node_groups.new(name=tree_name, type="luxcore_material_nodes")
mat.luxcore.node_tree = node_tree
# User counting does not work reliably with Python PointerProperty.
# Sometimes, the material this tree is linked to is not counted as user.
node_tree.use_fake_user = True
###############################################################
# Add nodes to the node tree
nodes = node_tree.nodes
output = nodes.new("LuxCoreNodeMatOutput")
output.location = 300, 200
output.select = False
glossy2 = nodes.new("LuxCoreNodeMatGlossy2")
glossy2.location = 50, 200
node_tree.links.new(glossy2.outputs[0], output.inputs[0])
# Load image, for docs on this see https://docs.blender.org/api/current/bpy_extras.image_utils.html#module-bpy_extras.image_utils
import bpy_extras
diffuse_img = bpy_extras.image_utils.load_image("/home/simon/Downloads/diamond_CPU.jpg")
# Create imagemap node
diffuse_img_node = nodes.new("LuxCoreNodeTexImagemap")
diffuse_img_node.location = -200, 200
diffuse_img_node.image = diffuse_img
node_tree.links.new(diffuse_img_node.outputs[0], glossy2.inputs["Diffuse Color"])
###############################################################
# Assign to object (if you want)
obj = bpy.context.active_object
if obj.material_slots:
obj.material_slots[obj.active_material_index].material = mat
else:
obj.data.materials.append(mat)
# For viewport render, we have to update the luxcore object
# because the newly created material is not yet assigned there
obj.update_tag()
Re: Simple shaders from python script
Wonderful!!!!
Thank you very, very much!!
Thank you very, very much!!
Re: Simple shaders from python script
Ok, This is the result of my script! Last question: How can I change the colors of slots?
I've tried "ramp_node.ramp_items[0].ColorRampItem = (0.4,0.2,0.0)" but it didn't work.
I've tried "ramp_node.ramp_items[0].ColorRampItem = (0.4,0.2,0.0)" but it didn't work.
Re: Simple shaders from python script
You should use gamma = 1 for the roughness map.
Like this:
Re: Simple shaders from python script
Now it works! Thank you so much!
Why? Always in B/W map?You should use gamma = 1 for the roughness map
Re: Simple shaders from python script
Reverse gamma is always applied to any image map (gamma = 1.0 is indeed a null transformation) because the rendering is than supposed to be gamma corrected. Otherwise you would end with gamma correction applied two times to texture maps.
Re: Simple shaders from python script
Ok! Quite difficult to understand for me! I'll study gamma much better! Thank you Dade