Skip to content

Commit f4479d6

Browse files
committed
Merge pull request #27 from lchai/getVar
added getVar function
2 parents 2c8656e + 7724bd9 commit f4479d6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/SeExpr/SeExprBuiltins.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,54 @@ static const char* vnoise_docstring=
14731473
"0 - none, 1 - linear, 2 - smooth, 3 - spline, \n"
14741474
"4 - monotone (non oscillating spline)";
14751475

1476+
class GetVarFunc:public SeExprFuncX
1477+
{
1478+
struct Data:public SeExprFuncNode::Data
1479+
{
1480+
Data(SeExprVarNode* varNode) : varNode(varNode) {}
1481+
~Data() { delete varNode; }
1482+
1483+
SeExprVarNode* varNode;
1484+
};
1485+
1486+
bool prep(SeExprFuncNode* node,bool wantVec)
1487+
{
1488+
if (!node->isStrArg(0)) {
1489+
node->child(1)->addError("First argument must be a string");
1490+
return false;
1491+
}
1492+
1493+
const SeExpression* expr = node->expr();
1494+
std::string varName = node->getStrArg(0);
1495+
SeExprVarNode* varNode = new SeExprVarNode(expr,varName.c_str());
1496+
if (varNode->prep(wantVec)) {
1497+
node->setData(new Data(varNode));
1498+
} else {
1499+
delete varNode;
1500+
}
1501+
1502+
return node->child(1)->prep(wantVec);
1503+
}
1504+
1505+
void eval(const SeExprFuncNode* node,SeVec3d& result) const
1506+
{
1507+
Data* data=static_cast<Data*>(node->getData());
1508+
if (data) {
1509+
data->varNode->eval(result);
1510+
} else {
1511+
SeVec3d* args=node->evalArgs();
1512+
result = args[1];
1513+
}
1514+
}
1515+
1516+
public:
1517+
GetVarFunc() : SeExprFuncX(true) {}
1518+
virtual ~GetVarFunc() {}
1519+
} getVar;
1520+
static const char *getVar_docstring=
1521+
"getVar(string varName,vector defaultValue)\n"
1522+
"return varName if variable exists, otherwise return defaultValue";
1523+
14761524
class PrintFuncX:public SeExprFuncX
14771525
{
14781526
struct Data : public SeExprFuncNode::Data
@@ -1714,6 +1762,7 @@ static const char* vnoise_docstring=
17141762
FUNCNDOC(curve, 1, -1);
17151763
FUNCNDOC(ccurve, 1, -1);
17161764
FUNCNDOC(swatch, 3, -1);
1765+
FUNCNDOC(getVar,2,2);
17171766
FUNCNDOC(printf,1,-1);
17181767

17191768
}

0 commit comments

Comments
 (0)