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:
|
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,31 +30,55 @@ 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))
|
||||||
|
|
||||||
source_obj.vertex_groups.active = source_obj.vertex_groups[vertex_group_name]
|
# then add it
|
||||||
# TODO Debug crashing / use context.temp_override(object=obj) style
|
target_obj.vertex_groups.new(name=vertex_group_name)
|
||||||
override = context.copy()
|
|
||||||
override["selected_editable_objects"] = [target_obj, source_obj]
|
# write weights
|
||||||
override["active_object"] = source_obj
|
for v in target_data.vertices:
|
||||||
override["object"] = source_obj
|
for vi_source, vgroupIndex_weight in data.items():
|
||||||
with context.temp_override(**override):
|
if v.index == vi_source:
|
||||||
bpy.ops.object.data_transfer(
|
for i in range(0, len(vgroupIndex_weight)):
|
||||||
data_type="VGROUP_WEIGHTS",
|
group = target_obj.vertex_groups
|
||||||
use_create=True,
|
for vgs in range(0, len(group)):
|
||||||
vert_mapping='POLYINTERP_NEAREST',
|
if group[vgs].name == vertex_group_name:
|
||||||
layers_select_src="ACTIVE",
|
group[vgs].add(
|
||||||
layers_select_dst="NAME",
|
(v.index,),
|
||||||
mix_mode="REPLACE",
|
vgroupIndex_weight[i][1],
|
||||||
|
"REPLACE",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user