Asset Pipeline v2 #145
@ -104,6 +104,7 @@ def apply_transfer_data(context: bpy.types.Context, transfer_data_map) -> None:
|
|||||||
if transfer_info.type == constants.VERTEX_GROUP_KEY:
|
if transfer_info.type == constants.VERTEX_GROUP_KEY:
|
||||||
print(f"Transfering Data {constants.VERTEX_GROUP_KEY}: {name}")
|
print(f"Transfering Data {constants.VERTEX_GROUP_KEY}: {name}")
|
||||||
transfer_functions.transfer_vertex_group(
|
transfer_functions.transfer_vertex_group(
|
||||||
|
context=context,
|
||||||
vertex_group_name=transfer_info.name,
|
vertex_group_name=transfer_info.name,
|
||||||
target_obj=target_obj,
|
target_obj=target_obj,
|
||||||
source_obj=source_obj,
|
source_obj=source_obj,
|
||||||
|
@ -30,55 +30,31 @@ def init_vertex_groups(scene, obj):
|
|||||||
|
|
||||||
|
|
||||||
def transfer_vertex_group(
|
def transfer_vertex_group(
|
||||||
|
context,
|
||||||
vertex_group_name: str,
|
vertex_group_name: str,
|
||||||
target_obj: bpy.types.Object,
|
target_obj: bpy.types.Object,
|
||||||
source_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:
|
if target_obj == source_obj:
|
||||||
return
|
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):
|
if target_obj.vertex_groups.get(vertex_group_name):
|
||||||
target_obj.vertex_groups.remove(target_obj.vertex_groups.get(vertex_group_name))
|
target_obj.vertex_groups.remove(target_obj.vertex_groups.get(vertex_group_name))
|
||||||
|
|
||||||
# then add it
|
source_obj.vertex_groups.active = source_obj.vertex_groups[vertex_group_name]
|
||||||
target_obj.vertex_groups.new(name=vertex_group_name)
|
# TODO Debug crashing / use context.temp_override(object=obj) style
|
||||||
|
override = context.copy()
|
||||||
# write weights
|
override["selected_editable_objects"] = [target_obj, source_obj]
|
||||||
for v in target_data.vertices:
|
override["active_object"] = source_obj
|
||||||
for vi_source, vgroupIndex_weight in data.items():
|
override["object"] = source_obj
|
||||||
if v.index == vi_source:
|
with context.temp_override(**override):
|
||||||
for i in range(0, len(vgroupIndex_weight)):
|
bpy.ops.object.data_transfer(
|
||||||
group = target_obj.vertex_groups
|
data_type="VGROUP_WEIGHTS",
|
||||||
for vgs in range(0, len(group)):
|
use_create=True,
|
||||||
if group[vgs].name == vertex_group_name:
|
vert_mapping='POLYINTERP_NEAREST',
|
||||||
group[vgs].add(
|
layers_select_src="ACTIVE",
|
||||||
(v.index,),
|
layers_select_dst="NAME",
|
||||||
vgroupIndex_weight[i][1],
|
mix_mode="REPLACE",
|
||||||
"REPLACE",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user