Asset Pipeline v2 #145
@ -104,7 +104,6 @@ def apply_transfer_data(context: bpy.types.Context, transfer_data_map) -> None:
|
||||
if transfer_info.type == constants.VERTEX_GROUP_KEY:
|
||||
print(f"Transfering Data {constants.VERTEX_GROUP_KEY}: {name}")
|
||||
transfer_functions.transfer_vertex_group(
|
||||
context=context,
|
||||
vertex_group_name=transfer_info.name,
|
||||
target_obj=target_obj,
|
||||
source_obj=source_obj,
|
||||
|
@ -30,32 +30,56 @@ def init_vertex_groups(scene, obj):
|
||||
|
||||
|
||||
def transfer_vertex_group(
|
||||
context,
|
||||
vertex_group_name: str,
|
||||
target_obj: bpy.types.Object,
|
||||
source_obj: bpy.types.Object,
|
||||
):
|
||||
# Adapted from https://stackoverflow.com/questions/40969164/how-to-add-vertex-groups-from-one-object-to-another-without-replacing-them
|
||||
if target_obj == source_obj:
|
||||
return
|
||||
|
||||
source_data = source_obj.data
|
||||
target_data = target_obj.data
|
||||
|
||||
# sanity check: do source and target have the same amount of verts?
|
||||
if len(source_data.vertices) != len(target_data.vertices):
|
||||
print(
|
||||
'ERROR: objects {} and {} have different vertex counts.'.format(
|
||||
source_obj.name, target_obj.name
|
||||
)
|
||||
)
|
||||
return
|
||||
|
||||
data = {}
|
||||
for v in source_data.vertices:
|
||||
vg = v.groups
|
||||
vertex_index = v.index
|
||||
if len(vg) > 0:
|
||||
vgroup_collect = []
|
||||
for i in range(0, len(vg)):
|
||||
vgroup_collect.append((vg[i].group, vg[i].weight))
|
||||
data[vertex_index] = vgroup_collect
|
||||
|
||||
# check if group already exists...
|
||||
if target_obj.vertex_groups.get(vertex_group_name):
|
||||
target_obj.vertex_groups.remove(target_obj.vertex_groups.get(vertex_group_name))
|
||||
|
||||
source_obj.vertex_groups.active = source_obj.vertex_groups[vertex_group_name]
|
||||
# TODO Debug crashing / use context.temp_override(object=obj) style
|
||||
override = context.copy()
|
||||
override["selected_editable_objects"] = [target_obj, source_obj]
|
||||
override["active_object"] = source_obj
|
||||
override["object"] = source_obj
|
||||
with context.temp_override(**override):
|
||||
bpy.ops.object.data_transfer(
|
||||
data_type="VGROUP_WEIGHTS",
|
||||
use_create=True,
|
||||
vert_mapping='POLYINTERP_NEAREST',
|
||||
layers_select_src="ACTIVE",
|
||||
layers_select_dst="NAME",
|
||||
mix_mode="REPLACE",
|
||||
)
|
||||
# then add it
|
||||
target_obj.vertex_groups.new(name=vertex_group_name)
|
||||
|
||||
# write weights
|
||||
for v in target_data.vertices:
|
||||
for vi_source, vgroupIndex_weight in data.items():
|
||||
if v.index == vi_source:
|
||||
for i in range(0, len(vgroupIndex_weight)):
|
||||
group = target_obj.vertex_groups
|
||||
for vgs in range(0, len(group)):
|
||||
if group[vgs].name == vertex_group_name:
|
||||
group[vgs].add(
|
||||
(v.index,),
|
||||
vgroupIndex_weight[i][1],
|
||||
"REPLACE",
|
||||
)
|
||||
|
||||
|
||||
# VERTEX COLORS
|
||||
|
Loading…
Reference in New Issue
Block a user