8191c795f4
add apply shapekey to basis to shapekey menu I believe I wrote this simple implementation for Tuxedo, I don't remember how I came up with it.
420 lines
16 KiB
Python
420 lines
16 KiB
Python
import bpy
|
|
import numpy as np
|
|
from .dictionaries import bone_names
|
|
import threading
|
|
import time
|
|
import webbrowser
|
|
import typing
|
|
|
|
from ..core.register import register_wrap
|
|
from typing import List, Optional, Tuple
|
|
from bpy.types import Object, ShapeKey, Mesh, Context, Material, PropertyGroup
|
|
from functools import lru_cache
|
|
from bpy.props import PointerProperty, IntProperty, StringProperty
|
|
from bpy.utils import register_class
|
|
|
|
|
|
|
|
|
|
class SceneMatClass(PropertyGroup):
|
|
mat: PointerProperty(type=Material)
|
|
|
|
register_class(SceneMatClass)
|
|
|
|
class MaterialListBool:
|
|
#For the love that is holy do not ever touch these. If this was java I would make these private
|
|
#They should only be accessed via context.scene.texture_atlas_Has_Mat_List_Shown
|
|
#This is so we know if the materials are up to date. messing with these variables directly will make the thing blow up.
|
|
|
|
#The only exception to this is the ExpandSection_Materials operator which populates this with new data once the materials have changed and need reloading.
|
|
old_list: dict[str,list[Material]] = {}
|
|
bool_material_list_expand: dict[str,bool] = {}
|
|
|
|
def set_bool(self, value: bool) -> None:
|
|
MaterialListBool.bool_material_list_expand[bpy.context.scene.name] = value
|
|
if value == False:
|
|
MaterialListBool.old_list[bpy.context.scene.name] = []
|
|
|
|
def get_bool(self) -> bool:
|
|
newlist: list[Material] = []
|
|
for obj in bpy.context.scene.objects:
|
|
if len(obj.material_slots)>0:
|
|
for mat_slot in obj.material_slots:
|
|
if mat_slot.material:
|
|
if mat_slot.material not in newlist:
|
|
newlist.append(mat_slot.material)
|
|
|
|
still_the_same: bool = True
|
|
if bpy.context.scene.name in MaterialListBool.old_list:
|
|
for item in newlist:
|
|
if item not in MaterialListBool.old_list[bpy.context.scene.name]:
|
|
still_the_same = False
|
|
break
|
|
for item in MaterialListBool.old_list[bpy.context.scene.name]:
|
|
if item not in newlist:
|
|
still_the_same = False
|
|
break
|
|
else:
|
|
still_the_same = False
|
|
MaterialListBool.bool_material_list_expand[bpy.context.scene.name] = still_the_same
|
|
|
|
return MaterialListBool.bool_material_list_expand[bpy.context.scene.name]
|
|
|
|
|
|
### Clean up material names in the given mesh by removing the '.001' suffix.
|
|
def clean_material_names(mesh: Mesh) -> None:
|
|
for j, mat in enumerate(mesh.material_slots):
|
|
if mat.name.endswith(('.0+', ' 0+')):
|
|
mesh.active_material_index = j
|
|
mesh.active_material.name = mat.name[:-len(mat.name.rstrip('0')) - 1]
|
|
|
|
# This will fix faulty uv coordinates, cats did this a other way which can have unintended consequences,
|
|
# this is the best way i could of think of doing this for the time being, however may need improvements.
|
|
|
|
def fix_uv_coordinates(context: Context) -> None:
|
|
obj = context.object
|
|
|
|
# Check if the object is in Edit Mode
|
|
if obj.mode != 'EDIT':
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
|
|
|
# Check if the object has any mesh data
|
|
if obj.type == 'MESH' and obj.data:
|
|
bpy.context.view_layer.objects.active = obj
|
|
bpy.ops.mesh.select_all(action='SELECT')
|
|
bpy.ops.uv.average_islands_scale()
|
|
|
|
# Switch back to Object Mode
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
|
else:
|
|
print("Object is not a valid mesh with UV data")
|
|
|
|
def has_shapekeys(mesh_obj: Object) -> bool:
|
|
return mesh_obj.data.shape_keys is not None
|
|
|
|
@lru_cache(maxsize=None)
|
|
def _get_shape_key_co(shape_key: ShapeKey) -> np.ndarray:
|
|
return np.array([v.co for v in shape_key.data])
|
|
|
|
def simplify_bonename(n: str) -> str:
|
|
return n.lower().translate(dict.fromkeys(map(ord, u" _.")))
|
|
|
|
def get_armature(context: Context, armature_name: Optional[str] = None) -> Optional[Object]:
|
|
if armature_name:
|
|
obj = bpy.data.objects[armature_name]
|
|
if obj.type == "ARMATURE":
|
|
return obj
|
|
else:
|
|
return None
|
|
if context.view_layer.objects.active:
|
|
obj = context.view_layer.objects.active
|
|
if obj.type == "ARMATURE":
|
|
return obj
|
|
return next((obj for obj in context.view_layer.objects if obj.type == 'ARMATURE'), None)
|
|
|
|
def get_armatures(self, context: Context) -> List[Tuple[str, str, str]]:
|
|
return [(obj.name, obj.name, "") for obj in bpy.data.objects if obj.type == 'ARMATURE']
|
|
|
|
def get_armatures_that_are_not_selected(self, context: Context) -> List[Tuple[str, str, str]]:
|
|
return [(obj.name, obj.name, "") for obj in bpy.data.objects if ((obj.type == 'ARMATURE') and (obj.name != context.scene.selected_armature))]
|
|
|
|
def get_selected_armature(context: Context) -> Optional[Object]:
|
|
if context.scene.selected_armature:
|
|
armature = bpy.data.objects.get(context.scene.selected_armature)
|
|
if is_valid_armature(armature):
|
|
return armature
|
|
return None
|
|
|
|
def set_selected_armature(context: Context, armature: Optional[Object]) -> None:
|
|
context.scene.selected_armature = armature.name if armature else ""
|
|
|
|
def is_valid_armature(armature: Object) -> bool:
|
|
if not armature or armature.type != 'ARMATURE':
|
|
return False
|
|
if not armature.data or not armature.data.bones:
|
|
return False
|
|
return True
|
|
|
|
def select_current_armature(context: Context) -> bool:
|
|
armature = get_selected_armature(context)
|
|
if armature:
|
|
bpy.ops.object.select_all(action='DESELECT')
|
|
armature.select_set(True)
|
|
context.view_layer.objects.active = armature
|
|
return True
|
|
return False
|
|
|
|
def apply_shapekey_to_basis(context: bpy.types.Context, obj: bpy.types.Object, shape_key_name: str, delete_old: bool = False) -> bool:
|
|
if shape_key_name not in obj.data.shape_keys.key_blocks:
|
|
return False
|
|
shapekeynum = obj.data.shape_keys.key_blocks.find(shape_key_name)
|
|
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
|
|
bpy.ops.mesh.select_all(action='SELECT')
|
|
|
|
|
|
obj.active_shape_key_index = 0
|
|
bpy.ops.mesh.blend_from_shape(shape = shape_key_name, add=True, blend=1)
|
|
obj.active_shape_key_index = shapekeynum
|
|
bpy.ops.mesh.select_all(action='SELECT')
|
|
bpy.ops.mesh.blend_from_shape(shape = shape_key_name, add=True, blend=-2)
|
|
|
|
|
|
bpy.ops.mesh.select_all(action='DESELECT')
|
|
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
print("blended!")
|
|
|
|
if delete_old:
|
|
obj.active_shape_key_index = shapekeynum
|
|
bpy.ops.object.shape_key_remove(all=False)
|
|
else:
|
|
mesh: bpy.types.Mesh = obj.data
|
|
mesh.shape_keys.key_blocks[shape_key_name].name = shape_key_name + "_reversed"
|
|
return True
|
|
|
|
def apply_pose_as_rest(context: Context, armature_obj: bpy.types.Object, meshes: list[bpy.types.Object]) -> bool:
|
|
for obj in meshes:
|
|
mesh_data: Mesh = obj.data
|
|
|
|
if mesh_data.shape_keys:
|
|
shape_key_obj_list: list[bpy.types.Object] = []
|
|
modifier_armature_name: str = ""
|
|
|
|
for modifier in obj.modifiers:
|
|
if modifier.type == "ARMATURE":
|
|
arm_modifier: bpy.types.ArmatureModifier = modifier
|
|
if not (arm_modifier.object == armature_obj):
|
|
continue
|
|
modifier_armature_name = arm_modifier.object.name
|
|
|
|
if modifier_armature_name == "":
|
|
continue
|
|
for idx,shape in enumerate(mesh_data.shape_keys.key_blocks):
|
|
if idx == 0:
|
|
continue
|
|
context.view_layer.objects.active = obj
|
|
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
bpy.ops.object.select_all(action="DESELECT")
|
|
context.view_layer.objects.active = obj
|
|
obj.select_set(True)
|
|
|
|
#create duplicate of object
|
|
bpy.ops.object.duplicate()
|
|
|
|
shape_obj = context.view_layer.objects.active
|
|
|
|
#make current shapekey a separate object
|
|
shape_obj.active_shape_key_index = idx
|
|
shape_obj.name = shape.name
|
|
|
|
bpy.ops.object.shape_key_move(type="TOP")
|
|
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
|
|
bpy.ops.object.shape_key_remove(all=True)
|
|
|
|
bpy.ops.object.modifier_apply(modifier=modifier_armature_name)
|
|
|
|
#for modifier_name in [i.name for i in shape_obj.modifiers]:
|
|
# bpy.ops.object.modifier_remove(modifier=modifier_name)
|
|
|
|
shape_key_obj_list.append(shape_obj) #add to a list of shape key objects
|
|
context.view_layer.objects.active = obj
|
|
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
context.view_layer.objects.active.select_set(True)
|
|
bpy.ops.object.shape_key_remove(all=True)
|
|
bpy.ops.object.modifier_apply(modifier=modifier_armature_name)
|
|
bpy.ops.object.select_all(action="DESELECT")
|
|
|
|
for shapekey_obj in shape_key_obj_list:
|
|
shapekey_obj.select_set(True)
|
|
context.view_layer.objects.active = obj
|
|
context.view_layer.objects.active.select_set(True)
|
|
|
|
try:
|
|
bpy.ops.object.join_shapes()
|
|
except:
|
|
|
|
#delete shapekey objects to not leave ourselves in a bad exit state - @989onan
|
|
context.view_layer.objects.active = shape_key_obj_list[0]
|
|
obj.select_set(False)
|
|
bpy.ops.object.delete(confirm=False)
|
|
return False
|
|
context.view_layer.objects.active = shape_key_obj_list[0]
|
|
obj.select_set(False)
|
|
bpy.ops.object.delete(confirm=False)
|
|
else:
|
|
modifier_armature_name: str = ""
|
|
|
|
for modifier in obj.modifiers:
|
|
if modifier.type == "ARMATURE":
|
|
arm_modifier: bpy.types.ArmatureModifier = modifier
|
|
if not (arm_modifier.object == armature_obj):
|
|
continue
|
|
modifier_armature_name = arm_modifier.object.name
|
|
|
|
if modifier_armature_name == "":
|
|
continue
|
|
context.view_layer.objects.active = obj
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
bpy.ops.object.select_all(action="DESELECT")
|
|
context.view_layer.objects.active.select_set(True)
|
|
bpy.ops.object.modifier_apply(modifier=modifier_armature_name)
|
|
|
|
context.view_layer.objects.active = armature_obj
|
|
armature_obj.select_set(True)
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
bpy.ops.object.mode_set(mode="POSE")
|
|
|
|
bpy.ops.pose.armature_apply(selected=False)
|
|
return True
|
|
|
|
def get_all_meshes(context: Context) -> List[Object]:
|
|
armature = get_selected_armature(context)
|
|
if armature and is_valid_armature(armature):
|
|
return [obj for obj in bpy.data.objects if obj.type == 'MESH' and obj.parent == armature]
|
|
return []
|
|
|
|
def get_mesh_items(self, context):
|
|
return [(obj.name, obj.name, "") for obj in get_all_meshes(context)]
|
|
|
|
def open_web_after_delay_multi_threaded(delay: typing.Optional[float] = 1.0, url: typing.Union[str, typing.Any] = ""):
|
|
thread = threading.Thread(target=open_web_after_delay,args=[delay,url],name="open_browser_thread")
|
|
thread.start()
|
|
|
|
def open_web_after_delay(delay, url):
|
|
print("opening browser in "+str(delay)+" seconds.")
|
|
time.sleep(delay)
|
|
|
|
webbrowser.open_new_tab(url)
|
|
|
|
def duplicatebone(b: bpy.types.EditBone) -> bpy.types.EditBone:
|
|
arm = bpy.context.object.data
|
|
cb = arm.edit_bones.new(b.name)
|
|
|
|
cb.head = b.head
|
|
cb.tail = b.tail
|
|
cb.matrix = b.matrix
|
|
cb.parent = b.parent
|
|
return cb
|
|
|
|
def has_shapekeys(mesh_obj: Object) -> bool:
|
|
return mesh_obj.data.shape_keys is not None
|
|
|
|
def sort_shape_keys(mesh: Object) -> None:
|
|
print("Starting shape key sorting...")
|
|
if not has_shapekeys(mesh):
|
|
print("No shape keys found. Exiting sort function.")
|
|
return
|
|
|
|
# Set the mesh as the active object
|
|
bpy.context.view_layer.objects.active = mesh
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
|
|
|
order = [
|
|
'Basis',
|
|
'vrc.blink_left',
|
|
'vrc.blink_right',
|
|
'vrc.lowerlid_left',
|
|
'vrc.lowerlid_right',
|
|
'vrc.v_aa',
|
|
'vrc.v_ch',
|
|
'vrc.v_dd',
|
|
'vrc.v_e',
|
|
'vrc.v_ff',
|
|
'vrc.v_ih',
|
|
'vrc.v_kk',
|
|
'vrc.v_nn',
|
|
'vrc.v_oh',
|
|
'vrc.v_ou',
|
|
'vrc.v_pp',
|
|
'vrc.v_rr',
|
|
'vrc.v_sil',
|
|
'vrc.v_ss',
|
|
'vrc.v_th',
|
|
]
|
|
|
|
shape_keys = mesh.data.shape_keys.key_blocks
|
|
print(f"Total shape keys: {len(shape_keys)}")
|
|
|
|
# Create a list of shape key names in their current order
|
|
current_order = [key.name for key in shape_keys]
|
|
|
|
# Create a new order list
|
|
new_order = []
|
|
|
|
# First, add all the keys that are in the predefined order
|
|
for name in order:
|
|
if name in current_order:
|
|
new_order.append(name)
|
|
current_order.remove(name)
|
|
|
|
# Then add any remaining keys that weren't in the predefined order
|
|
new_order.extend(current_order)
|
|
|
|
print("New order:", new_order)
|
|
|
|
# Now, rearrange the shape keys based on the new order
|
|
for i, name in enumerate(new_order):
|
|
index = shape_keys.find(name)
|
|
if index != i:
|
|
print(f"Moving {name} from index {index} to {i}")
|
|
mesh.active_shape_key_index = index
|
|
while mesh.active_shape_key_index > i:
|
|
bpy.ops.object.shape_key_move(type='UP')
|
|
|
|
print("Shape key sorting completed.")
|
|
|
|
def get_shapekeys(mesh: Object, prefix: str = '') -> List[tuple]:
|
|
if not has_shapekeys(mesh):
|
|
return []
|
|
return [(key.name, key.name, key.name) for key in mesh.data.shape_keys.key_blocks if key.name != 'Basis' and key.name.startswith(prefix)]
|
|
|
|
def remove_default_objects():
|
|
for obj in bpy.data.objects:
|
|
if obj.name in ["Camera", "Light", "Cube"]:
|
|
bpy.data.objects.remove(obj, do_unlink=True)
|
|
|
|
def init_progress(context, steps):
|
|
context.window_manager.progress_begin(0, 100)
|
|
context.scene.avatar_toolkit_progress_steps = steps
|
|
context.scene.avatar_toolkit_progress_current = 0
|
|
|
|
def update_progress(self, context, message):
|
|
context.scene.avatar_toolkit_progress_current += 1
|
|
progress = (context.scene.avatar_toolkit_progress_current / context.scene.avatar_toolkit_progress_steps) * 100
|
|
context.window_manager.progress_update(progress)
|
|
context.area.header_text_set(message)
|
|
self.report({'INFO'}, message)
|
|
|
|
def finish_progress(context):
|
|
context.window_manager.progress_end()
|
|
context.area.header_text_set(None)
|
|
|
|
def transfer_vertex_weights(context: Context, obj: bpy.types.Object, source_group: str, target_group: str, delete_source_group: bool = True) -> bool:
|
|
|
|
modifier: bpy.types.VertexWeightMixModifier = obj.modifiers.new(name="merge_weights",type="VERTEX_WEIGHT_MIX")
|
|
|
|
modifier.mix_set = 'B'
|
|
modifier.vertex_group_a = target_group
|
|
modifier.vertex_group_b = source_group
|
|
modifier.mask_constant = 1.0
|
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
|
prev_obj: bpy.types.Object = context.view_layer.objects.active
|
|
context.view_layer.objects.active = obj
|
|
bpy.ops.object.modifier_apply(modifier=modifier.name)
|
|
if delete_source_group:
|
|
obj.vertex_groups.remove(obj.vertex_groups.get(source_group))
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
|
context.view_layer.objects.active = prev_obj
|
|
|
|
return True
|
|
|