Asset Pipeline v2 #145

Closed
Nick Alberelli wants to merge 431 commits from (deleted):feature/asset-pipeline-v2 into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
2 changed files with 48 additions and 40 deletions
Showing only changes of commit e9f7278744 - Show all commits

View File

@ -73,7 +73,7 @@ def init_transfer_data(
transfer_functions.init_constraints(scene, obj) transfer_functions.init_constraints(scene, obj)
# transfer_functions.init_vertex_colors(scene, obj) # transfer_functions.init_vertex_colors(scene, obj)
# transfer_functions.init_uv_layers(scene, obj) # transfer_functions.init_uv_layers(scene, obj)
transfer_functions.init_shap_keys(scene, obj) transfer_functions.init_shape_keys(scene, obj)
transfer_functions.init_attributes(scene, obj) transfer_functions.init_attributes(scene, obj)

View File

@ -358,13 +358,6 @@ def shape_key_set_active(obj, shape_key_name):
obj.active_shape_key_index = index obj.active_shape_key_index = index
def shape_key_move(context, obj, shape_key_name, top=True):
move_type = "TOP" if top else "BOTTOM"
shape_key_set_active(obj, shape_key_name)
with context.temp_override(object=obj):
bpy.ops.object.shape_key_move(type=move_type)
def shape_key_closest_face_to_point(bm_source, p_target, bvh_tree=None): def shape_key_closest_face_to_point(bm_source, p_target, bvh_tree=None):
if not bvh_tree: if not bvh_tree:
bvh_tree = mathutils.bvhtree.BVHTree.FromBMesh(bm_source) bvh_tree = mathutils.bvhtree.BVHTree.FromBMesh(bm_source)
@ -409,18 +402,18 @@ def shape_keys_clean(obj):
obj.transfer_data_ownership, constants.SHAPE_KEY_KEY obj.transfer_data_ownership, constants.SHAPE_KEY_KEY
) )
for shape_key in obj.data.shape_keys.key_blocks: for shape_key in obj.data.shape_keys.key_blocks:
# Move Shape Keys relative to themselves to the top (usually basis key)
if shape_key.relative_key == shape_key:
shape_key_move(context, obj, shape_key.name)
if not asset_suffix.get_basename(shape_key.name) in transfer_data_list: if not asset_suffix.get_basename(shape_key.name) in transfer_data_list:
obj.shape_key_remove(shape_key) obj.shape_key_remove(shape_key)
def shape_key_is_missing(transfer_info): def shape_key_is_missing(transfer_info):
obj = transfer_info.id_data if not transfer_info.type == constants.SHAPE_KEY_KEY:
if obj.type != "MESH" or obj.data.shape_keys is None:
return return
obj = transfer_info.id_data
if obj.type != 'MESH':
return
if not obj.data.shape_keys:
return True
return transfer_core.transfer_info_is_missing( return transfer_core.transfer_info_is_missing(
transfer_info, transfer_info,
constants.SHAPE_KEY_KEY, constants.SHAPE_KEY_KEY,
@ -428,9 +421,18 @@ def shape_key_is_missing(transfer_info):
) )
def init_shap_keys(scene, obj): def init_shape_keys(scene, obj):
if obj.type != "MESH" or obj.data.shape_keys is None: if obj.type != "MESH" or obj.data.shape_keys is None:
return return
# Check that the order is legal.
# Key Blocks must be ordered after the key they are Relative To.
for i, kb in enumerate(obj.data.shape_keys.key_blocks):
if kb.relative_key:
base_shape_idx = obj.data.shape_keys.key_blocks.find(kb.relative_key.name)
if base_shape_idx > i:
raise Exception(f'Shape Key "{kb.name}" must be ordered after its base shape "{kb.relative_key.name}" on object "{obj.name}".')
transfer_core.transfer_info_init( transfer_core.transfer_info_init(
scene, obj, obj.data.shape_keys.key_blocks, constants.SHAPE_KEY_KEY scene, obj, obj.data.shape_keys.key_blocks, constants.SHAPE_KEY_KEY
) )
@ -442,42 +444,48 @@ def transfer_shape_key(
target_obj: bpy.types.Object, target_obj: bpy.types.Object,
source_obj: bpy.types.Object, source_obj: bpy.types.Object,
): ):
# BASIS SHAPE KEY MUST BE PASSED FIRST OTHERWISE THIS WILL ERROR OUT if not source_obj.data.shape_keys:
return
sk_source = source_obj.data.shape_keys.key_blocks.get(shape_key_name) sk_source = source_obj.data.shape_keys.key_blocks.get(shape_key_name)
print(f"Moving shape key: {shape_key_name}") assert sk_source
# If key is relative to another key that doesn't exist yet sk_target = None
if sk_source.relative_key != sk_source: if not target_obj.data.shape_keys:
relative_key = target_obj.data.shape_keys.key_blocks.get( sk_target = target_obj.shape_key_add()
sk_source.relative_key.name if not sk_target:
) sk_target = target_obj.data.shape_keys.key_blocks.get(shape_key_name)
if not relative_key: if not sk_target:
print( sk_target = target_obj.shape_key_add()
f"Shape Key '{sk_source.name}' failed to find Relative Key '{sk_source.relative_key.name}' on Object '{target_obj.name}'"
)
return
# Remove existing shape keys that match name
if target_obj.data.shape_keys is not None:
old_sk = target_obj.data.shape_keys.key_blocks.get(shape_key_name)
if old_sk:
target_obj.shape_key_remove(old_sk)
sk_target = target_obj.shape_key_add()
sk_target.name = sk_source.name sk_target.name = sk_source.name
sk_target.vertex_group = sk_source.vertex_group sk_target.vertex_group = sk_source.vertex_group
sk_target.relative_key = target_obj.data.shape_keys.key_blocks[ if sk_source.relative_key != sk_source:
sk_source.relative_key.name relative_key = None
] if target_obj.data.shape_keys:
relative_key = target_obj.data.shape_keys.key_blocks.get(
sk_source.relative_key.name
)
if relative_key:
sk_target.relative_key = relative_key
else:
# If the base shape of one of our shapes was removed by another task layer,
# the result will probably be pretty bad, but it's not a catastrophic failure.
# Proceed with a warning.
print(
f'Warning: Base shape "{sk_source.relative_key.name}" of Key "{sk_source.name}" was removed from "{target_obj.name}"'
)
sk_target.slider_min = sk_source.slider_min
sk_target.slider_max = sk_source.slider_max
sk_target.value = sk_source.value
sk_target.mute = sk_source.mute
bm_source = bmesh.new() bm_source = bmesh.new()
bm_source.from_mesh(source_obj.data) bm_source.from_mesh(source_obj.data)
bm_source.faces.ensure_lookup_table() bm_source.faces.ensure_lookup_table()
bvh_tree = mathutils.bvhtree.BVHTree.FromBMesh(bm_source) bvh_tree = mathutils.bvhtree.BVHTree.FromBMesh(bm_source)
tris_dict = shape_key_tris_per_face(bm_source) tris_dict = shape_key_tris_per_face(bm_source)
for i, vert in enumerate(target_obj.data.vertices): for i, vert in enumerate(target_obj.data.vertices):
p = vert.co p = vert.co
face = shape_key_closest_face_to_point(bm_source, p, bvh_tree) face = shape_key_closest_face_to_point(bm_source, p, bvh_tree)
@ -577,7 +585,7 @@ def transfer_attribute(
type=source_attribute.data_type, type=source_attribute.data_type,
domain=source_attribute.domain, domain=source_attribute.domain,
) )
print(f"Transferring Attribute{attribute_name}") # print(f"Transfering Attribute {attribute_name}")
for source_data_item in source_attribute.data.items(): for source_data_item in source_attribute.data.items():
index = source_data_item[0] index = source_data_item[0]
source_data = source_data_item[1] source_data = source_data_item[1]