I am using python script to read the mesh data present in one .blend file and apply materials to it which is present in another .blend file. The output is exported as a .png.
I have written the below script to achieve this.
If I skip applying material to the mesh object, the mesh object is rendered as is in the .png. However, if I apply material to the mesh object, I am getting an empty .png file. Nothing gets rendered.
Expected: The materials should get applied to the mesh model and get rendered.
Not sure what I am missing. Tried quite a few ways, no luck. Appreciate it, if you could let me know where I am going wrong.
import bpy
FILE_PATH = "/home/vandy/startUpProjects/tailorMade/pythonScripts/"
def render_different_materials_on_mesh_object():
filepath = FILE_PATH + "remote.blend"
mesh_ob = get_mesh_object(filepath)
material_name = append_material()
apply_material_to_mesh_object(material_name, mesh_ob)
render_as_png()
def get_mesh_object(filepath):
bpy.ops.wm.open_mainfile(filepath=filepath)
scene = bpy.context.scene
for ob in scene.objects:
if ob.type == 'MESH':
bpy.context.scene.objects.active = ob
break
return bpy.context.object
def append_material():
filename = "mainMaterial"
section = "\\Action\\"
directory = FILE_PATH
filepath = FILE_PATH + "material1.blend/Material/mainMaterial"
bpy.ops.wm.append(
filepath="//material1.blend\\Material\\",
directory=FILE_PATH + "material1.blend\\Material\\",
filename=filename,
link = False)
return filename
def apply_material_to_mesh_object(material_name, mesh_ob):
# Get material
mat = bpy.data.materials.get(material_name)
if mat is None:
# create material
mat = bpy.data.materials.new(name="Material")
# Assign it to object
if mesh_ob.data.materials:
# assign to 1st material slot
mesh_ob.data.materials[0] = mat
else:
# no slots
mesh_ob.data.materials.append(mat)
def render_as_png():
bpy.data.scenes['Scene'].render.filepath = FILE_PATH + 'test.png'
bpy.ops.render.render(write_still=True)
render_different_materials_on_mesh_object()