| 464 | } |
| 465 | |
| 466 | void |
| 467 | NodeCollection::checkNodeName(const Node* node, |
| 468 | const std::string& baseName, |
| 469 | bool appendDigit, |
| 470 | bool errorIfExists, |
| 471 | std::string* nodeName) |
| 472 | { |
| 473 | if ( baseName.empty() ) { |
| 474 | throw std::runtime_error( tr("Invalid script-name.").toStdString() ); |
| 475 | |
| 476 | return; |
| 477 | } |
| 478 | ///Remove any non alpha-numeric characters from the baseName |
| 479 | std::string cpy = NATRON_PYTHON_NAMESPACE::makeNameScriptFriendly(baseName); |
| 480 | if ( cpy.empty() ) { |
| 481 | throw std::runtime_error( tr("Invalid script-name.").toStdString() ); |
| 482 | |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | ///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 |
| 487 | ///the python attribute will be overwritten. Try to prevent this situation. |
| 488 | NodeGroup* isGroup = dynamic_cast<NodeGroup*>(this); |
| 489 | if (isGroup) { |
| 490 | const KnobsVec& knobs = isGroup->getKnobs(); |
| 491 | for (KnobsVec::const_iterator it = knobs.begin(); it != knobs.end(); ++it) { |
| 492 | if ( (*it)->getName() == cpy ) { |
| 493 | 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() ); |
| 494 | |
| 495 | return; |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | bool foundNodeWithName = false; |
| 500 | int no = 1; |
| 501 | |
| 502 | { |
| 503 | std::stringstream ss; |
| 504 | ss << cpy; |
| 505 | if (appendDigit) { |
| 506 | ss << no; |
| 507 | } |
| 508 | *nodeName = ss.str(); |
| 509 | } |
| 510 | do { |
| 511 | foundNodeWithName = false; |
| 512 | QMutexLocker l(&_imp->nodesMutex); |
| 513 | for (NodesList::iterator it = _imp->nodes.begin(); it != _imp->nodes.end(); ++it) { |
| 514 | if ( (it->get() != node) && (*it)->isActivated() && ( (*it)->getScriptName_mt_safe() == *nodeName ) ) { |
| 515 | foundNodeWithName = true; |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | if (foundNodeWithName) { |
| 520 | if (errorIfExists || !appendDigit) { |
| 521 | throw std::runtime_error( tr("A node with the script-name %1 already exists.").arg( QString::fromUtf8( nodeName->c_str() ) ).toStdString() ); |
| 522 | |
| 523 | return; |
no test coverage detected