|
15 | 15 | logger = getLogger("UnityPyLive2DExtractor") |
16 | 16 |
|
17 | 17 | import UnityPyLive2DExtractor.generated as generated |
18 | | -import importlib |
| 18 | + |
| 19 | +from .generated.Live2D.Cubism.Framework.Physics import ( |
| 20 | + CubismPhysicsNormalizationTuplet, |
| 21 | + CubismPhysicsNormalization, |
| 22 | + CubismPhysicsParticle, |
| 23 | + CubismPhysicsOutput, |
| 24 | + CubismPhysicsInput, |
| 25 | + CubismPhysicsSubRig, |
| 26 | + CubismPhysicsRig, |
| 27 | + CubismPhysicsController, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +def monkey_patch(cls): |
| 32 | + """ooh ooh aah aah""" |
| 33 | + |
| 34 | + def wrapper(func): |
| 35 | + setattr(cls, func.__name__, func) |
| 36 | + return func |
| 37 | + |
| 38 | + return wrapper |
| 39 | + |
| 40 | + |
| 41 | +@monkey_patch(CubismPhysicsNormalizationTuplet) |
| 42 | +def dump(self: CubismPhysicsNormalizationTuplet): |
| 43 | + return { |
| 44 | + "Maximum": self.Maximum, |
| 45 | + "Minimum": self.Minimum, |
| 46 | + "Default": self.Default, |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +@monkey_patch(CubismPhysicsNormalization) |
| 51 | +def dump(self: CubismPhysicsNormalization): |
| 52 | + return {"Position": self.Position.dump(), "Angle": self.Angle.dump()} |
| 53 | + |
| 54 | + |
| 55 | +@monkey_patch(CubismPhysicsParticle) |
| 56 | +def dump(self: CubismPhysicsParticle): |
| 57 | + return { |
| 58 | + "Position": {"X": self.InitialPosition.x, "Y": self.InitialPosition.y}, |
| 59 | + "Mobility": self.Mobility, |
| 60 | + "Delay": self.Delay, |
| 61 | + "Acceleration": self.Acceleration, |
| 62 | + "Radius": self.Radius, |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | +@monkey_patch(CubismPhysicsOutput) |
| 67 | +def dump(self: CubismPhysicsOutput): |
| 68 | + return { |
| 69 | + "Destination": {"Target": "Parameter", "Id": self.DestinationId}, |
| 70 | + "VertexIndex": self.ParticleIndex, |
| 71 | + "Scale": self.AngleScale, |
| 72 | + "Weight": self.Weight, |
| 73 | + "Type": ["X", "Y", "Angle"][self.SourceComponent], |
| 74 | + "Reflect": self.IsInverted, |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +@monkey_patch(CubismPhysicsInput) |
| 79 | +def dump(self: CubismPhysicsInput): |
| 80 | + return { |
| 81 | + "Source": {"Target": "Parameter", "Id": self.SourceId}, |
| 82 | + "Weight": self.Weight, |
| 83 | + "Type": ["X", "Y", "Angle"][self.SourceComponent], |
| 84 | + "Reflect": self.IsInverted, |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | +@monkey_patch(CubismPhysicsSubRig) |
| 89 | +def dump(self: CubismPhysicsSubRig): |
| 90 | + return { |
| 91 | + "Input": [x.dump() for x in self.Input], |
| 92 | + "Output": [x.dump() for x in self.Output], |
| 93 | + "Vertices": [x.dump() for x in self.Particles], |
| 94 | + "Normalization": self.Normalization.dump(), |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | +@monkey_patch(CubismPhysicsRig) |
| 99 | +def dump(self: CubismPhysicsRig): |
| 100 | + return [ |
| 101 | + {"Id": "PhysicsSetting%d" % (i + 1), **rig.dump()} |
| 102 | + for i, rig in enumerate(self.SubRigs) |
| 103 | + ] |
| 104 | + |
| 105 | + |
| 106 | +@monkey_patch(CubismPhysicsController) |
| 107 | +def dump(self: CubismPhysicsController): |
| 108 | + return { |
| 109 | + "Version": 3, |
| 110 | + "Meta": { |
| 111 | + "PhysicsSettingCount": len(self.Rig.SubRigs), |
| 112 | + "TotalInputCount": sum((len(x.Input) for x in self.Rig.SubRigs)), |
| 113 | + "TotalOutputCount": sum((len(x.Output) for x in self.Rig.SubRigs)), |
| 114 | + "VertexCount": sum((len(x.Particles) for x in self.Rig.SubRigs)), |
| 115 | + "Fps": 60, |
| 116 | + "EffectiveForces": { |
| 117 | + "Gravity": {"X": 0, "Y": -1}, |
| 118 | + "Wind": {"X": 0, "Y": 0}, |
| 119 | + }, |
| 120 | + "PhysicsDictionary": [ |
| 121 | + {"Id": "PhysicsSetting%d" % (i + 1), "Name": "%d" % (i + 1)} |
| 122 | + for i, _ in enumerate(self.Rig.SubRigs) |
| 123 | + ], |
| 124 | + }, |
| 125 | + "PhysicsSettings": self.Rig.dump(), |
| 126 | + } |
19 | 127 |
|
20 | 128 |
|
21 | 129 | def read_from(reader: ObjectReader, **kwargs): |
22 | 130 | """Import generated classes by MonoBehavior script class type and read from reader""" |
| 131 | + import importlib |
| 132 | + |
23 | 133 | match reader.type: |
24 | 134 | case ClassIDType.MonoBehaviour: |
25 | 135 | mono: MonoBehaviour = reader.read(check_read=False) |
|
0 commit comments