0
$\begingroup$

I'm quite new to blender and I'm trying to render some meshes in .ply format shown below.

ply
format ascii 1.0
element vertex 470282
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
element face 940612
property list uchar int vertex_index
end_header
x y z R G B
...
3 v1 v2 v3
...

This is what I tried so far and I can get the rendered image like 1st image but without any colors. What should I add in the python script so the rendered image has colors like the 2nd image(I was able to get this by changing to vertex paint in blender GUI but couldn't get the same image during rendering)? Would it be easier if I save the mesh in .obj format?

import bpy, sys, os
from math import pi

#remove
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
for this_obj in bpy.data.objects:
    if this_obj.type=="MESH":
        this_obj.select_set(True)
        bpy.ops.object.delete(use_global=False, confirm=False)

file_loc = 'path/to/mesh.ply'
imported_object = bpy.ops.import_mesh.ply(filepath=file_loc)

#load
for this_obj in bpy.data.objects:
    if this_obj.type=="MESH":
        this_obj.select_set(True)
        bpy.context.view_layer.objects.active = this_obj
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.split_normals()

bpy.ops.object.mode_set(mode='OBJECT')
# bpy.ops.object.mode_set(mode='VERTEX_PAINT')

# render
        
# rotate obj
bpy.context.object.rotation_euler[0] = pi / 2
bpy.context.object.rotation_euler[2] = -3 * pi / 4

# view - 1
cam = bpy.data.objects['Camera']
cam.location.x = -0.05
cam.location.y = -1.2
cam.location.z = 0.52
cam.rotation_euler[0] = 1.13446
cam.rotation_euler[1] = 0
cam.rotation_euler[2] = 0

bpy.context.scene.render.image_settings.color_mode = 'RGBA'
bpy.context.scene.render.filepath = 'path/to/render.png'
bpy.ops.render.render(write_still=True)

enter image description here

enter image description here

$\endgroup$
4
  • $\begingroup$ Hello ! If you need to plug an attribute node set to the name of your vertex color attribute into the output of your material $\endgroup$
    – Gorgious
    Jan 12 at 7:02
  • $\begingroup$ thanks, could you elaborate a bit more? $\endgroup$
    – Qimin Chen
    Jan 12 at 18:24
  • $\begingroup$ i.stack.imgur.com/P2M51.png $\endgroup$
    – Gorgious
    Jan 13 at 12:47
  • $\begingroup$ Thank you so much, i figured it out $\endgroup$
    – Qimin Chen
    Jan 13 at 19:04

0

You must log in to answer this question.

Browse other questions tagged .