| 2405 | } |
| 2406 | |
| 2407 | std::string |
| 2408 | KnobHelperPrivate::declarePythonVariables(bool addTab, |
| 2409 | int dim) |
| 2410 | { |
| 2411 | if (!holder) { |
| 2412 | throw std::runtime_error("This parameter cannot have an expression"); |
| 2413 | } |
| 2414 | |
| 2415 | std::string appID = holder->getApp()->getAppIDString(); |
| 2416 | std::string tabStr = addTab ? " " : ""; |
| 2417 | EffectInstance* effect = dynamic_cast<EffectInstance*>(holder); |
| 2418 | NodeCollectionPtr collection; |
| 2419 | NodeGroup* isParentGrp = nullptr; |
| 2420 | if (effect) { |
| 2421 | NodePtr node = effect->getNode(); |
| 2422 | assert(node); |
| 2423 | |
| 2424 | collection = node->getGroup(); |
| 2425 | if (!collection) { |
| 2426 | throw std::runtime_error("This parameter cannot have an expression"); |
| 2427 | } |
| 2428 | isParentGrp = dynamic_cast<NodeGroup*>( collection.get() ); |
| 2429 | } |
| 2430 | std::stringstream ss; |
| 2431 | if (appID != "app") { |
| 2432 | ss << tabStr << "app = " << appID << "\n"; |
| 2433 | } |
| 2434 | |
| 2435 | |
| 2436 | //Define all nodes reachable through expressions in the scope |
| 2437 | |
| 2438 | |
| 2439 | //Define all nodes in the same group reachable by their bare script-name |
| 2440 | std::string collectionScriptName; |
| 2441 | if (isParentGrp) { |
| 2442 | collectionScriptName = isParentGrp->getNode()->getFullyQualifiedName(); |
| 2443 | } else { |
| 2444 | collectionScriptName = appID; |
| 2445 | } |
| 2446 | if (collection) { |
| 2447 | NodesList siblings = collection->getNodes(); |
| 2448 | for (NodesList::iterator it = siblings.begin(); it != siblings.end(); ++it) { |
| 2449 | if ( (*it)->isActivated() && !(*it)->getParentMultiInstance() ) { |
| 2450 | std::string scriptName = (*it)->getScriptName_mt_safe(); |
| 2451 | if ( NATRON_PYTHON_NAMESPACE::isKeyword(scriptName) ) { |
| 2452 | throw std::runtime_error(scriptName + " is a Python keyword"); |
| 2453 | } |
| 2454 | std::string fullName = appID + "." + (*it)->getFullyQualifiedName(); |
| 2455 | ss << tabStr << "if hasattr("; |
| 2456 | if (isParentGrp) { |
| 2457 | ss << appID << "."; |
| 2458 | } |
| 2459 | ss << collectionScriptName << ",\"" << scriptName << "\"):\n"; |
| 2460 | |
| 2461 | ss << tabStr << " " << scriptName << " = " << fullName << "\n"; |
| 2462 | } |
| 2463 | } |
| 2464 | } |
no test coverage detected