Blender – Select Polygons by their Vertex Color

import bpy
from mathutils import Color

threshold = .1
obj = bpy.context.object

bpy.ops.object.mode_set(mode="OBJECT")

colors = obj.data.vertex_colors.active.data
selected_polygons = list(filter(lambda p: p.select, obj.data.polygons))

if len(selected_polygons):
    p = selected_polygons[0]
    r = g = b = 0
    for i in p.loop_indices:
        c = colors[i].color
        r += c[0]
        g += c[1]
        b += c[2]
    r /= p.loop_total
    g /= p.loop_total
    b /= p.loop_total
    target = Color((r, g, b))

    for p in obj.data.polygons:
        r = g = b = 0
        for i in p.loop_indices:
            c = colors[i].color
            r += c[0]
            g += c[1]
            b += c[2]
        r /= p.loop_total
        g /= p.loop_total
        b /= p.loop_total
        source = Color((r, g, b))

        print(target, source)

        if (abs(source.r - target.r) < threshold and
            abs(source.g - target.g) < threshold and
            abs(source.b - target.b) < threshold):

            p.select = True

bpy.ops.object.editmode_toggle()

https://blenderartists.org/t/select-vertices-according-to-vertex-color/471451/10

Blender – Flatten normals of mesh data in bulk

Recently I had to fix per-face-vertex normals for a project and I had to come up with a script snippet to ease the manual touches involved in the task.

Run this script on any selected object in Object Mode.

import bpy
print("Flattening per face normals ..")
current_obj = bpy.context.active_object
# Calc
current_obj.data.calc_normals_split()
# Vertex-per-face Normals
n = []
for face in current_obj.data.loops:
    n.append(face.normal.copy())
    n[face.index][1] = 0
    n[face.index] = n[face.index].normalized()
    print("i", face.index , "n", face.normal, "n2", n[face.index])

current_obj.data.normals_split_custom_set(n)
current_obj.data.update()    
current_obj.data.free_normals_split()
print("Done.")

After execution you may change into Edit Mode and see the normals flattened.

Blender Python Issue __version__

Recently I ran into an issue with Blender after adding the environment variables PYTHONHOME and PYTHONPATH.

I first didn’t even realize that this might be the issue since they pointed to a version of Python with which Blender claims to be compatible. But of course Blender comes with its own Python versions. Blender still works, but just some of that ‘little’ things are messed up.

So if you ‘re not able to load your Add-Ons or having strange results while exporting models, remember that you may have set these variables.

Blender – Import multiple files

Importing multiple files in Blender is okay-ish. While the underlying API supports that, the higher GUI does not (or was able, but is not anymore … ). This is for Windows .. but easily adaptable for *nix systems.

How to run?

Copy & past the one of the code snippets into Blender’s Text Editor. Don’t forget to create a New text file. You’re not able to paste nor type if there’s no present.

Objects (OBJ)

import os
import bpy

# put the location to the folder where the objs are located here in this fashion
# this line will only work on windows ie C:\objects
path_to_obj_dir = os.path.join('C:\\', 'objects')

# get list of all files in directory
file_list = sorted(os.listdir(path_to_obj_dir))

# get a list of files ending in 'obj'
obj_list = [item for item in file_list if item.endswith('.obj')]

# loop through the strings in obj_list and add the files to the scene
for item in obj_list:
    path_to_file = os.path.join(path_to_obj_dir, item)
    bpy.ops.import_scene.obj(filepath = path_to_file)
    # if heavy importing is expected 
    # you may want use saving to main file after every import 
    bpy.ops.wm.save_mainfile(filepath = "C:\\To\\Your\\File.blend")

Collada (DAE)

Importing Collada files differs a little. Object (OBJ) importing is located inside BPY’s import functionas, Collada’s is not …

import os
import bpy

# put the location to the folder where the objs are located here in this fashion
# this line will only work on windows ie C:\objects
path_to_obj_dir = os.path.join('C:\\', 'Some\\Folder\\Collada\\')

# get list of all files in directory
file_list = sorted(os.listdir(path_to_obj_dir))

# get a list of files ending in 'dae'
obj_list = [item for item in file_list if item.endswith('.dae')]

# loop through the strings in obj_list and add the files to the scene
for item in obj_list:
    path_to_file = os.path.join(path_to_obj_dir, item)
    bpy.ops.wm.collada_import(filepath = path_to_file)
    # if heavy importing is expected 
    # you may want use saving to main file after every import
    bpy.ops.wm.save_mainfile(filepath = "C:\\To\\Your\\File.blend")

Filmbox (FBX)

Importing FBX files is again, a bit different but the same ..

import os
import bpy

# put the location to the folder where the FBXs are located here in this fashion
# this line will only work on windows ie C:\objects
path_to_obj_dir = os.path.join('C:\\', 'objects')

# get list of all files in directory
file_list = sorted(os.listdir(path_to_obj_dir))

# get a list of files ending in 'fbx'
obj_list = [item for item in file_list if item.endswith('.fbx')]

# loop through the strings in obj_list and add the files to the scene
for item in obj_list:
    path_to_file = os.path.join(path_to_obj_dir, item)
    bpy.ops.import_scene.fbx(filepath = path_to_file)
    # if heavy importing is expected 
    # you may want use saving to main file after every import 
    bpy.ops.wm.save_mainfile(filepath = "C:\\To\\Your\\File.blend")

GL Transmission Format (GLB & GLTF)

Importing GLB / GLTF files is as well inside the import_scene obj, tested with Blender 2.81a:

import os
import bpy

# put the location to the folder where the GLBs are located here in this fashion
# this line will only work on windows ie C:\objects
path_to_obj_dir = os.path.join('C:\\', 'objects')

# get list of all files in directory
file_list = sorted(os.listdir(path_to_obj_dir))

# get a list of files ending in 'glb' (or 'gltf')
obj_list = [item for item in file_list if item.endswith('.glb')]

# loop through the strings in obj_list and add the files to the scene
for item in obj_list:
    path_to_file = os.path.join(path_to_obj_dir, item)
    bpy.ops.import_scene.gltf(filepath = path_to_file)
    # if heavy importing is expected 
    # you may want use saving to main file after every import 
    bpy.ops.wm.save_mainfile(filepath = "C:\\To\\Your\\File.blend")

Kudos go to .. 

The user poor has already created importing and exporting scripts including the Blender plugin interface. This needs to be adapted to Collada functions only, et voila.

References