NodePtr NodeRegistry::createNode(const string& sType, const py::dict& pyDict)
{
    const NodeDefinition& def = getNodeDef(sType);
    py::dict effParams;
    StylePtr pStyle;
    if (pyDict.has_key("style")) {
        py::object param = pyDict["style"];
        pyDict.attr("__delitem__")("style");
        pStyle = py::extract<StylePtr>(param);
        effParams = pStyle->mergeParams(pyDict);
    } else {
        effParams = pyDict;
    }
    ArgList args(def.getDefaultArgs(), effParams);
    NodeBuilder builder = def.getBuilder();
    NodePtr pNode = builder(args);
    pNode->setTypeInfo(&def);
    pNode->setStyle(pStyle);
    return pNode;
}
Example #2
0
File: Field.cpp Project: woodem/woo
void Node::pyHandleCustomCtorArgs(py::tuple& args, py::dict& kw){
	if(py::len(args)>0){
		if(py::len(args)>2) throw std::runtime_error("Node: only takes 0, 1 or 2 non-keyword arguments ("+to_string(py::len(args))+" given).");
		py::extract<Vector3r> posEx(args[0]);
		if(!posEx.check()) woo::TypeError("Node: first non-keyword argument must be Vector3 (pos)");
		pos=posEx();
		if(py::len(args)==2){
			py::extract<Quaternionr> oriEx(args[1]);
			if(!oriEx.check()) woo::TypeError("Node: second non-keyword argument must be Quaternion (ori)");
			ori=oriEx();
		}
		args=py::tuple(); // clear args
	}
	for(const char* name:{"dem","gl","sparc","clDem"}){
		if(!kw.has_key(name)) continue;
		auto& d=py::extract<shared_ptr<NodeData>>(kw[name])();
		if(d->getterName()!=string(name)) throw std::runtime_error("Node: mismatch passing "+string(name)+"="+d->pyStr()+": shorthand for this type should be "+d->getterName()+" (not "+string(name)+").");
		d->setDataOnNode(*this);
		py::api::delitem(kw,name);
	}
}
// Trajectory conversion utility functions
static inline py::object dictExtract(py::dict d, const string &k) {
  return d.has_key(k) ? d[k] : py::object();
}