Last active
June 6, 2020 11:02
-
-
Save nobbyfix/f170695ed3aa9c8bda028131b7accb7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import unitypack | |
| from PIL import Image | |
| def getFaceTransform(assetbundle_path): | |
| with open(assetbundle_path, 'rb') as ab: | |
| bundle = unitypack.load(ab) | |
| for asset in bundle.assets: | |
| for obj in asset.objects.values(): | |
| if obj.type != 'GameObject': continue | |
| objdata = obj.read() | |
| if objdata.name != 'face': continue | |
| component = next(objdata.component.iter(), None) | |
| if component == None: continue | |
| pointer = component['component'] | |
| return pointer.resolve() | |
| def getExpressionOffset(face_transform): | |
| father_transform = face_transform['m_Father'].resolve() | |
| sx, sy = father_transform['m_SizeDelta'].values() | |
| esx, esy = face_transform['m_SizeDelta'].values() | |
| ax, ay = face_transform['m_AnchorMin'].values() | |
| posx, posy = face_transform['m_AnchoredPosition'].values() | |
| px, py = face_transform['m_Pivot'].values() | |
| ox = sx*ax+posx-(px*esx) | |
| oy = sy*ay-posy-(py*esy)-1 | |
| return ox, oy | |
| def addExpression(assetbundle, painting, expression, dest): | |
| """ | |
| Pastes an expression of a ship onto the painting image. | |
| The assetbundle must the one not ending with '_tex' and the | |
| painting has to be already reconstructed. | |
| :param assetbundle: path to the painting assetbundle | |
| :param painting: path to the ship painting | |
| :param expression: path to the image of the expression | |
| :param dest: the destination to save the result to | |
| """ | |
| recttransform = getFaceTransform(assetbundle) | |
| offsetx, offsety = getExpressionOffset(recttransform) | |
| painting = Image.open(painting) | |
| expression = Image.open(expression) | |
| painting.paste(expression, (int(offsetx), int(offsety))) | |
| painting.save(dest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment