forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteNode.cpp
1338 lines (1135 loc) · 46.4 KB
/
WriteNode.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 "WriteNode.h"
#include <sstream> // stringstream
#include "Global/QtCompat.h"
#if !defined(SBK_RUN) && !defined(Q_MOC_RUN)
GCC_DIAG_UNUSED_LOCAL_TYPEDEFS_OFF
#include <boost/algorithm/string/predicate.hpp> // iequals
GCC_DIAG_UNUSED_LOCAL_TYPEDEFS_ON
#endif
#if !defined(Q_MOC_RUN) && !defined(SBK_RUN)
GCC_DIAG_UNUSED_LOCAL_TYPEDEFS_OFF
GCC_DIAG_OFF(unused-parameter)
// /opt/local/include/boost/serialization/smart_cast.hpp:254:25: warning: unused parameter 'u' [-Wunused-parameter]
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
// /usr/local/include/boost/serialization/shared_ptr.hpp:112:5: warning: unused typedef 'boost_static_assert_typedef_112' [-Wunused-local-typedef]
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/version.hpp>
GCC_DIAG_UNUSED_LOCAL_TYPEDEFS_ON
GCC_DIAG_ON(unused-parameter)
#endif
CLANG_DIAG_OFF(deprecated)
CLANG_DIAG_OFF(uninitialized)
#include <QtCore/QCoreApplication>
CLANG_DIAG_ON(deprecated)
CLANG_DIAG_ON(uninitialized)
#include <ofxNatron.h>
#include "Engine/AppInstance.h"
#include "Engine/AppManager.h"
#include "Engine/CreateNodeArgs.h"
#include "Engine/Node.h"
#include "Engine/KnobTypes.h"
#include "Engine/KnobFile.h"
#include "Engine/NodeSerialization.h"
#include "Engine/KnobSerialization.h" // createDefaultValueForParam
#include "Engine/OutputSchedulerThread.h"
#include "Engine/Plugin.h"
#include "Engine/Project.h"
#include "Engine/ReadNode.h"
#include "Engine/Settings.h"
//The plug-in that is instantiated whenever this node is created and doesn't point to any valid or known extension
#define WRITE_NODE_DEFAULT_WRITER PLUGINID_OFX_WRITEOIIO
#define kPluginSelectorParamEntryDefault "Default"
NATRON_NAMESPACE_ENTER
//Generic Writer
#define kParamFilename kOfxImageEffectFileParamName
#define kParamOutputFormat kNatronParamFormatChoice
#define kParamFormatType "formatType"
#define kParamFormatSize kNatronParamFormatSize
#define kParamFormatPar kNatronParamFormatPar
#define kParamFrameRange "frameRange"
#define kParamFirstFrame "firstFrame"
#define kParamLastFrame "lastFrame"
#define kParamInputPremult "inputPremult"
#define kParamClipInfo "clipInfo"
#define kParamOutputSpaceLabel "File Colorspace"
#define kParamClipToProject "clipToProject"
#define kNatronOfxParamProcessR "NatronOfxParamProcessR"
#define kNatronOfxParamProcessG "NatronOfxParamProcessG"
#define kNatronOfxParamProcessB "NatronOfxParamProcessB"
#define kNatronOfxParamProcessA "NatronOfxParamProcessA"
#define kParamOutputSpaceSet "ocioOutputSpaceSet"
#define kParamExistingInstance "ParamExistingInstance"
//Generic OCIO
#define kOCIOParamConfigFile "ocioConfigFile"
#define kOCIOParamInputSpace "ocioInputSpace"
#define kOCIOParamOutputSpace "ocioOutputSpace"
#define kOCIOParamInputSpaceChoice "ocioInputSpaceIndex"
#define kOCIOParamOutputSpaceChoice "ocioOutputSpaceIndex"
#define kOCIOHelpButton "ocioHelp"
#define kOCIOHelpLooksButton "ocioHelpLooks"
#define kOCIOHelpDisplaysButton "ocioHelpDisplays"
#define kOCIOParamContext "Context"
/*
These are names of knobs that are defined in GenericWriter and that should stay on the interface
no matter what the internal Reader is.
*/
struct GenericKnob
{
const char* scriptName;
bool mustKeepValue;
};
static GenericKnob genericWriterKnobNames[] =
{
{kParamFilename, false},
{kParamOutputFormat, true},
{kParamFormatType, true},
{kParamFormatSize, true},
{kParamFormatPar, true},
{kParamFrameRange, true},
{kParamFirstFrame, true},
{kParamLastFrame, true},
{kParamInputPremult, true}, // keep: don't change useful params behind the user's back
{kParamClipInfo, false},
{kParamOutputSpaceLabel, false},
{kParamClipToProject, true}, // keep: don't change useful params behind the user's back
{kNatronOfxParamProcessR, true},
{kNatronOfxParamProcessG, true},
{kNatronOfxParamProcessB, true},
{kNatronOfxParamProcessA, true},
{kParamOutputSpaceSet, true}, // keep: don't change useful params behind the user's back
{kParamExistingInstance, true}, // don't automatically set parameters when changing the filename, see GenericWriterPlugin::outputFileChanged()
{kOCIOParamConfigFile, true},
{kOCIOParamInputSpace, true}, // keep: don't change useful params behind the user's back
{kOCIOParamOutputSpace, false}, // don't keep: depends on format
{kOCIOParamInputSpaceChoice, true},
{kOCIOParamOutputSpaceChoice, false},
{kOCIOHelpButton, false},
{kOCIOHelpLooksButton, false},
{kOCIOHelpDisplaysButton, false},
{kOCIOParamContext, false},
{0, false}
};
static bool
isGenericKnob(const std::string& knobName,
bool *mustSerialize)
{
int i = 0;
while (genericWriterKnobNames[i].scriptName) {
if (genericWriterKnobNames[i].scriptName == knobName) {
*mustSerialize = genericWriterKnobNames[i].mustKeepValue;
return true;
}
++i;
}
return false;
}
bool
WriteNode::isBundledWriter(const std::string& pluginID,
bool wasProjectCreatedWithLowerCaseIDs)
{
if (wasProjectCreatedWithLowerCaseIDs) {
// Natron 1.x has plugin ids stored in lowercase
return ( boost::iequals(pluginID, PLUGINID_OFX_WRITEOIIO) ||
boost::iequals(pluginID, PLUGINID_OFX_WRITEFFMPEG) ||
boost::iequals(pluginID, PLUGINID_OFX_WRITEPFM) ||
boost::iequals(pluginID, PLUGINID_OFX_WRITEPNG) );
}
return (pluginID == PLUGINID_OFX_WRITEOIIO ||
pluginID == PLUGINID_OFX_WRITEFFMPEG ||
pluginID == PLUGINID_OFX_WRITEPFM ||
pluginID == PLUGINID_OFX_WRITEPNG);
}
bool
WriteNode::isBundledWriter(const std::string& pluginID)
{
return isBundledWriter( pluginID, getApp()->wasProjectCreatedWithLowerCaseIDs() );
}
struct WriteNodePrivate
{
Q_DECLARE_TR_FUNCTIONS(WriteNode)
public:
WriteNode* _publicInterface;
NodeWPtr embeddedPlugin, readBackNode, inputNode, outputNode;
std::list<KnobSerializationPtr> genericKnobsSerialization;
KnobOutputFileWPtr outputFileKnob;
//Thiese are knobs owned by the ReadNode and not the Reader
KnobIntWPtr frameIncrKnob;
KnobBoolWPtr readBackKnob;
KnobChoiceWPtr pluginSelectorKnob;
KnobStringWPtr pluginIDStringKnob;
KnobSeparatorWPtr separatorKnob;
KnobButtonWPtr renderButtonKnob;
std::list<KnobIWPtr> writeNodeKnobs;
//MT only
int creatingWriteNode;
// Plugin-ID of the last read node created.
// If this is different, we do not load serialized knobs
std::string lastPluginIDCreated;
WriteNodePrivate(WriteNode* publicInterface)
: _publicInterface(publicInterface)
, embeddedPlugin()
, readBackNode()
, inputNode()
, outputNode()
, genericKnobsSerialization()
, outputFileKnob()
, frameIncrKnob()
, pluginSelectorKnob()
, pluginIDStringKnob()
, separatorKnob()
, renderButtonKnob()
, writeNodeKnobs()
, creatingWriteNode(0)
, lastPluginIDCreated()
{
}
void placeWriteNodeKnobsInPage();
void createReadNodeAndConnectGraph(const std::string& filename);
void createWriteNode(bool throwErrors, const std::string& filename, const NodeSerializationPtr& serialization);
void destroyWriteNode();
void cloneGenericKnobs();
void refreshPluginSelectorKnob();
void createDefaultWriteNode();
bool checkEncoderCreated(double time, ViewIdx view);
void setReadNodeOriginalFrameRange();
};
class SetCreatingWriterRAIIFlag
{
WriteNodePrivate* _p;
public:
SetCreatingWriterRAIIFlag(WriteNodePrivate* p)
: _p(p)
{
++p->creatingWriteNode;
}
~SetCreatingWriterRAIIFlag()
{
--_p->creatingWriteNode;
}
};
WriteNode::WriteNode(NodePtr n)
: NodeGroup(n)
, _imp( new WriteNodePrivate(this) )
{
setSupportsRenderScaleMaybe(eSupportsYes);
}
WriteNode::~WriteNode()
{
}
NodePtr
WriteNode::getEmbeddedWriter() const
{
return _imp->embeddedPlugin.lock();
}
void
WriteNode::setEmbeddedWriter(const NodePtr& node)
{
_imp->embeddedPlugin = node;
}
void
WriteNodePrivate::placeWriteNodeKnobsInPage()
{
KnobIPtr pageKnob = _publicInterface->getKnobByName("Controls");
KnobPage* isPage = dynamic_cast<KnobPage*>( pageKnob.get() );
if (!isPage) {
return;
}
for (std::list<KnobIWPtr>::iterator it = writeNodeKnobs.begin(); it != writeNodeKnobs.end(); ++it) {
KnobIPtr knob = it->lock();
knob->setParentKnob( KnobIPtr() );
isPage->removeKnob( knob.get() );
}
KnobsVec children = isPage->getChildren();
int index = -1;
for (std::size_t i = 0; i < children.size(); ++i) {
if (children[i]->getName() == kParamLastFrame) {
index = i;
break;
}
}
if (index != -1) {
++index;
for (std::list<KnobIWPtr>::iterator it = writeNodeKnobs.begin(); it != writeNodeKnobs.end(); ++it) {
KnobIPtr knob = it->lock();
isPage->insertKnob(index, knob);
++index;
}
}
// Find the separatorKnob in the page and if the next parameter is also a separator, hide it
int foundSep = -1;
for (std::size_t i = 0; i < children.size(); ++i) {
if (children[i]== separatorKnob.lock()) {
foundSep = i;
break;
}
}
if (foundSep != -1) {
++foundSep;
if (foundSep < (int)children.size()) {
bool isSecret = children[foundSep]->getIsSecret();
while (isSecret && foundSep < (int)children.size()) {
++foundSep;
isSecret = children[foundSep]->getIsSecret();
}
if (foundSep < (int)children.size()) {
separatorKnob.lock()->setSecret(dynamic_cast<KnobSeparator*>(children[foundSep].get()));
} else {
separatorKnob.lock()->setSecret(true);
}
} else {
separatorKnob.lock()->setSecret(true);
}
}
//Set the render button as the last knob
KnobButtonPtr renderB = renderButtonKnob.lock();
if (renderB) {
renderB->setParentKnob( KnobIPtr() );
isPage->removeKnob( renderB.get() );
isPage->addKnob(renderB);
}
}
void
WriteNodePrivate::cloneGenericKnobs()
{
const KnobsVec& knobs = _publicInterface->getKnobs();
for (std::list<KnobSerializationPtr>::iterator it = genericKnobsSerialization.begin(); it != genericKnobsSerialization.end(); ++it) {
KnobIPtr serializedKnob = (*it)->getKnob();
for (KnobsVec::const_iterator it2 = knobs.begin(); it2 != knobs.end(); ++it2) {
if ( (*it2)->getName() == serializedKnob->getName() ) {
KnobChoice* isChoice = dynamic_cast<KnobChoice*>( (*it2).get() );
KnobChoice* choiceSerialized = dynamic_cast<KnobChoice*>( serializedKnob.get() );;
if (isChoice && choiceSerialized) {
const ChoiceExtraData* choiceData = dynamic_cast<const ChoiceExtraData*>( (*it)->getExtraData() );
assert(choiceData);
if (choiceData) {
std::string optionID = choiceData->_choiceString;
// first, try to get the id the easy way ( see choiceMatch() )
int id = isChoice->choiceRestorationId(choiceSerialized, optionID);
#pragma message WARN("TODO: choice id filters")
//if (id < 0) {
// // no luck, try the filters
// filterKnobChoiceOptionCompat(getPluginID(), serialization.getPluginMajorVersion(), serialization.getPluginMinorVersion(), projectInfos.vMajor, projectInfos.vMinor, projectInfos.vRev, serializedName, &optionID);
// id = isChoice->choiceRestorationId(choiceSerialized, optionID);
//}
isChoice->choiceRestoration(choiceSerialized, optionID, id);
}
} else {
(*it2)->clone( serializedKnob.get() );
}
/*(*it2)->setSecret( serializedKnob->getIsSecret() );
if ( (*it2)->getDimension() == serializedKnob->getDimension() ) {
for (int i = 0; i < (*it2)->getDimension(); ++i) {
(*it2)->setEnabled( i, serializedKnob->isEnabled(i) );
}
}*/
break;
}
}
}
}
void
WriteNodePrivate::destroyWriteNode()
{
assert( QThread::currentThread() == qApp->thread() );
NodePtr embeddedNode = embeddedPlugin.lock();
if (!embeddedNode) {
return;
}
KnobsVec knobs = _publicInterface->getKnobs();
genericKnobsSerialization.clear();
std::string serializationString;
try {
std::ostringstream ss;
{ // see http://boost.2283326.n4.nabble.com/the-boost-xml-serialization-to-a-stringstream-does-not-have-an-end-tag-td2580772.html
// xml_oarchive must be destroyed before obtaining ss.str(), or the </boost_serialization> tag is missing,
// which throws an exception in boost 1.66.0, due to the following change:
// https://fossies.org/diffs/boost/1_65_1_vs_1_66_0/libs/serialization/src/basic_xml_grammar.ipp-diff.html
// see also https://svn.boost.org/trac10/ticket/13400
// see also https://svn.boost.org/trac10/ticket/13354
boost::archive::xml_oarchive oArchive(ss);
std::list<KnobSerializationPtr> serialized;
for (KnobsVec::iterator it = knobs.begin(); it != knobs.end(); ++it) {
// The internal node still holds a shared ptr to the knob.
// Since we want to keep some knobs around, ensure they do not get deleted in the destructor of the embedded node
embeddedNode->getEffectInstance()->removeKnobFromList(it->get());
if ( !(*it)->isDeclaredByPlugin() ) {
continue;
}
//If it is a knob of this WriteNode, do not destroy it
bool isWriteNodeKnob = false;
for (std::list<KnobIWPtr>::iterator it2 = writeNodeKnobs.begin(); it2 != writeNodeKnobs.end(); ++it2) {
if (it2->lock() == *it) {
isWriteNodeKnob = true;
break;
}
}
if (isWriteNodeKnob) {
continue;
}
//Keep pages around they will be re-used
KnobPage* isPage = dynamic_cast<KnobPage*>( it->get() );
if (isPage) {
continue;
}
//This is a knob of the Writer plug-in
//Serialize generic knobs and keep them around until we create a new Writer plug-in
bool mustSerializeKnob;
bool isGeneric = isGenericKnob( (*it)->getName(), &mustSerializeKnob );
if (!isGeneric || mustSerializeKnob) {
/*if (!isGeneric && !(*it)->getDefaultIsSecret()) {
// Don't save the secret state otherwise some knobs could be invisible when cloning the serialization even if we change format
(*it)->setSecret(false);
}*/
KnobSerializationPtr s = boost::make_shared<KnobSerialization>(*it);
serialized.push_back(s);
}
if (!isGeneric) {
try {
_publicInterface->deleteKnob(it->get(), false);
} catch (...) {
}
}
}
int n = (int)serialized.size();
oArchive << boost::serialization::make_nvp("numItems", n);
for (std::list<KnobSerializationPtr>::const_iterator it = serialized.begin(); it!= serialized.end(); ++it) {
oArchive << boost::serialization::make_nvp("item", **it);
}
}
serializationString = ss.str();
} catch (...) {
assert(false);
}
try {
std::stringstream ss(serializationString);
{
boost::archive::xml_iarchive iArchive(ss);
int n ;
iArchive >> boost::serialization::make_nvp("numItems", n);
for (int i = 0; i < n; ++i) {
KnobSerializationPtr s = boost::make_shared<KnobSerialization>();
iArchive >> boost::serialization::make_nvp("item", *s);
genericKnobsSerialization.push_back(s);
}
}
} catch (const std::exception& e) {
qDebug() << e.what();
assert(false);
} catch (...) {
assert(false);
}
//This will remove the GUI of non generic parameters
_publicInterface->recreateKnobs(true);
#pragma message WARN("TODO: if Gui, refresh pluginID, version, help tooltip in DockablePanel to reflect embedded node change")
if (embeddedNode) {
embeddedNode->destroyNode(true, false);
}
embeddedPlugin.reset();
NodePtr readBack = readBackNode.lock();
if (readBack) {
readBack->destroyNode(true, false);
}
readBackNode.reset();
} // WriteNodePrivate::destroyWriteNode
void
WriteNodePrivate::createDefaultWriteNode()
{
NodeGroupPtr isNodeGroup = boost::dynamic_pointer_cast<NodeGroup>( _publicInterface->shared_from_this() );
CreateNodeArgs args( WRITE_NODE_DEFAULT_WRITER, isNodeGroup );
args.setProperty(kCreateNodeArgsPropNoNodeGUI, true);
args.setProperty(kCreateNodeArgsPropOutOfProject, true);
args.setProperty(kCreateNodeArgsPropSilent, true);
args.setProperty(kCreateNodeArgsPropMetaNodeContainer, _publicInterface->getNode());
args.setProperty<std::string>(kCreateNodeArgsPropNodeInitialName, "defaultWriteNodeWriter");
args.setProperty<bool>(kCreateNodeArgsPropAllowNonUserCreatablePlugins, true);
//args.paramValues.push_back(createDefaultValueForParam<std::string>(kOfxImageEffectFileParamName, filePattern));
embeddedPlugin = _publicInterface->getApp()->createNode(args);
if ( !embeddedPlugin.lock() ) {
QString error = tr("The IO.ofx.bundle OpenFX plug-in is required to use this node, make sure it is installed.");
throw std::runtime_error( error.toStdString() );
}
//We need to explcitly refresh the Python knobs since we attached the embedded node knobs into this node.
_publicInterface->getNode()->declarePythonFields();
//Destroy it to keep the default parameters
destroyWriteNode();
placeWriteNodeKnobsInPage();
separatorKnob.lock()->setSecret(true);
}
bool
WriteNodePrivate::checkEncoderCreated(double time,
ViewIdx view)
{
KnobOutputFilePtr fileKnob = outputFileKnob.lock();
assert(fileKnob);
std::string pattern = fileKnob->generateFileNameAtTime( std::floor(time + 0.5), ViewSpec( view.value() ) ).toStdString();
if ( pattern.empty() ) {
_publicInterface->setPersistentMessage( eMessageTypeError, tr("Filename is empty.").toStdString() );
return false;
}
if ( !embeddedPlugin.lock() ) {
QString s = tr("Encoder was not created for %1. Check that the file exists and its format is supported.")
.arg( QString::fromUtf8( pattern.c_str() ) );
_publicInterface->setPersistentMessage( eMessageTypeError, s.toStdString() );
return false;
}
return true;
}
static std::string
getFileNameFromSerialization(const std::list<KnobSerializationPtr>& serializations)
{
std::string filePattern;
for (std::list<KnobSerializationPtr>::const_iterator it = serializations.begin(); it != serializations.end(); ++it) {
if ( (*it)->getKnob()->getName() == kOfxImageEffectFileParamName ) {
KnobStringBase* isString = dynamic_cast<KnobStringBase*>( (*it)->getKnob().get() );
assert(isString);
if (isString) {
filePattern = isString->getValue();
}
break;
}
}
return filePattern;
}
void
WriteNodePrivate::setReadNodeOriginalFrameRange()
{
NodePtr readNode = readBackNode.lock();
if (!readNode) {
return;
}
NodePtr writeNode = embeddedPlugin.lock();
if (!writeNode) {
return;
}
double first, last;
writeNode->getEffectInstance()->getFrameRange_public(writeNode->getEffectInstance()->getHash(), &first, &last);
{
KnobIPtr originalFrameRangeKnob = readNode->getKnobByName(kReaderParamNameOriginalFrameRange);
assert(originalFrameRangeKnob);
KnobInt* originalFrameRange = dynamic_cast<KnobInt*>( originalFrameRangeKnob.get() );
if (originalFrameRange) {
originalFrameRange->setValues(first, last, ViewSpec::all(), eValueChangedReasonNatronInternalEdited);
}
}
{
KnobIPtr firstFrameKnob = readNode->getKnobByName(kParamFirstFrame);
assert(firstFrameKnob);
KnobInt* firstFrame = dynamic_cast<KnobInt*>( firstFrameKnob.get() );
if (firstFrame) {
firstFrame->setValue(first);
}
}
{
KnobIPtr lastFrameKnob = readNode->getKnobByName(kParamLastFrame);
assert(lastFrameKnob);
KnobInt* lastFrame = dynamic_cast<KnobInt*>( lastFrameKnob.get() );
if (lastFrame) {
lastFrame->setValue(last);
}
}
}
void
WriteNodePrivate::createReadNodeAndConnectGraph(const std::string& filename)
{
QString qpattern = QString::fromUtf8( filename.c_str() );
std::string ext = QtCompat::removeFileExtension(qpattern).toLower().toStdString();
NodeGroupPtr isNodeGroup = boost::dynamic_pointer_cast<NodeGroup>( _publicInterface->shared_from_this() );
std::string readerPluginID = appPTR->getReaderPluginIDForFileType(ext);
NodePtr writeNode = embeddedPlugin.lock();
readBackNode.reset();
if ( !readerPluginID.empty() ) {
CreateNodeArgs args(readerPluginID, isNodeGroup );
args.setProperty(kCreateNodeArgsPropNoNodeGUI, true);
args.setProperty(kCreateNodeArgsPropOutOfProject, true);
args.setProperty<std::string>(kCreateNodeArgsPropNodeInitialName, "internalDecoderNode");
args.setProperty<bool>(kCreateNodeArgsPropAllowNonUserCreatablePlugins, true);
//Set a pre-value for the inputfile knob only if it did not exist
if ( !filename.empty() ) {
args.addParamDefaultValue<std::string>(kOfxImageEffectFileParamName, filename);
}
if (writeNode) {
double first, last;
writeNode->getEffectInstance()->getFrameRange_public(writeNode->getEffectInstance()->getHash(), &first, &last);
std::vector<int> originalRange(2);
originalRange[0] = (int)first;
originalRange[1] = (int)last;
args.addParamDefaultValueN<int>(kReaderParamNameOriginalFrameRange, originalRange);
args.addParamDefaultValue<int>(kParamFirstFrame, (int)first);
args.addParamDefaultValue<int>(kParamFirstFrame, (int)last);
}
readBackNode = _publicInterface->getApp()->createNode(args);
}
NodePtr input = inputNode.lock(), output = outputNode.lock();
assert(input && output);
bool connectOutputToInput = true;
if (writeNode) {
writeNode->replaceInput(input, 0);
NodePtr readNode = readBackNode.lock();
if (readNode) {
bool readFile = readBackKnob.lock()->getValue();
if (readFile) {
output->replaceInput(readNode, 0);
connectOutputToInput = false;
}
readNode->replaceInput(input, 0);
// sync the output colorspace of the reader from input colorspace of the writer
KnobIPtr outputWriteColorSpace = writeNode->getKnobByName(kOCIOParamOutputSpace);
KnobIPtr inputReadColorSpace = readNode->getKnobByName(kNatronReadNodeOCIOParamInputSpace);
if (inputReadColorSpace && outputWriteColorSpace) {
inputReadColorSpace->slaveTo(0, outputWriteColorSpace, 0);
}
}
}
if (connectOutputToInput) {
output->replaceInput(input, 0);
}
} // WriteNodePrivate::createReadNodeAndConnectGraph
void
WriteNodePrivate::createWriteNode(bool throwErrors,
const std::string& filename,
const NodeSerializationPtr& serialization)
{
if (creatingWriteNode) {
return;
}
NodeGroupPtr isNodeGroup = boost::dynamic_pointer_cast<NodeGroup>( _publicInterface->shared_from_this() );
NodePtr input = inputNode.lock(), output = outputNode.lock();
//NodePtr maskInput;
assert( (input && output) || (!input && !output) );
if (!output) {
CreateNodeArgs args(PLUGINID_NATRON_OUTPUT, isNodeGroup);
args.setProperty(kCreateNodeArgsPropNoNodeGUI, true);
args.setProperty(kCreateNodeArgsPropOutOfProject, true);
args.setProperty<bool>(kCreateNodeArgsPropAllowNonUserCreatablePlugins, true);
output = _publicInterface->getApp()->createNode(args);
try {
output->setScriptName("Output");
} catch (...) {
}
assert(output);
outputNode = output;
}
if (!input) {
CreateNodeArgs args(PLUGINID_NATRON_INPUT, isNodeGroup);
args.setProperty(kCreateNodeArgsPropNoNodeGUI, true);
args.setProperty(kCreateNodeArgsPropOutOfProject, true);
args.setProperty<std::string>(kCreateNodeArgsPropNodeInitialName, "Source");
args.setProperty<bool>(kCreateNodeArgsPropAllowNonUserCreatablePlugins, true);
input = _publicInterface->getApp()->createNode(args);
assert(input);
inputNode = input;
}
SetCreatingWriterRAIIFlag creatingNode__(this);
QString qpattern = QString::fromUtf8( filename.c_str() );
std::string ext = QtCompat::removeFileExtension(qpattern).toLower().toStdString();
KnobStringPtr pluginIDKnob = pluginIDStringKnob.lock();
std::string writerPluginID = pluginIDKnob->getValue();
if ( writerPluginID.empty() ) {
KnobChoicePtr pluginChoiceKnob = pluginSelectorKnob.lock();
int pluginChoice_i = pluginChoiceKnob->getValue();
if (pluginChoice_i == 0) {
//Use default
writerPluginID = appPTR->getWriterPluginIDForFileType(ext);
} else {
std::vector<ChoiceOption> entries = pluginChoiceKnob->getEntries_mt_safe();
if ( (pluginChoice_i >= 0) && ( pluginChoice_i < (int)entries.size() ) ) {
writerPluginID = entries[pluginChoice_i].id;
}
}
}
// If the plug-in is the same, do not create a new decoder.
{
NodePtr writeNode = embeddedPlugin.lock();
if (writeNode && writeNode->getPluginID() == writerPluginID) {
KnobOutputFilePtr fileKnob = outputFileKnob.lock();
assert(fileKnob);
if (fileKnob) {
// Make sure instance changed action is called on the decoder and not caught in our knobChanged handler.
writeNode->getEffectInstance()->onKnobValueChanged_public(fileKnob.get(), eValueChangedReasonNatronInternalEdited, _publicInterface->getCurrentTime(), ViewSpec(0), true);
}
return;
}
}
//Destroy any previous reader
//This will store the serialization of the generic knobs
destroyWriteNode();
bool defaultFallback = false;
//Find the appropriate reader
if (writerPluginID.empty() && !serialization) {
//Couldn't find any reader
if ( !ext.empty() ) {
QString message = tr("No plugin capable of encoding %1 was found.").arg( QString::fromUtf8( ext.c_str() ) );
//Dialogs::errorDialog(tr("Read").toStdString(), message.toStdString(), false);
if (throwErrors) {
throw std::runtime_error( message.toStdString() );
}
}
defaultFallback = true;
} else {
if ( writerPluginID.empty() ) {
writerPluginID = WRITE_NODE_DEFAULT_WRITER;
}
CreateNodeArgs args(writerPluginID, isNodeGroup );
args.setProperty(kCreateNodeArgsPropNoNodeGUI, true);
args.setProperty(kCreateNodeArgsPropOutOfProject, true);
args.setProperty<std::string>(kCreateNodeArgsPropNodeInitialName, "internalEncoderNode");
args.setProperty<NodeSerializationPtr>(kCreateNodeArgsPropNodeSerialization, serialization);
args.setProperty<NodePtr>(kCreateNodeArgsPropMetaNodeContainer, _publicInterface->getNode());
args.setProperty<bool>(kCreateNodeArgsPropAllowNonUserCreatablePlugins, true);
if (serialization) {
args.setProperty<bool>(kCreateNodeArgsPropSilent, true);
args.setProperty<bool>(kCreateNodeArgsPropAllowNonUserCreatablePlugins, true); // also load deprecated plugins
}
NodePtr writeNode = _publicInterface->getApp()->createNode(args);
embeddedPlugin = writeNode;
if (pluginIDKnob) {
pluginIDKnob->setValue(writerPluginID);
}
// Set the filename value
if (writeNode) {
KnobOutputFilePtr fileKnob = boost::dynamic_pointer_cast<KnobOutputFile>(writeNode->getKnobByName(kOfxImageEffectFileParamName));
if (fileKnob) {
fileKnob->setValue(filename);
}
}
placeWriteNodeKnobsInPage();
separatorKnob.lock()->setSecret(false);
//We need to explcitly refresh the Python knobs since we attached the embedded node knobs into this node.
_publicInterface->getNode()->declarePythonFields();
}
if ( !embeddedPlugin.lock() ) {
defaultFallback = true;
}
if (defaultFallback) {
createDefaultWriteNode();
}
// Make the write node be a pass-through while we do not render
NodePtr writeNode = embeddedPlugin.lock();
bool readFromFile = readBackKnob.lock()->getValue();
if (readFromFile) {
createReadNodeAndConnectGraph(filename);
} else {
NodePtr input = inputNode.lock(), output = outputNode.lock();
if (writeNode) {
output->replaceInput(writeNode, 0);
writeNode->replaceInput(input, 0);
} else {
output->replaceInput(input, 0);
}
}
_publicInterface->getNode()->findPluginFormatKnobs();
// Clone the old values of the generic knobs if we created the same encoder than before
if (lastPluginIDCreated == writerPluginID) {
cloneGenericKnobs();
}
lastPluginIDCreated = writerPluginID;
NodePtr thisNode = _publicInterface->getNode();
//Refresh accepted bitdepths on the node
thisNode->refreshAcceptedBitDepths();
//Refresh accepted components
thisNode->initializeInputs();
//This will refresh the GUI with this Reader specific parameters
_publicInterface->recreateKnobs(true);
#pragma message WARN("TODO: if Gui, refresh pluginID, version, help tooltip in DockablePanel to reflect embedded node change")
KnobIPtr knob = writeNode ? writeNode->getKnobByName(kOfxImageEffectFileParamName) : _publicInterface->getKnobByName(kOfxImageEffectFileParamName);
if (knob) {
outputFileKnob = boost::dynamic_pointer_cast<KnobOutputFile>(knob);
}
} // WriteNodePrivate::createWriteNode
void
WriteNodePrivate::refreshPluginSelectorKnob()
{
KnobOutputFilePtr fileKnob = outputFileKnob.lock();
assert(fileKnob);
std::string filePattern = fileKnob->getValue();
std::vector<ChoiceOption> entries;
entries.push_back(ChoiceOption(kPluginSelectorParamEntryDefault, "", tr("Use the default plug-in chosen from the Preferences to write this file format").toStdString()));
QString qpattern = QString::fromUtf8( filePattern.c_str() );
std::string ext = QtCompat::removeFileExtension(qpattern).toLower().toStdString();
std::string pluginID;
if ( !ext.empty() ) {
pluginID = appPTR->getWriterPluginIDForFileType(ext);
IOPluginSetForFormat writersForFormat;
appPTR->getWritersForFormat(ext, &writersForFormat);
// Reverse it so that we sort them by decreasing score order
for (IOPluginSetForFormat::reverse_iterator it = writersForFormat.rbegin(); it != writersForFormat.rend(); ++it) {
Plugin* plugin = appPTR->getPluginBinary(QString::fromUtf8( it->pluginID.c_str() ), -1, -1, false);
std::stringstream ss;
ss << "Use " << plugin->getPluginLabel().toStdString() << " version ";
ss << plugin->getMajorVersion() << "." << plugin->getMinorVersion();
ss << " to write this file format";
entries.push_back( ChoiceOption(plugin->getPluginID().toStdString(), "", ss.str()));
}
}
KnobChoicePtr pluginChoice = pluginSelectorKnob.lock();
pluginChoice->populateChoices(entries);
pluginChoice->blockValueChanges();
pluginChoice->resetToDefaultValue(0);
pluginChoice->unblockValueChanges();
if (entries.size() <= 2) {
pluginChoice->setSecret(true);
} else {
pluginChoice->setSecret(false);
}
KnobStringPtr pluginIDKnob = pluginIDStringKnob.lock();
pluginIDKnob->blockValueChanges();
pluginIDKnob->setValue(pluginID);
pluginIDKnob->unblockValueChanges();
}
bool
WriteNode::isWriter() const
{
return true;
}
// static
bool
WriteNode::isVideoWriter(const std::string& pluginID)
{
return (pluginID == PLUGINID_OFX_WRITEFFMPEG);
}
bool
WriteNode::isVideoWriter() const
{
NodePtr p = _imp->embeddedPlugin.lock();
return p ? isVideoWriter( p->getPluginID() ) : false;
}
bool
WriteNode::isGenerator() const
{
return false;
}
bool
WriteNode::isOutput() const
{
return true;
}
bool
WriteNode::getCreateChannelSelectorKnob() const
{
return false;
}
bool
WriteNode::isHostChannelSelectorSupported(bool* /*defaultR*/,
bool* /*defaultG*/,
bool* /*defaultB*/,
bool* /*defaultA*/) const
{
return false;
}
int
WriteNode::getMajorVersion() const
{ return 1; }
int
WriteNode::getMinorVersion() const
{ return 0; }
std::string
WriteNode::getPluginID() const
{ return PLUGINID_NATRON_WRITE; }
std::string