My goal is to get a rendered image of an PLY mesh from command line. I wrote this script:
import os
import bpy
import sys
from math import pi
filename = 'web.ply'
in_dir = os.path.join('J:\\', 'aa')
item = os.path.join(in_dir, filename)
out_dir = os.path.join(in_dir, 'out')
os.makedirs(out_dir, exist_ok=True)
bpy.ops.object.delete()
bpy.ops.import_mesh.ply(filepath=os.path.join(item))
bpy.ops.object.lamp_add(type='SUN', radius=100, view_align=False, location=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.ops.object.select_all(action='DESELECT')
for ob in bpy.data.objects:
print (ob.type, ob.name)
if ob.type == 'MESH':
bpy.context.scene.objects.active = ob
ob.select = True
elif ob.type == 'CAMERA':
ob.data.clip_end = 1000000
ob.data.clip_start = 0.01
ob.select = False
else:
ob.select = False
bpy.ops.transform.rotate(value=pi/2, axis=(True, False, False))
bpy.context.scene.render.resolution_x = 692
bpy.context.scene.render.resolution_y = 900
bpy.context.scene.render.alpha_mode = 'TRANSPARENT'
bpy.ops.object.mode_set(mode='VERTEX_PAINT')
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
ctx = bpy.context.copy()
ctx['area'] = area
ctx['region'] = area.regions[-1]
bpy.ops.view3d.view_selected(ctx)
bpy.ops.view3d.camera_to_view_selected(ctx)
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.context.scene.render.image_settings.quality = 100
bpy.context.scene.render.filepath = os.path.join(out_dir, filename+'-preview.jpg')
bpy.ops.render.render(write_still=True)
It works, but not properly renders colors. The output image is in gray scale colors! I think that the issue is bpy.ops.object.mode_set(mode='VERTEX_PAINT') and the position inside the script. Something is wrong. Any tips? Here a PLY example file: http://156.54.99.175/3d/a.ply