forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OfxImageEffectInstance.cpp
1504 lines (1301 loc) · 58.4 KB
/
OfxImageEffectInstance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Natron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "OfxImageEffectInstance.h"
#include <cassert>
#include <cstdarg>
#include <memory>
#include <string>
#include <map>
#include <locale>
#include <stdexcept>
#include <cstring> // for std::memcpy, std::memset, std::strcmp
CLANG_DIAG_OFF(deprecated)
CLANG_DIAG_OFF(uninitialized)
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
CLANG_DIAG_ON(deprecated)
CLANG_DIAG_ON(uninitialized)
//ofx extension
#include <nuke/fnPublicOfxExtensions.h>
//for parametric params properties
#include <ofxParametricParam.h>
#include <ofxNatron.h>
#include <ofxhUtilities.h> // for StatStr
#include <ofxhPluginCache.h>
#include "Engine/AppInstance.h"
#include "Engine/AppManager.h"
#include "Engine/Format.h"
#include "Engine/Knob.h"
#include "Engine/KnobFactory.h"
#include "Engine/KnobTypes.h"
#include "Engine/MemoryInfo.h" // printAsRAM
#include "Engine/Node.h"
#include "Engine/NodeMetadata.h"
#include "Engine/OfxClipInstance.h"
#include "Engine/OfxEffectInstance.h"
#include "Engine/OfxMemory.h"
#include "Engine/OfxOverlayInteract.h"
#include "Engine/OfxParamInstance.h"
#include "Engine/Project.h"
#include "Engine/TimeLine.h"
#include "Engine/ViewIdx.h"
#include "Engine/ViewerInstance.h"
#ifdef DEBUG
#include "Global/FloatingPointExceptions.h"
#endif
NATRON_NAMESPACE_ENTER
// see second answer of http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
static
std::string
string_format(const std::string fmt,
...)
{
int size = ( (int)fmt.size() ) * 2 + 50; // Use a rubric appropriate for your code
std::string str;
va_list ap;
while (1) { // Maximum two passes on a POSIX system...
str.resize(size);
va_start(ap, fmt);
int n = vsnprintf( (char *)str.data(), size, fmt.c_str(), ap );
va_end(ap);
if ( (n > -1) && (n < size) ) { // Everything worked
str.resize(n);
return str;
}
if (n > -1) { // Needed size returned
size = n + 1; // For null char
} else {
size *= 2; // Guess at a larger size (OS specific)
}
}
return str;
}
OfxImageEffectInstance::OfxImageEffectInstance(OFX::Host::ImageEffect::ImageEffectPlugin* plugin,
OFX::Host::ImageEffect::Descriptor & desc,
const std::string & context,
bool interactive)
: OFX::Host::ImageEffect::Instance(plugin, desc, context, interactive)
, _ofxEffectInstance()
{
getProps().setGetHook(kNatronOfxExtraCreatedPlanes, (OFX::Host::Property::GetHook*)this);
}
OfxImageEffectInstance::OfxImageEffectInstance(const OfxImageEffectInstance& other)
: OFX::Host::ImageEffect::Instance(other)
, _ofxEffectInstance()
{
}
OfxImageEffectInstance::~OfxImageEffectInstance()
{
}
class ThreadIsActionCaller_RAII
{
OfxImageEffectInstance* _self;
public:
ThreadIsActionCaller_RAII(OfxImageEffectInstance* self)
: _self(self)
{
appPTR->setThreadAsActionCaller(_self, true);
}
~ThreadIsActionCaller_RAII()
{
appPTR->setThreadAsActionCaller(_self, false);
}
};
OfxStatus
OfxImageEffectInstance::mainEntry(const char *action,
const void *handle,
OFX::Host::Property::Set *inArgs,
OFX::Host::Property::Set *outArgs)
{
#ifdef DEBUG
boost_adaptbx::floating_point::exception_trapping trap(0);
#endif
ThreadIsActionCaller_RAII t(this);
return OFX::Host::ImageEffect::Instance::mainEntry(action, handle, inArgs, outArgs);
}
OfxStatus
OfxImageEffectInstance::createInstanceAction()
{
///Overridden because the call to setDefaultClipPreferences is done in Natron
# ifdef OFX_DEBUG_ACTIONS
std::cout << "OFX: " << (void*)this << "->" << kOfxActionCreateInstance << "()" << std::endl;
# endif
// now tell the plug-in to create instance
OfxStatus st = mainEntry(kOfxActionCreateInstance, this->getHandle(), 0, 0);
# ifdef OFX_DEBUG_ACTIONS
std::cout << "OFX: " << (void*)this << "->" << kOfxActionCreateInstance << "()->" << StatStr(st) << std::endl;
# endif
if (st == kOfxStatOK) {
_created = true;
}
return st;
}
const std::string &
OfxImageEffectInstance::getDefaultOutputFielding() const
{
static const std::string v(kOfxImageFieldNone);
return v;
}
/// make a clip
OFX::Host::ImageEffect::ClipInstance*
OfxImageEffectInstance::newClipInstance(OFX::Host::ImageEffect::Instance* plugin,
OFX::Host::ImageEffect::ClipDescriptor* descriptor,
int index)
{
Q_UNUSED(plugin);
return new OfxClipInstance(getOfxEffectInstance(), this, index, descriptor);
}
OfxStatus
OfxImageEffectInstance::setPersistentMessage(const char* type,
const char* /*id*/,
const char* format,
va_list args)
{
assert(type);
assert(format);
std::string message = string_format(format, args);
OfxEffectInstancePtr effect = _ofxEffectInstance.lock();
assert(effect);
if (effect) {
if (std::strcmp(type, kOfxMessageError) == 0) {
effect->setPersistentMessage(eMessageTypeError, message);
} else if (std::strcmp(type, kOfxMessageWarning) == 0) {
effect->setPersistentMessage(eMessageTypeWarning, message);
} else if (std::strcmp(type, kOfxMessageMessage) == 0) {
effect->setPersistentMessage(eMessageTypeInfo, message);
}
}
return kOfxStatOK;
}
OfxStatus
OfxImageEffectInstance::clearPersistentMessage()
{
OfxEffectInstancePtr effect = _ofxEffectInstance.lock();
if (!effect) {
return kOfxStatFailed;
}
effect->clearPersistentMessage(false);
return kOfxStatOK;
}
OfxStatus
OfxImageEffectInstance::vmessage(const char* msgtype,
const char* /*id*/,
const char* format,
va_list args)
{
assert(msgtype);
assert(format);
std::string message = string_format(format, args);
std::string type(msgtype);
OfxEffectInstancePtr effect = _ofxEffectInstance.lock();
if (!effect) {
return kOfxStatFailed;
}
if (type == kOfxMessageLog) {
LogEntry::LogEntryColor c;
if (effect->getNode()->getColor(&c.r, &c.g, &c.b)) {
c.colorSet = true;
}
appPTR->writeToErrorLog_mt_safe(QString::fromUtf8( effect->getNode()->getLabel().c_str() ), QDateTime::currentDateTime(),
QString::fromUtf8( message.c_str() ), false, c);
} else if ( (type == kOfxMessageFatal) || (type == kOfxMessageError) ) {
effect->message(eMessageTypeError, message);
} else if (type == kOfxMessageWarning) {
effect->message(eMessageTypeWarning, message);
} else if (type == kOfxMessageMessage) {
effect->message(eMessageTypeInfo, message);
} else if (type == kOfxMessageQuestion) {
if ( effect->message(eMessageTypeQuestion, message) ) {
return kOfxStatReplyYes;
} else {
return kOfxStatReplyNo;
}
}
return kOfxStatReplyDefault;
}
const std::vector<std::string>&
OfxImageEffectInstance::getUserCreatedPlanes() const
{
OfxEffectInstancePtr effect = _ofxEffectInstance.lock();
const std::vector<std::string>& planes = effect->getUserPlanes();
return planes;
}
int
OfxImageEffectInstance::getDimension(const std::string &name) const OFX_EXCEPTION_SPEC
{
OfxEffectInstancePtr effect = _ofxEffectInstance.lock();
if (!effect) {
return 0;
}
if (name != kNatronOfxExtraCreatedPlanes) {
return OFX::Host::ImageEffect::Instance::getDimension(name);
}
try {
const std::vector<std::string>& planes = effect->getUserPlanes();
return (int)planes.size();
} catch (...) {
throw OFX::Host::Property::Exception(kOfxStatErrUnknown);
}
}
// The size of the current project in canonical coordinates.
// The size of a project is a sub set of the kOfxImageEffectPropProjectExtent. For example a
// project may be a PAL SD project, but only be a letter-box within that. The project size is
// the size of this sub window.
void
OfxImageEffectInstance::getProjectSize(double & xSize,
double & ySize) const
{
Format f;
_ofxEffectInstance.lock()->getApp()->getProject()->getProjectDefaultFormat(&f);
RectI pixelF;
pixelF.x1 = f.x1;
pixelF.x2 = f.x2;
pixelF.y1 = f.y1;
pixelF.y2 = f.y2;
RectD canonicalF;
pixelF.toCanonical_noClipping(0, f.getPixelAspectRatio(), &canonicalF);
xSize = canonicalF.width();
ySize = canonicalF.height();
}
// The offset of the current project in canonical coordinates.
// The offset is related to the kOfxImageEffectPropProjectSize and is the offset from the origin
// of the project 'subwindow'. For example for a PAL SD project that is in letterbox form, the
// project offset is the offset to the bottom left hand corner of the letter box. The project
// offset is in canonical coordinates.
void
OfxImageEffectInstance::getProjectOffset(double & xOffset,
double & yOffset) const
{
Format f;
_ofxEffectInstance.lock()->getApp()->getProject()->getProjectDefaultFormat(&f);
RectI pixelF;
pixelF.x1 = f.x1;
pixelF.x2 = f.x2;
pixelF.y1 = f.y1;
pixelF.y2 = f.y2;
RectD canonicalF;
pixelF.toCanonical_noClipping(0, f.getPixelAspectRatio(), &canonicalF);
xOffset = canonicalF.left();
yOffset = canonicalF.bottom();
}
// The extent of the current project in canonical coordinates.
// The extent is the size of the 'output' for the current project. See ProjectCoordinateSystems
// for more information on the project extent. The extent is in canonical coordinates and only
// returns the top right position, as the extent is always rooted at 0,0. For example a PAL SD
// project would have an extent of 768, 576.
void
OfxImageEffectInstance::getProjectExtent(double & xSize,
double & ySize) const
{
Format f;
_ofxEffectInstance.lock()->getApp()->getProject()->getProjectDefaultFormat(&f);
RectI pixelF;
pixelF.x1 = f.x1;
pixelF.x2 = f.x2;
pixelF.y1 = f.y1;
pixelF.y2 = f.y2;
RectD canonicalF;
pixelF.toCanonical_noClipping(0, f.getPixelAspectRatio(), &canonicalF);
xSize = canonicalF.right();
ySize = canonicalF.top();
}
// The pixel aspect ratio of the current project
double
OfxImageEffectInstance::getProjectPixelAspectRatio() const
{
assert( _ofxEffectInstance.lock() );
Format f;
_ofxEffectInstance.lock()->getApp()->getProject()->getProjectDefaultFormat(&f);
return f.getPixelAspectRatio();
}
// The duration of the effect
// This contains the duration of the plug-in effect, in frames.
double
OfxImageEffectInstance::getEffectDuration() const
{
assert( getOfxEffectInstance() );
NodePtr node = getOfxEffectInstance()->getNode();
if (!node) {
return 0;
}
int firstFrame, lastFrame;
bool lifetimeEnabled = node->isLifetimeActivated(&firstFrame, &lastFrame);
if (lifetimeEnabled) {
return std::max(double(lastFrame - firstFrame) + 1., 1.);
} else {
// return the project duration if the effect has no lifetime
double projFirstFrame, projLastFrame;
node->getApp()->getProject()->getFrameRange(&projFirstFrame, &projLastFrame);
return std::max(projLastFrame - projFirstFrame + 1., 1.);
}
}
// For an instance, this is the frame rate of the project the effect is in.
double
OfxImageEffectInstance::getFrameRate() const
{
assert( getOfxEffectInstance() && getOfxEffectInstance()->getApp() );
return getOfxEffectInstance()->getApp()->getProjectFrameRate();
}
/// This is called whenever a param is changed by the plugin so that
/// the recursive instanceChangedAction will be fed the correct frame
double
OfxImageEffectInstance::getFrameRecursive() const
{
assert( getOfxEffectInstance() );
return getOfxEffectInstance()->getCurrentTime();
}
/// This is called whenever a param is changed by the plugin so that
/// the recursive instanceChangedAction will be fed the correct
/// renderScale
void
OfxImageEffectInstance::getRenderScaleRecursive(double &x,
double &y) const
{
assert( getOfxEffectInstance() );
std::list<ViewerInstance*> attachedViewers;
getOfxEffectInstance()->getNode()->hasViewersConnected(&attachedViewers);
///get the render scale of the 1st viewer
if ( !attachedViewers.empty() ) {
ViewerInstance* first = attachedViewers.front();
int mipMapLevel = first->getMipMapLevel();
x = Image::getScaleFromMipMapLevel( (unsigned int)mipMapLevel );
y = x;
} else {
x = 1.;
y = 1.;
}
}
OfxStatus
OfxImageEffectInstance::getViewCount(int *nViews) const
{
*nViews = getOfxEffectInstance()->getApp()->getProject()->getProjectViewsCount();
return kOfxStatOK;
}
// overridden from OFX::Host::ImageEffect::Instance
OfxStatus
OfxImageEffectInstance::getViewName(int viewIndex,
const char** name) const
{
const std::vector<std::string>& views = getOfxEffectInstance()->getApp()->getProject()->getProjectViewNames();
if ( (viewIndex >= 0) && ( viewIndex < (int)views.size() ) ) {
*name = views[viewIndex].data();
return kOfxStatOK;
}
static const std::string emptyViewName;
*name = emptyViewName.data();
return kOfxStatErrBadIndex;
}
const OFX::Host::Property::PropSpec*
OfxImageEffectInstance::getOfxParamOverlayInteractDescProps()
{
///These props are properties of the PARAMETER descriptor but the describe function of the INTERACT descriptor
///expects those properties to exist, so we add them to the INTERACT descriptor.
static const OFX::Host::Property::PropSpec interactDescProps[] = {
{ kOfxParamPropInteractSize, OFX::Host::Property::eInt, 2, true, "0" },
{ kOfxParamPropInteractSizeAspect, OFX::Host::Property::eDouble, 1, false, "1" },
{ kOfxParamPropInteractMinimumSize, OFX::Host::Property::eDouble, 2, false, "10" },
{ kOfxParamPropInteractPreferedSize, OFX::Host::Property::eInt, 2, false, "10" },
OFX::Host::Property::propSpecEnd
};
return interactDescProps;
}
// make a parameter instance
OFX::Host::Param::Instance *
OfxImageEffectInstance::newParam(const std::string ¶mName,
OFX::Host::Param::Descriptor &descriptor)
{
// note: the order for parameter types is the same as in ofxParam.h
OFX::Host::Param::Instance* instance = NULL;
KnobIPtr knob;
bool paramShouldBePersistent = true;
bool secretByDefault = descriptor.getSecret();
bool enabledByDefault = descriptor.getEnabled();
std::string paramType = descriptor.getType();
bool isToggableButton = false;
if (paramType == kOfxParamTypeBoolean) {
isToggableButton = (bool)descriptor.getProperties().getIntProperty(kNatronOfxBooleanParamPropIsToggableButton);
if (isToggableButton) {
paramType = kOfxParamTypePushButton;
}
}
if (paramType == kOfxParamTypeInteger) {
OfxIntegerInstance *ret = new OfxIntegerInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeDouble) {
OfxDoubleInstance *ret = new OfxDoubleInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeBoolean) {
OfxBooleanInstance *ret = new OfxBooleanInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeChoice) {
OfxChoiceInstance *ret = new OfxChoiceInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeRGBA) {
OfxRGBAInstance *ret = new OfxRGBAInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeRGB) {
OfxRGBInstance *ret = new OfxRGBInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeDouble2D) {
OfxDouble2DInstance *ret = new OfxDouble2DInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeInteger2D) {
OfxInteger2DInstance *ret = new OfxInteger2DInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeDouble3D) {
OfxDouble3DInstance *ret = new OfxDouble3DInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeInteger3D) {
OfxInteger3DInstance *ret = new OfxInteger3DInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeString) {
OfxStringInstance *ret = new OfxStringInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeCustom) {
/*
http://openfx.sourceforge.net/Documentation/1.3/ofxProgrammingReference.html#kOfxParamTypeCustom
Custom parameters contain null terminated char * C strings, and may animate. They are designed to provide plugins with a way of storing data that is too complicated or impossible to store in a set of ordinary parameters.
If a custom parameter animates, it must set its kOfxParamPropCustomInterpCallbackV1 property, which points to a OfxCustomParamInterpFuncV1 function. This function is used to interpolate keyframes in custom params.
Custom parameters have no interface by default. However,
* if they animate, the host's animation sheet/editor should present a keyframe/curve representation to allow positioning of keys and control of interpolation. The 'normal' (ie: paged or hierarchical) interface should not show any gui.
* if the custom param sets its kOfxParamPropInteractV1 property, this should be used by the host in any normal (ie: paged or hierarchical) interface for the parameter.
Custom parameters are mandatory, as they are simply ASCII C strings. However, animation of custom parameters an support for an in editor interact is optional.
*/
//throw std::runtime_error(std::string("Parameter ") + paramName + " has unsupported OFX type " + paramType);
secretByDefault = true;
enabledByDefault = false;
OfxCustomInstance *ret = new OfxCustomInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
instance = ret;
} else if (paramType == kOfxParamTypeGroup) {
OfxGroupInstance *ret = new OfxGroupInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
KnobGroup* isGroup = dynamic_cast<KnobGroup*>(knob.get());
assert(isGroup);
if (isGroup) {
bool haveShortcut = (bool)descriptor.getProperties().getIntProperty(kNatronOfxParamPropInViewerContextCanHaveShortcut);
isGroup->setInViewerContextCanHaveShortcut(haveShortcut);
bool isInToolbar = (bool)descriptor.getProperties().getIntProperty(kNatronOfxParamPropInViewerContextIsInToolbar);
if (isInToolbar) {
isGroup->setAsToolButton(true);
} else {
bool isDialog = (bool)descriptor.getProperties().getIntProperty(kNatronOfxGroupParamPropIsDialog);
if (isDialog) {
isGroup->setAsDialog(true);
}
}
}
instance = ret;
paramShouldBePersistent = false;
} else if (paramType == kOfxParamTypePage) {
OfxPageInstance* ret = new OfxPageInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
#ifdef DEBUG_PAGE
qDebug() << "Page " << descriptor.getName().c_str() << " has children:";
int nChildren = ret->getProperties().getDimension(kOfxParamPropPageChild);
for (int i = 0; i < nChildren; ++i) {
qDebug() << "- " << ret->getProperties().getStringProperty(kOfxParamPropPageChild, i).c_str();
}
#endif
KnobPage* isPage = dynamic_cast<KnobPage*>(knob.get());
assert(isPage);
if (isPage) {
bool isInToolbar = (bool)descriptor.getProperties().getIntProperty(kNatronOfxParamPropInViewerContextIsInToolbar);
if (isInToolbar) {
isPage->setAsToolBar(true);
}
}
instance = ret;
paramShouldBePersistent = false;
} else if (paramType == kOfxParamTypePushButton) {
OfxPushButtonInstance *ret = new OfxPushButtonInstance(getOfxEffectInstance(), descriptor);
knob = ret->getKnob();
if (isToggableButton) {
KnobButton* isBtn = dynamic_cast<KnobButton*>(knob.get());
assert(isBtn);
if (isBtn) {
isBtn->setCheckable(true);
int def = descriptor.getProperties().getIntProperty(kOfxParamPropDefault);
isBtn->setValue((bool)def);
bool haveShortcut = (bool)descriptor.getProperties().getIntProperty(kNatronOfxParamPropInViewerContextCanHaveShortcut);
isBtn->setInViewerContextCanHaveShortcut(haveShortcut);
}
}
instance = ret;
//paramShouldBePersistent = false;
} else if (paramType == kOfxParamTypeParametric) {
OfxParametricInstance* ret = new OfxParametricInstance(getOfxEffectInstance(), descriptor);
OfxStatus stat = ret->defaultInitializeAllCurves(descriptor);
if (stat == kOfxStatFailed) {
throw std::runtime_error("The parameter failed to create curves from their default\n"
"initialized by the plug-in.");
}
ret->onCurvesDefaultInitialized();
knob = ret->getKnob();
instance = ret;
}
assert(knob);
if (!instance) {
throw std::runtime_error( std::string("Parameter ") + paramName + " has unknown OFX type " + paramType );
}
#ifdef NATRON_ENABLE_IO_META_NODES
/**
* For readers/writers embedded in a ReadNode or WriteNode, the holder will be the ReadNode and WriteNode
* but to ensure that all functions such as getKnobByName actually work, we add them to the knob vector so that
* interacting with the Reader or the container is actually the same.
**/
if ( knob->getHolder() != getOfxEffectInstance().get() ) {
getOfxEffectInstance()->addKnob(knob);
}
#endif
OfxParamToKnob* ptk = dynamic_cast<OfxParamToKnob*>(instance);
assert(ptk);
if (!ptk) {
throw std::logic_error("");
}
//knob->setName(paramName);
knob->setEvaluateOnChange( descriptor.getEvaluateOnChange() );
bool persistent = descriptor.getIsPersistent();
if (!paramShouldBePersistent) {
persistent = false;
}
const std::string & iconFilePath = descriptor.getProperties().getStringProperty(kOfxPropIcon, 1);
if ( !iconFilePath.empty() ) {
knob->setIconLabel(iconFilePath);
}
bool isMarkdown = descriptor.getProperties().getIntProperty(kNatronOfxPropDescriptionIsMarkdown);
knob->setHintIsMarkdown(isMarkdown);
knob->setIsMetadataSlave( isClipPreferencesSlaveParam(paramName) );
knob->setIsPersistent(persistent);
knob->setAnimationEnabled( descriptor.getCanAnimate() );
knob->setSecretByDefault(secretByDefault);
knob->setDefaultAllDimensionsEnabled(enabledByDefault);
knob->setHintToolTip( descriptor.getHint() );
knob->setCanUndo( descriptor.getCanUndo() );
knob->setSpacingBetweenItems( descriptor.getProperties().getIntProperty(kOfxParamPropLayoutPadWidth) );
if ( knob->isAnimationEnabled() ) {
KnobSignalSlotHandlerPtr handler = knob->getSignalSlotHandler();
if (handler) {
QObject::connect( handler.get(), SIGNAL(animationLevelChanged(ViewSpec,int)), ptk,
SLOT(onKnobAnimationLevelChanged(ViewSpec,int)) );
}
}
int layoutHint = descriptor.getProperties().getIntProperty(kOfxParamPropLayoutHint);
if (layoutHint == kOfxParamPropLayoutHintNoNewLine) {
knob->setAddNewLine(false);
} else if (layoutHint == kOfxParamPropLayoutHintDivider) {
knob->setAddSeparator(true);
}
knob->setInViewerContextItemSpacing( descriptor.getProperties().getIntProperty(kNatronOfxParamPropInViewerContextLayoutPadWidth) );
int viewportLayoutHint = descriptor.getProperties().getIntProperty(kNatronOfxParamPropInViewerContextLayoutHint);
if (viewportLayoutHint == kNatronOfxParamPropInViewerContextLayoutHintAddNewLine) {
knob->setInViewerContextNewLineActivated(true);
} else if (viewportLayoutHint == kNatronOfxParamPropInViewerContextLayoutHintNormalDivider) {
knob->setInViewerContextAddSeparator(true);
}
bool viewportSecret = (bool)descriptor.getProperties().getIntProperty(kNatronOfxParamPropInViewerContextSecret);
if (viewportSecret) {
knob->setInViewerContextSecret(viewportSecret);
}
const std::string& viewportLabel = descriptor.getProperties().getStringProperty(kNatronOfxParamPropInViewerContextLabel);
if (!viewportLabel.empty()) {
knob->setInViewerContextLabel(QString::fromUtf8(viewportLabel.c_str()));
}
knob->setOfxParamHandle( (void*)instance->getHandle() );
bool isInstanceSpecific = descriptor.getProperties().getIntProperty(kNatronOfxParamPropIsInstanceSpecific) != 0;
if (isInstanceSpecific) {
knob->setAsInstanceSpecific();
}
ptk->connectDynamicProperties();
return instance;
} // newParam
struct PageOrdered
{
KnobPagePtr page;
std::list<OfxParamToKnob*> paramsOrdered;
};
typedef boost::shared_ptr<PageOrdered> PageOrderedPtr;
typedef std::list<PageOrderedPtr> PageOrderedPtrList;
void
OfxImageEffectInstance::addParamsToTheirParents()
{
//All parameters in their order of declaration by the plug-in
const std::list<OFX::Host::Param::Instance*> & params = getParamList();
OfxEffectInstancePtr effect = getOfxEffectInstance();
//Extract pages and their children and add knobs to groups
PageOrderedPtrList finalPages;
for (std::list<OFX::Host::Param::Instance*>::const_iterator it = params.begin(); it != params.end(); ++it) {
OfxParamToKnob* isKnownKnob = dynamic_cast<OfxParamToKnob*>(*it);
assert(isKnownKnob);
if (!isKnownKnob) {
continue;
}
KnobIPtr associatedKnob = isKnownKnob->getKnob();
if (!associatedKnob) {
continue;
}
OfxPageInstance* isPage = dynamic_cast<OfxPageInstance*>(*it);
if (isPage) {
const std::map<int, OFX::Host::Param::Instance*>& children = isPage->getChildren();
PageOrderedPtr pageData = boost::make_shared<PageOrdered>();
pageData->page = boost::dynamic_pointer_cast<KnobPage>(associatedKnob);
assert(pageData->page);
std::map<OfxParamToKnob*, int> childrenList;
for (std::map<int, OFX::Host::Param::Instance*>::const_iterator it2 = children.begin(); it2 != children.end(); ++it2) {
OfxParamToKnob* isParamToKnob = dynamic_cast<OfxParamToKnob*>(it2->second);
assert(isParamToKnob);
if (!isParamToKnob) {
continue;
}
pageData->paramsOrdered.push_back(isParamToKnob);
}
finalPages.push_back(pageData);
} else {
OFX::Host::Param::Instance* hasParent = (*it)->getParentInstance();
if (hasParent) {
OfxGroupInstance* parentIsGroup = dynamic_cast<OfxGroupInstance*>(hasParent);
if (!parentIsGroup) {
std::cerr << getDescriptor().getPlugin()->getIdentifier() << ": Warning: attempting to set a parent which is not a group. (" << (*it)->getName() << ")" << std::endl;
} else {
parentIsGroup->addKnob(associatedKnob);
///Add a separator in the group if needed
if ( associatedKnob->isSeparatorActivated() ) {
std::string separatorName = (*it)->getName() + "_separator";
KnobHolder* knobHolder = associatedKnob->getHolder();
KnobSeparatorPtr sep = knobHolder->getKnobByNameAndType<KnobSeparator>(separatorName);
if (sep) {
sep->resetParent();
} else {
sep = AppManager::createKnob<KnobSeparator>( knobHolder, std::string() );
assert(sep);
sep->setName(separatorName);
#ifdef NATRON_ENABLE_IO_META_NODES
/**
* For readers/writers embedded in a ReadNode or WriteNode, the holder will be the ReadNode and WriteNode
* but to ensure that all functions such as getKnobByName actually work, we add them to the knob vector so that
* interacting with the Reader or the container is actually the same.
**/
if ( knobHolder != getOfxEffectInstance().get() ) {
getOfxEffectInstance()->addKnob(sep);
}
#endif
}
parentIsGroup->addKnob(sep);
}
}
}
}
}
//Extract the "Main" page, i.e: the first page declared, if no page were created, create one
PageOrderedPtrList::iterator mainPage = finalPages.end();
if ( !finalPages.empty() ) {
mainPage = finalPages.begin();
} else {
KnobPagePtr page = AppManager::createKnob<KnobPage>( effect.get(), tr("Settings") );
PageOrderedPtr pageData = boost::make_shared<PageOrdered>();
pageData->page = page;
finalPages.push_back(pageData);
mainPage = finalPages.begin();
}
assert( mainPage != finalPages.end() );
if ( mainPage == finalPages.end() ) {
throw std::logic_error("");
}
// In this pass we check that all parameters belong to a page.
// For parameters that do not belong to a page, we add them "on the fly" to the page
std::list<OfxParamToKnob*> &mainPageParamsOrdered = (*mainPage)->paramsOrdered;
std::list<OfxParamToKnob*>::iterator lastParamInsertedInMainPage = mainPageParamsOrdered.end();
for (std::list<OFX::Host::Param::Instance*>::const_iterator it = params.begin(); it != params.end(); ++it) {
OfxParamToKnob* isKnownKnob = dynamic_cast<OfxParamToKnob*>(*it);
assert(isKnownKnob);
if (!isKnownKnob) {
continue;
}
KnobIPtr knob = isKnownKnob->getKnob();
assert(knob);
if (!knob) {
continue;
}
KnobPage* isPage = dynamic_cast<KnobPage*>( knob.get() );
if (isPage) {
continue;
}
bool foundPage = false;
for (std::list<OfxParamToKnob*>::iterator itParam = mainPageParamsOrdered.begin(); itParam != mainPageParamsOrdered.end(); ++itParam) {
if (isKnownKnob == *itParam) {
foundPage = true;
lastParamInsertedInMainPage = itParam;
break;
}
}
if (!foundPage) {
// param not found in main page, try in other pages
PageOrderedPtrList::iterator itPage = mainPage;
++itPage;
for (; itPage != finalPages.end(); ++itPage) {
for (std::list<OfxParamToKnob*>::iterator itParam = (*itPage)->paramsOrdered.begin(); itParam != (*itPage)->paramsOrdered.end(); ++itParam) {
if (isKnownKnob == *itParam) {
foundPage = true;
break;
}
}
if (foundPage) {
break;
}
}
}
if (!foundPage) {
//The parameter does not belong to a page, put it in the main page
if ( lastParamInsertedInMainPage != mainPageParamsOrdered.end() ) {
++lastParamInsertedInMainPage;
}
lastParamInsertedInMainPage = mainPageParamsOrdered.insert(lastParamInsertedInMainPage, isKnownKnob);
}
} // for (std::list<OFX::Host::Param::Instance*>::const_iterator it = params.begin(); it != params.end(); ++it)
// For all pages, append their knobs in order
for (PageOrderedPtrList::iterator itPage = finalPages.begin(); itPage != finalPages.end(); ++itPage) {
KnobPagePtr pageKnob = (*itPage)->page;
for (std::list<OfxParamToKnob*>::iterator itParam = (*itPage)->paramsOrdered.begin(); itParam != (*itPage)->paramsOrdered.end(); ++itParam) {
OfxParamToKnob* isKnownKnob = *itParam;
assert(isKnownKnob);
if (!isKnownKnob) {
continue;
}
KnobIPtr child = isKnownKnob->getKnob();
assert(child);
if ( !child->getParentKnob() ) {
pageKnob->addKnob(child);
if ( child->isSeparatorActivated() ) {
std::string separatorName = child->getName() + "_separator";
KnobHolder* knobHolder = child->getHolder();
KnobSeparatorPtr sep = knobHolder->getKnobByNameAndType<KnobSeparator>(separatorName);
if (sep) {
sep->resetParent();
} else {
sep = AppManager::createKnob<KnobSeparator>( knobHolder, std::string() );
assert(sep);
sep->setName(separatorName);
#ifdef NATRON_ENABLE_IO_META_NODES
/**
* For readers/writers embedded in a ReadNode or WriteNode, the holder will be the ReadNode and WriteNode
* but to ensure that all functions such as getKnobByName actually work, we add them to the knob vector so that
* interacting with the Reader or the container is actually the same.
**/
if ( knobHolder != getOfxEffectInstance().get() ) {
getOfxEffectInstance()->addKnob(sep);
}
#endif
}
pageKnob->addKnob(sep);
}
} // if (!knob->getParentKnob()) {
}
}
// Add the parameters to the viewport in order
int nDims = getProps().getDimension(kNatronOfxImageEffectPropInViewerContextParamsOrder);
for (int i = 0; i < nDims; ++i) {
const std::string& paramName = getProps().getStringProperty(kNatronOfxImageEffectPropInViewerContextParamsOrder);
OFX::Host::Param::Instance* param = getParam(paramName);
if (!param) {
continue;
}
OfxParamToKnob* isKnownKnob = dynamic_cast<OfxParamToKnob*>(param);
assert(isKnownKnob);
if (isKnownKnob) {
KnobIPtr knob = isKnownKnob->getKnob();
assert(knob);
effect->addKnobToViewerUI(knob);
}
}
} // OfxImageEffectInstance::addParamsToTheirParents
/** @brief Used to group any parameter changes for undo/redo purposes
\arg paramSet the parameter set in which this is happening
\arg name label to attach to any undo/redo string UTF8
If a plugin calls paramSetValue/paramSetValueAtTime on one or more parameters, either from custom GUI interaction
or some analysis of imagery etc.. this is used to indicate the start of a set of a parameter
changes that should be considered part of a single undo/redo block.
See also OfxParameterSuiteV1::paramEditEnd
\return
- ::kOfxStatOK - all was OK
- ::kOfxStatErrBadHandle - if the instance handle was invalid
*/
OfxStatus
OfxImageEffectInstance::editBegin(const std::string & /*name*/)
{
///Don't push undo/redo actions while creating a group
OfxEffectInstancePtr effect = getOfxEffectInstance();
if ( !effect->getApp()->isCreatingPythonGroup() ) {
effect->setMultipleParamsEditLevel(KnobHolder::eMultipleParamsEditOnCreateNewCommand);
}
return kOfxStatOK;
}
/// Triggered when the plug-in calls OfxParameterSuiteV1::paramEditEnd
///
/// Client host code needs to implement this
OfxStatus
OfxImageEffectInstance::editEnd()
{
///Don't push undo/redo actions while creating a group
OfxEffectInstancePtr effect = getOfxEffectInstance();
if ( !effect->getApp()->isCreatingPythonGroup() ) {
effect->setMultipleParamsEditLevel(KnobHolder::eMultipleParamsEditOff);
}