Last active
March 20, 2019 00:32
-
-
Save Kwizatz/cd365e83f986c73615d68eaf4a461861 to your computer and use it in GitHub Desktop.
Blender script to paint OBJ file vertices with material diffuse color and export as AeonEngine mesh.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/blender -b -P | |
import bpy | |
import mathutils | |
import glob | |
# This was created to work with Kenney's 3D objects (http://kenney.itch.io/kenney-donation), | |
# but should work with any other OBJ files. | |
# This is a batch script, run in a folder containing at least one 3d .obj file as follows: | |
# blender -b -P VertexPaintMaterials.py | |
for obj in glob.glob('*.obj'): | |
bpy.ops.import_scene.obj(filepath=obj) | |
mesh = bpy.data.objects[len(bpy.data.objects) - 1] | |
# Scale by 32 units | |
mesh.matrix_basis = mesh.matrix_basis * mathutils.Matrix.Scale(32,4) | |
bpy.context.scene.objects.active = mesh | |
# Apply transforms | |
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) | |
mesh.data.use_paint_mask = True | |
for uv_layer in mesh.data.uv_layers: | |
bpy.ops.mesh.uv_texture_remove() | |
if len(mesh.data.vertex_colors)==0: | |
bpy.ops.mesh.vertex_color_add() | |
for i in range(0,len(mesh.data.materials)): | |
bpy.ops.object.mode_set(mode='EDIT') | |
mesh.active_material_index = i | |
bpy.ops.mesh.select_all(action='SELECT') | |
bpy.ops.mesh.quads_convert_to_tris() | |
bpy.ops.mesh.tris_convert_to_quads() | |
bpy.ops.mesh.select_all(action='DESELECT') | |
bpy.ops.object.material_slot_select() | |
bpy.ops.object.mode_set(mode='VERTEX_PAINT') | |
bpy.data.brushes["Draw"].color = mesh.data.materials[i].diffuse_color | |
bpy.ops.paint.vertex_color_set() | |
bpy.ops.object.mode_set(mode='EDIT') | |
bpy.ops.object.material_slot_deselect() | |
bpy.ops.export_mesh.msh(filepath=obj.replace('.obj','')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment