I tried to render an object (.obj & .ply - both doesn't work) using bpy(version 2.93.1
), but they were grey (without color), despite that they had vertex color.
how object looks like in meshlab
import bpy
import os
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()
file_loc = '../from-obj-to-photos/objects/glass.ply'
# imported_object = bpy.ops.import_scene.obj(filepath=file_loc, split_mode="OFF")
bpy.ops.import_mesh.ply(filepath=file_loc)
obj_object = bpy.context.selected_objects[0]
print('Imported name:', obj_object.name)
bpy.context.scene.render.filepath = '~/Desktop/photos/img.jpg'
bpy.context.scene.render.engine = 'CYCLES'
bpy.ops.render.render('INVOKE_DEFAULT', write_still=True)
After applying this code I got a grey glass like this: grey glass
Then I found out how to fix this, and my final code looks like this:
import bpy
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()
file_loc = '../from-obj-to-photos/objects/glass.ply'
# imported_object = bpy.ops.import_scene.obj(filepath=file_loc, split_mode="OFF")
bpy.ops.import_mesh.ply(filepath=file_loc)
obj_object = bpy.context.selected_objects[0]
print('Imported name:', obj_object.name)
bpy.data.objects["glass"].select_set(True)
bpy.ops.paint.vertex_paint_toggle()
#bpy.context.area.ui_type = 'ShaderNodeTree'
#bpy.ops.material.new()
mat = bpy.data.materials.get("Material")
if mat:
mat.node_tree.nodes.new("ShaderNodeVertexColor")
mat.node_tree.links.new(mat.node_tree.nodes[2].outputs['Color'], mat.node_tree.nodes[1].inputs['Base Color'])
bpy.context.scene.render.filepath = '~/Desktop/photos/img.jpg'
bpy.context.scene.render.engine = 'CYCLES'
bpy.ops.render.render('INVOKE_DEFAULT', write_still=True)
But it shows an error:
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
KeyError: 'bpy_prop_collection[key]: key "Base Color" not found'
And to fix it, I have to open Shader Editor, tap the button browse material to be linked
and choose Material
.
After executing code the glass is colored (result image). But I don't know how to automate processes from previous paragraph.
Also, i guess that converting vertex color to texture might fix this issue, but I didn't find anywhere how to do it using python.
I would be really grateful if anyone helps me with this issue.
I found the solution! if you pasteif len(bpy.context.active_object.data.materials) == 0: bpy.context.active_object.data.materials.append(bpy.data.materials['Material']) else: bpy.context.active_object.data.materials[0] = bpy.data.materials['Material']