void Renderer::pyHandleCustomCtorArgs(py::tuple& args, py::dict& kw){ if(py::len(kw)>0){ string keys; py::list k(kw.keys()); for(int i=0; i<py::len(k); i++) keys+=(i>0?", ":"")+py::extract<string>(k[i])(); Master().instance().checkApi(10102,"Constructing Renderer with keywords ("+keys+") will have no effect unless passed to GlSetup/S.gl.",/*pyWarn*/true); } }
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; }
ArgList::ArgList(const ArgList& argTemplates, const py::dict& PyDict) { // TODO: Check if all required args are being set. copyArgsFrom(argTemplates); py::list keys = PyDict.keys(); int nKeys = py::len(keys); for (int i = 0; i < nKeys; i++) { py::object keyObj = keys[i]; py::object valObj = PyDict[keyObj]; py::extract<string> keyStrProxy(keyObj); if (!keyStrProxy.check()) { throw Exception(AVG_ERR_INVALID_ARGS, "Argument name must be a string."); } string keyStr = keyStrProxy(); setArgValue(keyStr, valObj); } }
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); } }
void GlSetup::pyHandleCustomCtorArgs(py::tuple& args, py::dict& kw){ pyCall(args); args=py::tuple(); // not sure this is really useful py::list kwl=kw.items(); for(int i=0; i<py::len(kwl); i++){ py::tuple item=py::extract<py::tuple>(kwl[i]); string key=py::extract<string>(item[0]); auto I=std::find(objAccessorNames.begin(),objAccessorNames.end(),key); // we don't consume unknown keys, Object's business to use them or error out if(I==objAccessorNames.end()) continue; size_t index=I-objAccessorNames.begin(); py::extract<shared_ptr<Object>> ex(item[1]); if(!ex.check()) woo::TypeError(key+" must be a "+objTypeNames[index]+"."); const auto o(ex()); const auto oPtr=o.get(); if(!o) woo::TypeError(key+" must not be None."); if(std::type_index(typeid(*oPtr))!=objTypeIndices[index]) woo::TypeError(key+" must be a "+objTypeNames[index]+" (not "+o->getClassName()+")."); objs[index]=o; py::api::delitem(kw,key.c_str()); } }
// Trajectory conversion utility functions static inline py::object dictExtract(py::dict d, const string &k) { return d.has_key(k) ? d[k] : py::object(); }