Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Apply Groom Modifiers Extension #2

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions send2ue/resources/extensions/apply_groom_modifiers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import bpy
from send2ue.core import utilities
from send2ue.constants import ToolInfo
from send2ue.core.extension import ExtensionBase

# Get or create temp collection (necessary?)
def get_temp_collection(collection_name="GroomTempCollection"):
if collection_name in bpy.data.collections:
temp_collection = bpy.data.collections[collection_name]
else:
temp_collection = bpy.data.collections.new(name=collection_name)
bpy.context.scene.collection.children.link(temp_collection)

return temp_collection

# make copies of original objects, link to temp collection
def apply_groom_modifiers():
properties = bpy.context.scene.send2ue
hair_objects = utilities.get_hair_objects(properties)
temp_collection = get_temp_collection()

for hair_object in hair_objects:
hair_copy = hair_object.copy()
hair_copy.data = hair_object.data.copy()
temp_collection.objects.link(hair_copy)

for modifier in hair_object.modifiers:
bpy.context.view_layer.objects.active = hair_object
print(modifier.name)
if modifier.name != 'Surface Deform':
bpy.ops.object.modifier_apply(modifier=modifier.name)

# Delete modified objects and restore original copies
def restore_groom_modifiers():
temp_collection = get_temp_collection()
export_collection = bpy.data.collections.get(ToolInfo.EXPORT_COLLECTION.value)
hair_copies = temp_collection.all_objects

for hair_copy in hair_copies:
original_name = hair_copy.name.split(".")[0]
modified_hair_object = bpy.data.objects.get(original_name)
if modified_hair_object:
bpy.data.objects.remove(modified_hair_object, do_unlink=True)
export_collection.objects.link(hair_copy)
temp_collection.objects.unlink(hair_copy)
hair_copy.name = original_name

bpy.data.collections.remove(temp_collection)

class ApplyGroomModifiersExtension(ExtensionBase):
name = 'applygroommodifiers'

apply_groom_mods: bpy.props.BoolProperty(
name= "Apply Groom Mods",
default= False,
description="Automatically applies hair modifiers for export "
"Modifiers restored after export finishes."
)

# Draws setting in extensions panel.
def draw_export(self, dialog, layout, properties):
box = layout.box()
box.label(text='Groom Modifiers:')
dialog.draw_property(self, box, 'apply_groom_mods')

def pre_operation(self, properties):
if self.apply_groom_mods:
print('groommods enabled')
apply_groom_modifiers()

def post_operation(self, properties):
if self.apply_groom_mods:
print('groommods post')
restore_groom_modifiers()
3 changes: 3 additions & 0 deletions send2ue/resources/setting_templates/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
"export_custom_property_fcurves": true,
"export_object_name_as_root": true,
"extensions": {
"applygroommodifiers": {
"apply_groom_mods": true
},
"affixes": {
"animation_sequence_name_affix": "Anim_",
"auto_add_asset_name_affixes": false,
Expand Down
3 changes: 3 additions & 0 deletions tests/test_files/send2ue_templates/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
"export_custom_property_fcurves": true,
"export_object_name_as_root": true,
"extensions": {
"applygroommodifiers": {
"apply_groom_mods": true
},
"affixes": {
"animation_sequence_name_affix": "Anim_",
"auto_add_asset_name_affixes": false,
Expand Down
3 changes: 3 additions & 0 deletions tests/test_files/send2ue_templates/template_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
"export_custom_property_fcurves": true,
"export_object_name_as_root": true,
"extensions": {
"applygroommodifiers": {
"apply_groom_mods": true
},
"affixes": {
"animation_sequence_name_affix": "Anim_",
"auto_add_asset_name_affixes": false,
Expand Down