forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeGroup.cpp
2911 lines (2538 loc) · 115 KB
/
NodeGroup.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 "NodeGroup.h"
#include <set>
#include <locale>
#include <cfloat>
#include <algorithm> // min, max
#include <cassert>
#include <stdexcept>
#include <sstream> // stringstream
#include <limits>
#include <QtCore/QCoreApplication>
#include <QtCore/QTextStream>
#include "Engine/AppInstance.h"
#include "Engine/Bezier.h"
#include "Engine/BezierCP.h"
#include "Engine/Curve.h"
#include "Engine/GroupInput.h"
#include "Engine/GroupOutput.h"
#include "Engine/Image.h"
#include "Engine/KnobFile.h"
#include "Engine/KnobTypes.h"
#include "Engine/Node.h"
#include "Engine/NodeGraphI.h"
#include "Engine/NodeGuiI.h"
#include "Engine/OutputSchedulerThread.h"
#include "Engine/Plugin.h"
#include "Engine/Project.h"
#include "Engine/PrecompNode.h"
#include "Engine/RotoContext.h"
#include "Engine/RotoLayer.h"
#include "Engine/Settings.h"
#include "Engine/TimeLine.h"
#include "Engine/ViewIdx.h"
#include "Engine/ViewerInstance.h"
#define NATRON_PYPLUG_EXPORTER_VERSION 10
NATRON_NAMESPACE_ENTER
struct NodeCollectionPrivate
{
AppInstanceWPtr app;
NodeGraphI* graph;
mutable QMutex nodesMutex;
NodesList nodes;
NodeCollectionPrivate(const AppInstancePtr& app)
: app(app)
, graph(0)
, nodesMutex()
, nodes()
{
}
NodePtr findNodeInternal(const std::string& name, const std::string& recurseName) const;
};
NodeCollection::NodeCollection(const AppInstancePtr& app)
: _imp( new NodeCollectionPrivate(app) )
{
}
NodeCollection::~NodeCollection()
{
}
AppInstancePtr
NodeCollection::getApplication() const
{
return _imp->app.lock();
}
void
NodeCollection::setNodeGraphPointer(NodeGraphI* graph)
{
_imp->graph = graph;
}
void
NodeCollection::discardNodeGraphPointer()
{
_imp->graph = 0;
}
NodeGraphI*
NodeCollection::getNodeGraph() const
{
return _imp->graph;
}
NodesList
NodeCollection::getNodes() const
{
QMutexLocker k(&_imp->nodesMutex);
return _imp->nodes;
}
void
NodeCollection::getNodes_recursive(NodesList& nodes,
bool onlyActive) const
{
std::list<NodeGroup*> groupToRecurse;
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::const_iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) {
if ( onlyActive && !(*it)->isActivated() ) {
continue;
}
nodes.push_back(*it);
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
groupToRecurse.push_back(isGrp);
}
}
}
for (std::list<NodeGroup*>::const_iterator it = groupToRecurse.begin(); it != groupToRecurse.end(); ++it) {
(*it)->getNodes_recursive(nodes, onlyActive);
}
}
void
NodeCollection::addNode(const NodePtr& node)
{
{
QMutexLocker k(&_imp->nodesMutex);
_imp->nodes.push_back(node);
}
}
void
NodeCollection::removeNode(const Node* node)
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::iterator it =_imp->nodes.begin(); it != _imp->nodes.end();++it) {
if ( it->get() == node ) {
_imp->nodes.erase(it);
break;
}
}
}
NodePtr
NodeCollection::getLastNode(const std::string& pluginID) const
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::reverse_iterator it = _imp->nodes.rbegin(); it != _imp->nodes.rend(); ++it) {
if ( (*it)->getPluginID() == pluginID ) {
return *it;
}
}
return NodePtr();
}
bool
NodeCollection::hasNodes() const
{
QMutexLocker k(&_imp->nodesMutex);
return _imp->nodes.size() > 0;
}
void
NodeCollection::getActiveNodes(NodesList* nodes) const
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) {
if ( (*it)->isActivated() ) {
nodes->push_back(*it);
}
}
}
void
NodeCollection::getActiveNodesExpandGroups(NodesList* nodes) const
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) {
if ( (*it)->isActivated() ) {
nodes->push_back(*it);
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
isGrp->getActiveNodesExpandGroups(nodes);
}
}
}
}
void
NodeCollection::getViewers(std::list<ViewerInstance*>* viewers) const
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) {
ViewerInstance* isViewer = (*it)->isEffectViewer();
if (isViewer) {
viewers->push_back(isViewer);
}
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
isGrp->getViewers(viewers);
}
}
}
void
NodeCollection::getWriters(std::list<OutputEffectInstance*>* writers) const
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) {
if ( (*it)->getGroup() && (*it)->isActivated() && (*it)->getEffectInstance()->isWriter() && (*it)->isPartOfProject() ) {
OutputEffectInstance* out = dynamic_cast<OutputEffectInstance*>( (*it)->getEffectInstance().get() );
assert(out);
writers->push_back(out);
}
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
isGrp->getWriters(writers);
}
}
}
void
NodeCollection::quitAnyProcessingInternal(bool blocking)
{
NodesList nodes = getNodes();
for (NodesList::iterator it = nodes.begin(); it != nodes.end(); ++it) {
if (blocking) {
(*it)->quitAnyProcessing_blocking(true);
} else {
(*it)->quitAnyProcessing_non_blocking();
}
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
isGrp->quitAnyProcessingInternal(blocking);
}
PrecompNode* isPrecomp = dynamic_cast<PrecompNode*>( (*it)->getEffectInstance().get() );
if (isPrecomp) {
isPrecomp->getPrecompApp()->getProject()->quitAnyProcessingInternal(blocking);
}
}
}
void
NodeCollection::quitAnyProcessingForAllNodes_blocking()
{
quitAnyProcessingInternal(true);
}
void
NodeCollection::quitAnyProcessingForAllNodes_non_blocking()
{
quitAnyProcessingInternal(false);
}
bool
NodeCollection::isCacheIDAlreadyTaken(const std::string& name) const
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) {
if ( (*it)->getCacheID() == name ) {
return true;
}
}
return false;
}
bool
NodeCollection::hasNodeRendering() const
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) {
if ( (*it)->isOutputNode() ) {
NodeGroup* isGrp = (*it)->isEffectGroup();
PrecompNode* isPrecomp = dynamic_cast<PrecompNode*>( (*it)->getEffectInstance().get() );
if (isGrp) {
if ( isGrp->hasNodeRendering() ) {
return true;
}
} else if (isPrecomp) {
if ( isPrecomp->getPrecompApp()->getProject()->hasNodeRendering() ) {
return true;
}
} else {
OutputEffectInstance* effect = dynamic_cast<OutputEffectInstance*>( (*it)->getEffectInstance().get() );
if ( effect && effect->getRenderEngine()->hasThreadsWorking() ) {
return true;
}
}
}
}
return false;
}
void
NodeCollection::refreshViewersAndPreviews()
{
assert( QThread::currentThread() == qApp->thread() );
AppInstancePtr appInst = getApplication();
if (!appInst) {
return;
}
if ( !appInst->isBackground() ) {
NodesList nodes = getNodes();
for (NodesList::iterator it = nodes.begin(); it != nodes.end(); ++it) {
assert(*it);
(*it)->refreshPreviewsAfterProjectLoad();
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
isGrp->refreshViewersAndPreviews();
} else {
ViewerInstance* n = (*it)->isEffectViewer();
if (n) {
n->renderCurrentFrame(true);
}
}
}
}
}
void
NodeCollection::refreshPreviews()
{
AppInstancePtr appInst = getApplication();
if (!appInst) {
return;
}
if ( appInst->isBackground() ) {
return;
}
double time = appInst->getTimeLine()->currentFrame();
NodesList nodes;
getActiveNodes(&nodes);
for (NodesList::iterator it = nodes.begin(); it != nodes.end(); ++it) {
if ( (*it)->isPreviewEnabled() ) {
(*it)->refreshPreviewImage(time);
}
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
isGrp->refreshPreviews();
}
}
}
void
NodeCollection::forceRefreshPreviews()
{
AppInstancePtr appInst = getApplication();
if (!appInst) {
return;
}
if ( appInst->isBackground() ) {
return;
}
double time = appInst->getTimeLine()->currentFrame();
NodesList nodes;
getActiveNodes(&nodes);
for (NodesList::iterator it = nodes.begin(); it != nodes.end(); ++it) {
if ( (*it)->isPreviewEnabled() ) {
(*it)->computePreviewImage(time);
}
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
isGrp->forceRefreshPreviews();
}
}
}
void
NodeCollection::clearNodesInternal(bool blocking)
{
NodesList nodesToDelete;
{
QMutexLocker l(&_imp->nodesMutex);
nodesToDelete = _imp->nodes;
}
///Clear recursively containers inside this group
for (NodesList::iterator it = nodesToDelete.begin(); it != nodesToDelete.end(); ++it) {
// You should have called quitAnyProcessing before!
assert( !(*it)->isNodeRendering() );
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
isGrp->clearNodesInternal(blocking);
}
PrecompNode* isPrecomp = dynamic_cast<PrecompNode*>( (*it)->getEffectInstance().get() );
if (isPrecomp) {
isPrecomp->getPrecompApp()->getProject()->clearNodesInternal(blocking);
}
}
///Kill effects
for (NodesList::iterator it = nodesToDelete.begin(); it != nodesToDelete.end(); ++it) {
(*it)->destroyNode(blocking, false);
}
if (_imp->graph) {
_imp->graph->onNodesCleared();
}
{
QMutexLocker l(&_imp->nodesMutex);
_imp->nodes.clear();
}
nodesToDelete.clear();
}
void
NodeCollection::clearNodesBlocking()
{
quitAnyProcessingForAllNodes_blocking();
clearNodesInternal(true);
}
void
NodeCollection::clearNodesNonBlocking()
{
clearNodesInternal(false);
}
void
NodeCollection::checkNodeName(const Node* node,
const std::string& baseName,
bool appendDigit,
bool errorIfExists,
std::string* nodeName)
{
if ( baseName.empty() ) {
throw std::runtime_error( tr("Invalid script-name.").toStdString() );
return;
}
///Remove any non alpha-numeric characters from the baseName
std::string cpy = NATRON_PYTHON_NAMESPACE::makeNameScriptFriendly(baseName);
if ( cpy.empty() ) {
throw std::runtime_error( tr("Invalid script-name.").toStdString() );
return;
}
///If this is a group and one of its parameter has the same script-name as the script-name of one of the node inside
///the python attribute will be overwritten. Try to prevent this situation.
NodeGroup* isGroup = dynamic_cast<NodeGroup*>(this);
if (isGroup) {
const KnobsVec& knobs = isGroup->getKnobs();
for (KnobsVec::const_iterator it = knobs.begin(); it != knobs.end(); ++it) {
if ( (*it)->getName() == cpy ) {
throw std::runtime_error( tr("A node within a group cannot have the same script-name (%1) as a parameter on the group for scripting purposes.").arg( QString::fromUtf8( cpy.c_str() ) ).toStdString() );
return;
}
}
}
bool foundNodeWithName = false;
int no = 1;
{
std::stringstream ss;
ss << cpy;
if (appendDigit) {
ss << no;
}
*nodeName = ss.str();
}
do {
foundNodeWithName = false;
QMutexLocker l(&_imp->nodesMutex);
for (NodesList::iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) {
if ( (it->get() != node) && (*it)->isActivated() && ( (*it)->getScriptName_mt_safe() == *nodeName ) ) {
foundNodeWithName = true;
break;
}
}
if (foundNodeWithName) {
if (errorIfExists || !appendDigit) {
throw std::runtime_error( tr("A node with the script-name %1 already exists.").arg( QString::fromUtf8( nodeName->c_str() ) ).toStdString() );
return;
}
++no;
{
std::stringstream ss;
ss << cpy << no;
*nodeName = ss.str();
}
}
} while (foundNodeWithName);
} // NodeCollection::checkNodeName
void
NodeCollection::initNodeName(const std::string& pluginLabel,
std::string* nodeName)
{
std::string baseName(pluginLabel);
if ( (baseName.size() > 3) &&
( baseName[baseName.size() - 1] == 'X') &&
( baseName[baseName.size() - 2] == 'F') &&
( baseName[baseName.size() - 3] == 'O') ) {
baseName = baseName.substr(0, baseName.size() - 3);
}
checkNodeName(0, baseName, true, false, nodeName);
}
bool
NodeCollection::connectNodes(int inputNumber,
const NodePtr& input,
const NodePtr& output,
bool force)
{
////Only called by the main-thread
assert( QThread::currentThread() == qApp->thread() );
NodePtr existingInput = output->getRealInput(inputNumber);
if (force && existingInput) {
bool ok = disconnectNodes(existingInput, output);
if (!ok) {
return false;
}
if ( input && (input->getNInputs() > 0) ) {
ok = connectNodes(input->getPreferredInputForConnection(), existingInput, input);
if (!ok) {
return false;
}
}
}
if (!input) {
return true;
}
Node::CanConnectInputReturnValue ret = output->canConnectInput(input, inputNumber);
bool connectionOk = ret == Node::eCanConnectInput_ok ||
ret == Node::eCanConnectInput_differentFPS ||
ret == Node::eCanConnectInput_differentPars ||
ret == Node::eCanConnectInput_multiResNotSupported;
if (ret == Node::eCanConnectInput_multiResNotSupported) {
LogEntry::LogEntryColor c;
if (output->getColor(&c.r, &c.g, &c.b)) {
c.colorSet = true;
}
QString err = tr("Warning: %1 does not support inputs of different sizes but its inputs produce different output size. Please check this.").arg( QString::fromUtf8( output->getScriptName().c_str() ) );
appPTR->writeToErrorLog_mt_safe(QString::fromUtf8( output->getScriptName().c_str() ) , QDateTime::currentDateTime(), err, false, c);
}
if ( !connectionOk || !output->connectInput(input, inputNumber) ) {
return false;
}
return true;
}
bool
NodeCollection::connectNodes(int inputNumber,
const std::string & inputName,
const NodePtr& output)
{
NodesList nodes = getNodes();
for (NodesList::iterator it = nodes.begin(); it != nodes.end(); ++it) {
assert(*it);
if ( (*it)->getScriptName() == inputName ) {
return connectNodes(inputNumber, *it, output);
}
}
return false;
}
bool
NodeCollection::disconnectNodes(const NodePtr& input,
const NodePtr& output,
bool autoReconnect)
{
NodePtr inputToReconnectTo;
int indexOfInput = output->inputIndex( input );
if (indexOfInput == -1) {
return false;
}
int inputsCount = input->getNInputs();
if (inputsCount == 1) {
inputToReconnectTo = input->getInput(0);
}
if (output->disconnectInput( input.get() ) < 0) {
return false;
}
if (autoReconnect && inputToReconnectTo) {
connectNodes(indexOfInput, inputToReconnectTo, output);
}
return true;
}
bool
NodeCollection::autoConnectNodes(const NodePtr& selected,
const NodePtr& created)
{
///We follow this rule:
// 1) selected is output
// a) created is output --> fail
// b) created is input --> connect input
// c) created is regular --> connect input
// 2) selected is input
// a) created is output --> connect output
// b) created is input --> fail
// c) created is regular --> connect output
// 3) selected is regular
// a) created is output--> connect output
// b) created is input --> connect input
// c) created is regular --> connect output
///if true if will connect 'created' as input of 'selected',
///otherwise as output.
bool connectAsInput = false;
///cannot connect 2 input nodes together: case 2-b)
if ( (selected->getNInputs() == 0) && (created->getNInputs() == 0) ) {
return false;
}
///cannot connect 2 output nodes together: case 1-a)
if ( selected->isOutputNode() && created->isOutputNode() ) {
return false;
}
///1)
if ( selected->isOutputNode() ) {
///assert we're not in 1-a)
assert( !created->isOutputNode() );
///for either cases 1-b) or 1-c) we just connect the created node as input of the selected node.
connectAsInput = true;
}
///2) and 3) are similar exceptfor case b)
else {
///case 2 or 3- a): connect the created node as output of the selected node.
if ( created->isOutputNode() ) {
connectAsInput = false;
}
///case b)
else if (created->getNInputs() == 0) {
assert(selected->getNInputs() != 0);
///case 3-b): connect the created node as input of the selected node
connectAsInput = true;
}
///case c) connect created as output of the selected node
else {
connectAsInput = false;
}
}
bool ret = false;
if (connectAsInput) {
///connect it to the first input
int selectedInput = selected->getPreferredInputForConnection();
if (selectedInput != -1) {
bool ok = connectNodes(selectedInput, created, selected, true);
assert(ok);
Q_UNUSED(ok);
ret = true;
} else {
ret = false;
}
} else {
if ( !created->isOutputNode() ) {
///we find all the nodes that were previously connected to the selected node,
///and connect them to the created node instead.
std::map<NodePtr, int> outputsConnectedToSelectedNode;
selected->getOutputsConnectedToThisNode(&outputsConnectedToSelectedNode);
for (std::map<NodePtr, int>::iterator it = outputsConnectedToSelectedNode.begin();
it != outputsConnectedToSelectedNode.end(); ++it) {
if ( it->first->getParentMultiInstanceName().empty() ) {
bool ok = disconnectNodes(selected, it->first);
assert(ok);
ok = connectNodes(it->second, created, it->first);
Q_UNUSED(ok);
//assert(ok); Might not be ok if the disconnectNodes() action above was queued
}
}
}
///finally we connect the created node to the selected node
int createdInput = created->getPreferredInputForConnection();
if (createdInput != -1) {
bool ok = connectNodes(createdInput, selected, created);
assert(ok);
Q_UNUSED(ok);
ret = true;
} else {
ret = false;
}
}
///update the render trees
std::list<ViewerInstance* > viewers;
created->hasViewersConnected(&viewers);
for (std::list<ViewerInstance* >::iterator it = viewers.begin(); it != viewers.end(); ++it) {
(*it)->renderCurrentFrame(true);
}
return ret;
} // autoConnectNodes
NodePtr
NodeCollectionPrivate::findNodeInternal(const std::string& name,
const std::string& recurseName) const
{
QMutexLocker k(&nodesMutex);
for (NodesList::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
if ( (*it)->isActivated() && (*it)->getScriptName_mt_safe() == name ) {
if ( !recurseName.empty() ) {
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
return isGrp->getNodeByFullySpecifiedName(recurseName);
} else {
NodesList children;
(*it)->getChildrenMultiInstance(&children);
for (NodesList::iterator it2 = children.begin(); it2 != children.end(); ++it2) {
if ( (*it2)->isActivated() && (*it2)->getScriptName_mt_safe() == recurseName ) {
return *it2;
}
}
}
} else {
return *it;
}
}
}
return NodePtr();
}
NodePtr
NodeCollection::getNodeByName(const std::string & name) const
{
return _imp->findNodeInternal( name, std::string() );
}
void
NodeCollection::getNodeNameAndRemainder_LeftToRight(const std::string& fullySpecifiedName,
std::string& name,
std::string& remainder)
{
std::size_t foundDot = fullySpecifiedName.find_first_of('.');
if (foundDot != std::string::npos) {
name = fullySpecifiedName.substr(0, foundDot);
if ( foundDot + 1 < fullySpecifiedName.size() ) {
remainder = fullySpecifiedName.substr(foundDot + 1, std::string::npos);
}
} else {
name = fullySpecifiedName;
}
}
void
NodeCollection::getNodeNameAndRemainder_RightToLeft(const std::string& fullySpecifiedName,
std::string& name,
std::string& remainder)
{
std::size_t foundDot = fullySpecifiedName.find_last_of('.');
if (foundDot != std::string::npos) {
name = fullySpecifiedName.substr(foundDot + 1, std::string::npos);
if (foundDot > 0) {
remainder = fullySpecifiedName.substr(0, foundDot - 1);
}
} else {
name = fullySpecifiedName;
}
}
NodePtr
NodeCollection::getNodeByFullySpecifiedName(const std::string& fullySpecifiedName) const
{
std::string toFind;
std::string recurseName;
getNodeNameAndRemainder_LeftToRight(fullySpecifiedName, toFind, recurseName);
return _imp->findNodeInternal(toFind, recurseName);
}
void
NodeCollection::fixRelativeFilePaths(const std::string& projectPathName,
const std::string& newProjectPath,
bool blockEval)
{
NodesList nodes = getNodes();
AppInstancePtr appInst = getApplication();
if (!appInst) {
return;
}
ProjectPtr project = appInst->getProject();
for (NodesList::iterator it = nodes.begin(); it != nodes.end(); ++it) {
if ( (*it)->isActivated() ) {
(*it)->getEffectInstance()->beginChanges();
const KnobsVec& knobs = (*it)->getKnobs();
for (U32 j = 0; j < knobs.size(); ++j) {
KnobStringBase* isString = dynamic_cast< KnobStringBase* >( knobs[j].get() );
KnobString* isStringKnob = dynamic_cast<KnobString*>(isString);
if ( !isString || isStringKnob || ( knobs[j] == project->getEnvVarKnob() ) ) {
continue;
}
std::string filepath = isString->getValue();
if ( !filepath.empty() ) {
if ( project->fixFilePath(projectPathName, newProjectPath, filepath) ) {
isString->setValue(filepath);
}
}
}
(*it)->getEffectInstance()->endChanges(blockEval);
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
isGrp->fixRelativeFilePaths(projectPathName, newProjectPath, blockEval);
}
}
}
}
void
NodeCollection::fixPathName(const std::string& oldName,
const std::string& newName)
{
NodesList nodes = getNodes();
AppInstancePtr appInst = getApplication();
if (!appInst) {
return;
}
ProjectPtr project = appInst->getProject();
for (NodesList::iterator it = nodes.begin(); it != nodes.end(); ++it) {
if ( (*it)->isActivated() ) {
const KnobsVec& knobs = (*it)->getKnobs();
for (U32 j = 0; j < knobs.size(); ++j) {
KnobStringBase* isString = dynamic_cast< KnobStringBase* >( knobs[j].get() );
KnobString* isStringKnob = dynamic_cast<KnobString*>(isString);
if ( !isString || isStringKnob || ( knobs[j] == project->getEnvVarKnob() ) ) {
continue;
}
std::string filepath = isString->getValue();
if ( ( filepath.size() >= (oldName.size() + 2) ) &&
( filepath[0] == '[') &&
( filepath[oldName.size() + 1] == ']') &&
( filepath.substr( 1, oldName.size() ) == oldName) ) {
filepath.replace(1, oldName.size(), newName);
isString->setValue(filepath);
}
}
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
isGrp->fixPathName(oldName, newName);
}
}
}
}
bool
NodeCollection::checkIfNodeLabelExists(const std::string & n,
const Node* caller) const
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::const_iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) {
if ( (it->get() != caller) && (*it)->isActivated() && ( (*it)->getLabel_mt_safe() == n ) ) {
return true;
}
}
return false;
}
bool
NodeCollection::checkIfNodeNameExists(const std::string & n,
const Node* caller) const
{
QMutexLocker k(&_imp->nodesMutex);
for (NodesList::const_iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) {
if ( (it->get() != caller) && (*it)->isActivated() && ( (*it)->getScriptName_mt_safe() == n ) ) {
return true;
}
}
return false;
}
static void
recomputeFrameRangeForAllReadersInternal(NodeCollection* group,
int* firstFrame,
int* lastFrame,
bool setFrameRange)
{
NodesList nodes = group->getNodes();
for (NodesList::iterator it = nodes.begin(); it != nodes.end(); ++it) {
if ( (*it)->isActivated() ) {
if ( (*it)->getEffectInstance()->isReader() ) {
double thisFirst, thislast;
(*it)->getEffectInstance()->getFrameRange_public( (*it)->getHashValue(), &thisFirst, &thislast );
if (thisFirst != INT_MIN) {
*firstFrame = setFrameRange ? thisFirst : std::min(*firstFrame, (int)thisFirst);
}
if (thislast != INT_MAX) {
*lastFrame = setFrameRange ? thislast : std::max(*lastFrame, (int)thislast);
}
} else {
NodeGroup* isGrp = (*it)->isEffectGroup();
if (isGrp) {
recomputeFrameRangeForAllReadersInternal(isGrp, firstFrame, lastFrame, false);
}
}
}
}
}
void
NodeCollection::recomputeFrameRangeForAllReaders(int* firstFrame,
int* lastFrame)
{
recomputeFrameRangeForAllReadersInternal(this, firstFrame, lastFrame, true);
}
void
NodeCollection::forceComputeInputDependentDataOnAllTrees()
{
NodesList nodes;
getNodes_recursive(nodes, true);
std::list<Project::NodesTree> trees;
Project::extractTreesFromNodes(nodes, trees);
for (NodesList::iterator it = nodes.begin(); it != nodes.end(); ++it) {
(*it)->markAllInputRelatedDataDirty();
}
std::list<Node*> markedNodes;
for (std::list<Project::NodesTree>::iterator it = trees.begin(); it != trees.end(); ++it) {