Asset Pipeline v2 #145
@ -2,7 +2,7 @@ import bpy
|
|||||||
|
|
||||||
from . import core
|
from . import core
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from . import transferable_data
|
from . import transferable_data, transfer_functions
|
||||||
|
|
||||||
TASK_LAYER_TYPES = [
|
TASK_LAYER_TYPES = [
|
||||||
'RIG',
|
'RIG',
|
||||||
@ -19,6 +19,7 @@ class ASSETPIPE_OT_update_ownership(bpy.types.Operator):
|
|||||||
file_name = bpy.path.basename(bpy.context.blend_data.filepath)
|
file_name = bpy.path.basename(bpy.context.blend_data.filepath)
|
||||||
task_layer_name = file_name.split(".")[-2]
|
task_layer_name = file_name.split(".")[-2]
|
||||||
transferable_data.vertex_groups_update(obj, task_layer_name)
|
transferable_data.vertex_groups_update(obj, task_layer_name)
|
||||||
|
transferable_data.modifiers_update(obj, task_layer_name)
|
||||||
return {'FINISHED'}
|
return {'FINISHED'}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
import bpy
|
import bpy
|
||||||
from bpy import context
|
from bpy import context
|
||||||
|
|
||||||
def transfer_vertex_group(context, vertex_group_name:str, target_obj:bpy.types.Object, source_obj:bpy.types.Object):
|
|
||||||
|
def transfer_vertex_group(
|
||||||
|
context,
|
||||||
|
vertex_group_name: str,
|
||||||
|
target_obj: bpy.types.Object,
|
||||||
|
source_obj: bpy.types.Object,
|
||||||
|
):
|
||||||
source_obj.vertex_groups.active = source_obj.vertex_groups[vertex_group_name]
|
source_obj.vertex_groups.active = source_obj.vertex_groups[vertex_group_name]
|
||||||
override = context.copy()
|
override = context.copy()
|
||||||
override["selected_editable_objects"] = [target_obj, source_obj]
|
override["selected_editable_objects"] = [target_obj, source_obj]
|
||||||
override["active_object"] = source_obj
|
override["active_object"] = source_obj
|
||||||
override["object"] = source_obj #TODO test if needed
|
override["object"] = source_obj # TODO test if needed
|
||||||
with context.temp_override(**override ):
|
with context.temp_override(**override):
|
||||||
bpy.ops.object.data_transfer(
|
bpy.ops.object.data_transfer(
|
||||||
data_type="VGROUP_WEIGHTS",
|
data_type="VGROUP_WEIGHTS",
|
||||||
use_create=True,
|
use_create=True,
|
||||||
@ -16,3 +22,58 @@ def transfer_vertex_group(context, vertex_group_name:str, target_obj:bpy.types.O
|
|||||||
layers_select_dst="NAME",
|
layers_select_dst="NAME",
|
||||||
mix_mode="REPLACE",
|
mix_mode="REPLACE",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def transfer_modifier(item, obj_target):
|
||||||
|
# remove old and sync existing modifiers
|
||||||
|
obj_source = item.id
|
||||||
|
old_mod = obj_target.modifiers.get(item.name)
|
||||||
|
if old_mod:
|
||||||
|
obj_target.modifiers.remove(old_mod)
|
||||||
|
|
||||||
|
# transfer new modifiers
|
||||||
|
for i, mod in enumerate(obj_source.modifiers):
|
||||||
|
if mod.name == item.name:
|
||||||
|
mod_new = obj_target.modifiers.new(mod.name, mod.type)
|
||||||
|
# sort new modifier at correct index (default to beginning of the stack)
|
||||||
|
idx = 0
|
||||||
|
if i > 0:
|
||||||
|
name_prev = obj_source.modifiers[i - 1].name
|
||||||
|
for target_mod_i, target_mod in enumerate(obj_target.modifiers):
|
||||||
|
if target_mod.name == name_prev:
|
||||||
|
idx = target_mod_i + 1
|
||||||
|
bpy.ops.object.modifier_move_to_index(
|
||||||
|
{'object': obj_target}, modifier=mod_new.name, index=idx
|
||||||
|
)
|
||||||
|
mod_target = obj_target.modifiers.get(mod.name)
|
||||||
|
props = [p.identifier for p in mod.bl_rna.properties if not p.is_readonly]
|
||||||
|
for prop in props:
|
||||||
|
value = getattr(mod, prop)
|
||||||
|
setattr(mod_target, prop, value)
|
||||||
|
|
||||||
|
# rebind modifiers (corr. smooth, surf. deform, mesh deform)
|
||||||
|
for mod in obj_target.modifiers:
|
||||||
|
if mod.type == 'SURFACE_DEFORM':
|
||||||
|
if not mod.is_bound:
|
||||||
|
continue
|
||||||
|
for i in range(2):
|
||||||
|
bpy.ops.object.surfacedeform_bind(
|
||||||
|
{"object": obj_target, "active_object": obj_target},
|
||||||
|
modifier=mod.name,
|
||||||
|
)
|
||||||
|
elif mod.type == 'MESH_DEFORM':
|
||||||
|
if not mod.is_bound:
|
||||||
|
continue
|
||||||
|
for i in range(2):
|
||||||
|
bpy.ops.object.meshdeform_bind(
|
||||||
|
{"object": obj_target, "active_object": obj_target},
|
||||||
|
modifier=mod.name,
|
||||||
|
)
|
||||||
|
elif mod.type == 'CORRECTIVE_SMOOTH':
|
||||||
|
if not mod.is_bind:
|
||||||
|
continue
|
||||||
|
for i in range(2):
|
||||||
|
bpy.ops.object.correctivesmooth_bind(
|
||||||
|
{"object": obj_target, "active_object": obj_target},
|
||||||
|
modifier=mod.name,
|
||||||
|
)
|
||||||
|
@ -11,7 +11,7 @@ def update_transfer_data_ownership(transfer_data_item, target_obj: bpy.types.Obj
|
|||||||
new_item.name = transfer_data_item.name
|
new_item.name = transfer_data_item.name
|
||||||
new_item.owner = transfer_data_item.owner
|
new_item.owner = transfer_data_item.owner
|
||||||
new_item.type = transfer_data_item.type
|
new_item.type = transfer_data_item.type
|
||||||
new_item.id = bpy.data.objects[target_obj.name]
|
new_item.id = target_obj
|
||||||
|
|
||||||
|
|
||||||
def apply_transfer_data(
|
def apply_transfer_data(
|
||||||
@ -24,32 +24,54 @@ def apply_transfer_data(
|
|||||||
if asset_suffix.get_asset_basename(
|
if asset_suffix.get_asset_basename(
|
||||||
target_obj.name
|
target_obj.name
|
||||||
) == asset_suffix.get_asset_basename(item.id.name):
|
) == asset_suffix.get_asset_basename(item.id.name):
|
||||||
print(f"{target_obj.name}: READY TO TRANSFER BABY!")
|
if item.type == "VERTEX_GROUP":
|
||||||
transfer_functions.transfer_vertex_group(
|
transfer_functions.transfer_vertex_group(
|
||||||
context=context,
|
context=context,
|
||||||
vertex_group_name=item.name,
|
vertex_group_name=item.name,
|
||||||
target_obj=target_obj,
|
target_obj=target_obj,
|
||||||
source_obj=item.id,
|
source_obj=item.id,
|
||||||
)
|
)
|
||||||
|
if item.type == "MODIFIER":
|
||||||
|
transfer_functions.transfer_modifier(item, target_obj)
|
||||||
update_transfer_data_ownership(
|
update_transfer_data_ownership(
|
||||||
transfer_data_item=item,
|
transfer_data_item=item,
|
||||||
target_obj=target_obj,
|
target_obj=target_obj,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def check_transfer_data_entry(ownership, key, td_type):
|
||||||
|
existing_items = [item.name for item in ownership if item.type == td_type]
|
||||||
|
return set([key]).intersection(set(existing_items))
|
||||||
|
|
||||||
|
|
||||||
|
def transfer_data_add_entry(ownership, name, td_type, task_layer_name):
|
||||||
|
obj = ownership.id_data
|
||||||
|
item = ownership.add()
|
||||||
|
item.name = name
|
||||||
|
item.owner = task_layer_name.upper()
|
||||||
|
item.type = td_type
|
||||||
|
item.id = obj
|
||||||
|
|
||||||
|
|
||||||
## FUNCTIONS SPECFIC TO TRANSFER DATA TYPES
|
## FUNCTIONS SPECFIC TO TRANSFER DATA TYPES
|
||||||
|
|
||||||
|
|
||||||
# VERTEX GROUPS
|
# VERTEX GROUPS
|
||||||
def vertex_groups_update(obj, task_layer_name):
|
def vertex_groups_update(obj, task_layer_name):
|
||||||
ownership = obj.transfer_data_ownership
|
ownership = obj.transfer_data_ownership
|
||||||
for vertex_group in obj.vertex_groups:
|
for vertex_group in obj.vertex_groups:
|
||||||
# Only add new ownership item if vertex group doesn't have an owner
|
# Only add new ownership item if vertex group doesn't have an owner
|
||||||
existing_items = [item.name for item in ownership]
|
matches = check_transfer_data_entry(
|
||||||
matches = set([vertex_group.name]).intersection(set(existing_items))
|
ownership, vertex_group.name, "VERTEX_GROUP"
|
||||||
|
)
|
||||||
if len(matches) == 0:
|
if len(matches) == 0:
|
||||||
item = ownership.add()
|
transfer_data_add_entry(
|
||||||
item.name = vertex_group.name
|
ownership, vertex_group.name, "VERTEX_GROUP", task_layer_name
|
||||||
item.owner = task_layer_name.upper()
|
)
|
||||||
item.type = "VERTEX_GROUP"
|
|
||||||
item.id = bpy.data.objects[f"{obj.name.split('.')[-1]}"]
|
|
||||||
|
# MODIFIERS
|
||||||
|
def modifiers_update(obj, task_layer_name):
|
||||||
|
ownership = obj.transfer_data_ownership
|
||||||
|
for mod in obj.modifiers:
|
||||||
|
matches = check_transfer_data_entry(ownership, mod.name, "MODIFIER")
|
||||||
|
if len(matches) == 0:
|
||||||
|
transfer_data_add_entry(ownership, mod.name, "MODIFIER", task_layer_name)
|
||||||
|
Loading…
Reference in New Issue
Block a user