Skip to content

Instantly share code, notes, and snippets.

@giangm9
Last active April 10, 2021 08:39
Show Gist options
  • Save giangm9/5b2bdbf9ab07b3e5b538e948dd845390 to your computer and use it in GitHub Desktop.
Save giangm9/5b2bdbf9ab07b3e5b538e948dd845390 to your computer and use it in GitHub Desktop.

Append data from other blender files (example material)

with bpy.data.libraries.load(MATERIAL_LIBRARY_FILE) as (data_from, data_to):
    data_to.materials = data_from.materials

for mat in templateMat:
    materialList.append(mat.name)

remove all materials

import bpy
for material in bpy.data.materials:
    material.user_clear()
    bpy.data.materials.remove(material)
@giangm9
Copy link
Author

giangm9 commented Feb 7, 2021

Remove seconday uv layers when import from Unity fbx exporter

import bpy
for obj in bpy.data.objects:
    if obj.type != 'MESH':
        continue
    layers = obj.data.uv_layers
    if 'UVSet1' in layers:
        layers.remove(layers['UVSet1'])

@giangm9
Copy link
Author

giangm9 commented Feb 7, 2021

Link origin color to pbr node in light map

# Link default default value
import bpy
for obj in bpy.data.objects:
    
    if (not 'TLM_ObjectProperties' in obj) or (not 'tlm_mesh_lightmap_use' in obj['TLM_ObjectProperties']) :
        continue
    
    for mat in obj.data.materials:
        if not mat.use_nodes:
            continue
        
        nodes = mat.node_tree.nodes
        links = mat.node_tree.links
        

        inp = nodes['LM_P'] if 'LM_P' in nodes else nodes['Lightmap_BasecolorNode_A']

        
        links.new(inp.outputs[0], nodes['Principled BSDF'].inputs['Base Color'])

@giangm9
Copy link
Author

giangm9 commented Mar 5, 2021

import bpy
for i in range(1, 31):
    bpy.ops.import_scene.obj(filepath=('D:\\vrkid\\convert\\exported_frames\\teacher-1\\obj\\teacher_%06d.obj' % i))
    objs = bpy.context.selected_objects[:]
    
    for obj in objs:
        for base_obj in bpy.data.collections['Main'].all_objects:
            if obj.name.startswith(base_obj.name):
                
                print(obj.name, base_obj.name)
                obj.name = obj.name[:len(base_obj.name) + 4]
                
                obj.data.materials[0] = base_obj.data.materials[0]
                for o in bpy.data.objects:
                    o.select_set(False)
                bpy.context.view_layer.objects.active = base_obj
                obj.select_set(True)
                bpy.ops.object.join_uvs()
                

@giangm9
Copy link
Author

giangm9 commented Mar 7, 2021

Export PLY

import bpy

for i in range(1, 100):
    if i % 10 != 0:
        continue
    bpy.context.scene.frame_set(i)
    
    bpy.ops.export_mesh.ply(filepath='D:\\three-morph\\public\\frames\\%06d.ply' % i,
        filter_glob='*.ply', 
        use_ascii=False, 
        use_selection=True, 
        use_mesh_modifiers=True, 
        use_normals=False, 
        use_uv_coords=False, 
        use_colors=False, 
        global_scale=1.0, 
        axis_forward='-Z', 
        axis_up='Y')

@giangm9
Copy link
Author

giangm9 commented Mar 15, 2021

generate meshes from MeshCacheSequence

import bpy
SKIP_RATE = 5

    
def generate_frame(name):
    
    for i in range(1, 30):
        if i % SKIP_RATE != 0:
            continue
        
        bpy.context.scene.frame_set(i)
        obj = bpy.data.objects[name]
        frame = obj.copy()
        frame.data  = obj.data.copy()
        
        bpy.data.collections['Collection'].objects.link(frame)
        bpy.context.view_layer.objects.active = frame

        bpy.ops.object.modifier_apply(modifier="MeshSequenceCache")
        frame.hide_viewport = True
        frame.parent = obj

generate_frame('Vay')

@giangm9
Copy link
Author

giangm9 commented Mar 29, 2021

clear morph frames

def clear_frames():
    for obj in bpy.data.objects:
        if obj.get('is_frame'):
            bpy.data.objects.remove(obj)    

@giangm9
Copy link
Author

giangm9 commented Apr 10, 2021

Assign mesh sequence cache

def assign_abc(obj):
    for child in obj.children:
        child.modifiers.clear()
        mod = child.modifiers.new("MeshSequenceCache", "MESH_SEQUENCE_CACHE")
        mod.cache_file = bpy.data.cache_files['Girl.abc']
        
        for path in mod.cache_file.object_paths:
            if child.name in path.path:
                mod.object_path = path.path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment