Coverage for lib/lottie/objects/effects.py: 61%

150 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-20 16:17 +0100

1from .base import LottieObject, LottieProp, PseudoBool 

2from .properties import Value, MultiDimensional, ColorValue 

3from ..nvector import NVector 

4from ..utils.color import Color 

5from .helpers import VisualObject 

6 

7 

8## @ingroup Lottie 

9class EffectValue(VisualObject): 

10 """! 

11 Value for an effect 

12 """ 

13 ## %Effect value type. 

14 type = None 

15 _classes = {} 

16 

17 _props = [ 

18 LottieProp("effect_index", "ix", int, False), 

19 LottieProp("type", "ty", int, False), 

20 ] 

21 

22 def __init__(self): 

23 super().__init__() 

24 ## Effect Index. Used for expressions. 

25 self.effect_index = None 

26 

27 @classmethod 

28 def _load_get_class(cls, lottiedict): 

29 if not EffectValue._classes: 

30 EffectValue._classes = { 

31 sc.type: sc 

32 for sc in EffectValue.__subclasses__() 

33 } 

34 return EffectValue._classes.get(lottiedict["ty"], EffectValueSlider) 

35 

36 def __str__(self): 

37 return self.name or super().__str__() 

38 

39 

40## @ingroup Lottie 

41class Effect(VisualObject): 

42 """! 

43 Layer effect 

44 """ 

45 ## %Effect type. 

46 type = None 

47 _classes = {} 

48 

49 _props = [ 

50 LottieProp("effect_index", "ix", int, False), 

51 LottieProp("type", "ty", int, False), 

52 LottieProp("effects", "ef", EffectValue, True), 

53 LottieProp("enabled", "en", bool, False), 

54 ] 

55 _effects = [] 

56 

57 def __init__(self, *args, **kwargs): 

58 super().__init__() 

59 ## Effect Index. Used for expressions. 

60 self.effect_index = None 

61 ## Effect parameters 

62 self.effects = self._load_values(*args, **kwargs) 

63 ## Whether the effect is enabled 

64 self.enabled = None 

65 

66 @classmethod 

67 def _load_get_class(cls, lottiedict): 

68 if not Effect._classes: 

69 Effect._classes = { 

70 sc.type: sc 

71 for sc in Effect.__subclasses__() 

72 } 

73 type = lottiedict["ty"] 

74 

75 if type in Effect._classes: 

76 return Effect._classes[type] 

77 else: 

78 return Effect 

79 

80 def _load_values(self, *args, **kwargs): 

81 values = [] 

82 for i, (name, type) in enumerate(self._effects): 

83 val = [] 

84 if len(args) > i: 

85 val = [args[i]] 

86 if name in kwargs: 

87 val = [kwargs[name]] 

88 values.append(type(*val)) 

89 return values 

90 

91 def __getattr__(self, key): 

92 for i, (name, type) in enumerate(self._effects): 

93 if name == key: 

94 return self.effects[i].value 

95 raise AttributeError(key) 

96 

97 def __str__(self): 

98 return self.name or super().__str__() 

99 

100 

101## @ingroup Lottie 

102## @ingroup LottieCheck 

103class EffectNoValue(EffectValue): 

104 _props = [] 

105 

106 

107## @ingroup Lottie 

108class EffectValueSlider(EffectValue): 

109 _props = [ 

110 LottieProp("value", "v", Value, False), 

111 ] 

112 ## %Effect type. 

113 type = 0 

114 

115 def __init__(self, value=0): 

116 EffectValue.__init__(self) 

117 ## Effect value. 

118 self.value = Value(value) 

119 

120 

121## @ingroup Lottie 

122class EffectValueAngle(EffectValue): 

123 _props = [ 

124 LottieProp("value", "v", Value, False), 

125 ] 

126 ## %Effect type. 

127 type = 1 

128 

129 def __init__(self, angle=0): 

130 EffectValue.__init__(self) 

131 ## Effect value. 

132 self.value = Value(angle) 

133 

134 

135## @ingroup Lottie 

136class EffectValueColor(EffectValue): 

137 _props = [ 

138 LottieProp("value", "v", ColorValue, False), 

139 ] 

140 ## %Effect type. 

141 type = 2 

142 

143 def __init__(self, value=Color(0, 0, 0)): 

144 EffectValue.__init__(self) 

145 ## Effect value. 

146 self.value = ColorValue(value) 

147 

148 

149## @ingroup Lottie 

150class EffectValuePoint(EffectValue): 

151 _props = [ 

152 LottieProp("value", "v", MultiDimensional, False), 

153 ] 

154 ## %Effect type. 

155 type = 3 

156 

157 def __init__(self, value=NVector(0, 0)): 

158 EffectValue.__init__(self) 

159 ## Effect value. 

160 self.value = MultiDimensional(value) 

161 

162 

163## @ingroup Lottie 

164class EffectValueCheckbox(EffectValue): 

165 _props = [ 

166 LottieProp("value", "v", Value, False), 

167 ] 

168 ## %Effect type. 

169 type = 4 

170 

171 def __init__(self, value=0): 

172 EffectValue.__init__(self) 

173 ## Effect value. 

174 self.value = Value(value) 

175 

176 

177## @ingroup Lottie 

178## @ingroup LottieCheck 

179## Lottie-web ignores these 

180class EffectValueIgnored(EffectValue): 

181 _props = [ 

182 LottieProp("value", "v", float, False), 

183 ] 

184 ## %Effect type. 

185 type = 6 

186 

187 def __init__(self, value=0): 

188 EffectValue.__init__(self) 

189 ## Effect value. 

190 self.value = value 

191 

192 

193## @ingroup Lottie 

194## @ingroup LottieCheck 

195class EffectValueDropDown(EffectValue): 

196 _props = [ 

197 LottieProp("value", "v", Value, False), 

198 ] 

199 ## %Effect type. 

200 type = 7 

201 

202 def __init__(self, value=0): 

203 EffectValue.__init__(self) 

204 ## Effect value. 

205 self.value = Value(value) 

206 

207 

208## @ingroup Lottie 

209## @ingroup LottieCheck 

210class EffectValueLayer(EffectValue): 

211 _props = [ 

212 LottieProp("value", "v", Value, False), 

213 ] 

214 ## %Effect type. 

215 type = 10 

216 

217 def __init__(self): 

218 EffectValue.__init__(self) 

219 ## Effect value. 

220 self.value = Value() 

221 

222 

223## @ingroup Lottie 

224class FillEffect(Effect): 

225 """! 

226 Replaces the whole layer with the given color 

227 @note Opacity is in [0, 1] 

228 """ 

229 _effects = [ 

230 ("00", EffectValuePoint), 

231 ("01", EffectValueDropDown), 

232 ("color", EffectValueColor), 

233 ("03", EffectValueDropDown), 

234 ("04", EffectValueSlider), 

235 ("05", EffectValueSlider), 

236 ("opacity", EffectValueSlider), 

237 ] 

238 ## %Effect type. 

239 type = 21 

240 

241 

242## @ingroup Lottie 

243class StrokeEffect(Effect): 

244 _effects = [ 

245 ("00", EffectValueColor), 

246 ("01", EffectValueCheckbox), 

247 ("02", EffectValueCheckbox), 

248 ("color", EffectValueColor), 

249 ("04", EffectValueSlider), 

250 ("05", EffectValueSlider), 

251 ("06", EffectValueSlider), 

252 ("07", EffectValueSlider), 

253 ("08", EffectValueSlider), 

254 ("09", EffectValueDropDown), 

255 ("type", EffectValueDropDown), 

256 ] 

257 ## %Effect type. 

258 type = 22 

259 

260 

261## @ingroup Lottie 

262class TritoneEffect(Effect): 

263 """! 

264 Maps layers colors based on bright/mid/dark colors 

265 """ 

266 _effects = [ 

267 ("bright", EffectValueColor), 

268 ("mid", EffectValueColor), 

269 ("dark", EffectValueColor), 

270 ] 

271 ## %Effect type. 

272 type = 23 

273 

274 

275""" 

276## @ingroup Lottie 

277## @ingroup LottieCheck 

278class GroupEffect(Effect): 

279 _props = [ 

280 LottieProp("enabled", "en", PseudoBool, False), 

281 ] 

282 

283 def __init__(self): 

284 Effect.__init__(self) 

285 ## Enabled AE property value 

286 self.enabled = True 

287""" 

288 

289 

290## @ingroup Lottie 

291## @ingroup LottieCheck 

292class ProLevelsEffect(Effect): 

293 _effects = [ 

294 ("00", EffectValueDropDown), 

295 ("01", EffectNoValue), 

296 ("02", EffectNoValue), 

297 ("comp_inblack", EffectValueSlider), 

298 ("comp_inwhite", EffectValueSlider), 

299 ("comp_gamma", EffectValueSlider), 

300 ("comp_outblack", EffectValueSlider), 

301 ("comp_outwhite", EffectValueSlider), 

302 ("08", EffectNoValue), 

303 ("09", EffectValueSlider), 

304 ("r_inblack", EffectValueSlider), 

305 ("r_inwhite", EffectValueSlider), 

306 ("r_gamma", EffectValueSlider), 

307 ("r_outblack", EffectValueSlider), 

308 ("r_outwhite", EffectValueSlider), 

309 ("15", EffectValueSlider), 

310 ("16", EffectValueSlider), 

311 ("g_inblack", EffectValueSlider), 

312 ("g_inwhite", EffectValueSlider), 

313 ("g_gamma", EffectValueSlider), 

314 ("g_outblack", EffectValueSlider), 

315 ("g_outwhite", EffectNoValue), 

316 ("22", EffectValueSlider), 

317 ("23", EffectValueSlider), 

318 ("b_inblack", EffectValueSlider), 

319 ("b_inwhite", EffectValueSlider), 

320 ("b_gamma", EffectValueSlider), 

321 ("b_outblack", EffectValueSlider), 

322 ("b_outwhite", EffectNoValue), 

323 ("29", EffectValueSlider), 

324 ("a_inblack", EffectValueSlider), 

325 ("a_inwhite", EffectValueSlider), 

326 ("a_gamma", EffectValueSlider), 

327 ("a_outblack", EffectValueSlider), 

328 ("a_outwhite", EffectNoValue), 

329 ] 

330 ## %Effect type. 

331 type = 24 

332 

333 

334## @ingroup Lottie 

335class TintEffect(Effect): 

336 """! 

337 Colorizes the layer 

338 @note Opacity is in [0, 100] 

339 """ 

340 _effects = [ 

341 ("color_black", EffectValueColor), 

342 ("color_white", EffectValueColor), 

343 ("opacity", EffectValueSlider), 

344 ] 

345 ## %Effect type. 

346 type = 20 

347 

348 

349## @ingroup Lottie 

350class DropShadowEffect(Effect): 

351 """! 

352 Adds a shadow to the layer 

353 @note Opacity is in [0, 255] 

354 """ 

355 _effects = [ 

356 ("color", EffectValueColor), 

357 ("opacity", EffectValueSlider), 

358 ("angle", EffectValueAngle), 

359 ("distance", EffectValueSlider), 

360 ("blur", EffectValueSlider), 

361 ] 

362 ## %Effect type. 

363 type = 25 

364 

365 

366## @ingroup Lottie 

367## @ingroup LottieCheck 

368class Matte3Effect(Effect): 

369 _effects = [ 

370 ("index", EffectValueSlider), 

371 ] 

372 ## %Effect type. 

373 type = 28 

374 

375 

376## @ingroup Lottie 

377class GaussianBlurEffect(Effect): 

378 """! 

379 Gaussian blur 

380 """ 

381 _effects = [ 

382 ("sigma", EffectValueSlider), 

383 ("dimensions", EffectValueSlider), 

384 ("wrap", EffectValueCheckbox), 

385 ] 

386 ## %Effect type. 

387 type = 29 

388 

389 

390## @ingroup Lottie 

391class CustomEffect(Effect): 

392 """! 

393 Grouing properties in custom effects 

394 """ 

395 _effects = [] 

396 ## %Effect type. 

397 type = 5 

398 

399 

400#ingroup Lottie 

401class MeshWarpEffect(Effect): 

402 _effects = [ 

403 ("Rows", EffectValueSlider), 

404 ("Columns", EffectValueSlider), 

405 ("Quality", EffectValueSlider), 

406 ("03", EffectNoValue), 

407 ] 

408 ## %Effect type. 

409 type = 31 

410 

411 

412#ingroup Lottie 

413class DisplacementMapEffect(Effect): 

414 _effects = [ 

415 ("Displacement Map Layer", EffectValueLayer), 

416 ("Use For Horizontal Displacement", EffectValueDropDown), 

417 ("Max Horizontal Displacement", EffectValueSlider), 

418 ("Use For Vertical Displacement", EffectValueDropDown), 

419 ("Max Vertical Displacement", EffectValueSlider), 

420 ("Displacement Map Behavior", EffectValueDropDown), 

421 ("Edge Behavior", EffectValueDropDown), 

422 ("Expand Output", EffectValueDropDown), 

423 ] 

424 ## %Effect type. 

425 type = 27 

426 

427 

428#ingroup Lottie 

429class PaintOverTransparentEffect(Effect): 

430 _effects = [ 

431 ("00", EffectValueSlider) 

432 ] 

433 ## %Effect type. 

434 type = 7 

435 

436 

437#ingroup Lottie 

438class PuppetEffect(Effect): 

439 _effects = [ 

440 ("Puppet Engine", EffectValueDropDown), 

441 ("Mesh Rotation Refinement", EffectValueSlider), 

442 ("On Transparent", EffectValueDropDown), 

443 ("03", EffectNoValue), 

444 ] 

445 ## %Effect type. 

446 type = 34 

447 

448 

449#ingroup Lottie 

450class RadialWipeEffect(Effect): 

451 _effects = [ 

452 ("Completion", EffectValueSlider), 

453 ("Start Angle", EffectValueAngle), 

454 ("Wipe Center", EffectValuePoint), 

455 ("Wipe", EffectValueSlider), 

456 ("Feather", EffectValueSlider), 

457 ] 

458 ## %Effect type. 

459 type = 26 

460 

461 

462#ingroup Lottie 

463class SpherizeEffect(Effect): 

464 _effects = [ 

465 ("radius", EffectValueSlider), 

466 ("center", EffectValuePoint), 

467 ] 

468 ## %Effect type. 

469 effect_type = 33 

470 

471 

472#ingroup Lottie 

473class WavyEffect(Effect): 

474 _effects = [ 

475 ("Radius", EffectValueSlider), 

476 ("Center", EffectValuePoint), 

477 ("Conversion type", EffectValueDropDown), 

478 ("Speed", EffectValueDropDown), 

479 ("Width", EffectValueSlider), 

480 ("Height", EffectValueSlider), 

481 ("Phase", EffectValueSlider), 

482 ] 

483 ## %Effect type. 

484 type = 32