Useful Blender Scripts - Ezekial711/MonsterHunterWorldModding GitHub Wiki

The following is a disjoint collection of blender scripts that might be of use to modellers:

Lists all unused Bone Function IDs on a scene (Useful for adding physics bones)

used = sorted([e["boneFunction"] for e in C.scene.objects if e.type == "EMPTY" and "boneFunction" in e]) 
unused = set(range(512)) 
unused.difference(set(used)) 
print (unused)

Prints duplicated bone functions

import bpy
boneFunctions = {}
repeats = set()
for bone in [obj for obj in bpy.context.scene.objects if obj.type == "EMPTY" and "boneFunction" in obj]:
    func = bone["boneFunction"]
    if func in boneFunctions:
        boneFunctions[func].append(bone.name)
        repeats.add(func)
    else:
        boneFunctions[func] = [bone.name]

for repeated in repeats:
    print("Bone Function: %d\n"%repeated+'\n'.join(["\t%s"%repeat for repeat in boneFunctions[repeated]]))

Mark vertices with repeated UVs

for meshobj in sorted([o for o in bpy.context.scene.objects if o.type=="MESH"], key=lambda x: x.name):
    mesh = meshobj.data
    uvList = []
    for layer in mesh.uv_layers:
        uvMap = {}
        for loop,loopUV in zip(mesh.loops, layer.data):
            uvPoint = (loopUV.uv[0],1-loopUV.uv[1])
            if loop.vertex_index in uvMap and uvMap[loop.vertex_index] != uvPoint:
                o = bpy.data.objects.new("YAVP-%s"%meshobj.name, None )
                bpy.context.scene.objects.link( o )
                o.location = meshobj.matrix_world * mesh.vertices[loop.vertex_index].co
                o.show_x_ray = True
                o.empty_draw_size = .5
            else:
                uvMap[loop.vertex_index] = uvPoint
        uvList.append(uvMap)

Renames bones to their bone function id (and renames the appropriate vertex groups as well)

renameTable = {}

for bone in [o for o in bpy.context.scene.objects if o.type == "EMPTY" and "boneFunction" in o]:
    newname = "BoneFunction%03d"%bone["boneFunction"]
    renameTable[bone .name] = newname 
    bone.name = newname

for mesh in [o for o in bpy.context.scene.objects if o.type == "MESH"]:
    for group in mesh.vertex_groups:
        if group.name in renameTable:
            group.name = renameTable[group.name]

Copy properties of object to all selected meshes

active = bpy.context.active_object
for obj in [ o for o in bpy.context.selected_objects if o.type == "MESH" and "blockLabel" not in o.data]:
	for property in active.data.keys():
		obj.data[property] = active.data[property]

Remove Vertex Colors of all selected meshes

for obj in [o for o in bpy.context.selected_objects if o.type == "MESH"]:
	colors = list(obj.data.vertex_colors)
	for c in colors:
		obj.data.vertex_colors.remove(c)

Clean Weighting Data of all selected meshes

for obj in [o for o in bpy.context.selected_objects if o.type == "MESH"]:
	weights = list(obj.vertex_groups)
	for w in weights:
		obj.vertex_groups.remove(w)

Set modifier on selection

for obj in [o for o in bpy.context.selected_objects if o.type == "MESH"]:
	modifiers =obj.modifiers
	m = modifiers.new("Mod Name","MODIFIER TYPE")
	#Properties you want to be set are done as m.PropName = Value

Mass weight selection to a single bone

for obj in [o for o in bpy.context.selected_objects if o.type == "MESH"]:
	weights = obj.vertex_groups
	group = weights.new("Your Bone Goes Here")
	group.add([i for i in range(len(obj.data.vertices))],1.0,'REPLACE')

Mass Apply modifiers to selection

for obj in [o for o in bpy.context.selected_objects if o.type == "MESH"]:
	modifiers =obj.modifiers
	for m in modifiers:
		bpy.context.scene.objects.active = obj
		bpy.ops.object.modifier_apply(modifier=m.name)