I have created a cube in blender using python. I have modified the code a little bit by dividing each face of the cube further into 4 sub-faces. The example code I used is from Python API.
import bpy
vertices=[(1,1,-1),(1,-1,-1),(-1,-1,-1),(-1,1,-1),(1,1,1),(1,-1,1),(-1,-1,1),(-1,1,1)]
edges=[]
faces=[(0,1,2,3),(4,5,6,7),(0,4,7,3),(0,1,5,4),(1,2,6,5),(7,6,2,3)]
new_mesh=bpy.data.meshes.new("new_mesh")
new_mesh.from_pydata(vertices, edges, faces)
new_mesh.update()
new_object = bpy.data.objects.new("new_object", new_mesh)
view_layer=bpy.context.view_layer
view_layer.active_layer_collection.collection.objects.link(new_object)
Now, I want to convert two sub-face from each face of the cube into a glass material. This is what I did for adding a new glass material.
# Create a new material
face_to_convert_to_glass = [(0,1,2,3),(4,5,6,7)]
material_glass = bpy.data.materials.new('Green')
material_glass.use_nodes = True
p_BSDF = material_glass.node_tree.nodes["Principled BSDF"]
p_BSDF.inputs[0].default_value = (0, 1, 0, 1)
p_BSDF.inputs[7].default_value = 0
p_BSDF.inputs[15].default_value = 1
bpy.context.object.data.materials.append(material_glass)
It may be a simple thing but being a novice I am not able to link the glass material with the new_object I created. How do I achieve this using Python ?