node_xml.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright 2011-2016 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "graph/node_xml.h"
  17. #include "util/util_foreach.h"
  18. #include "util/util_string.h"
  19. #include "util/util_transform.h"
  20. CCL_NAMESPACE_BEGIN
  21. static bool xml_read_boolean(const char *value)
  22. {
  23. return string_iequals(value, "true") || (atoi(value) != 0);
  24. }
  25. static const char *xml_write_boolean(bool value)
  26. {
  27. return (value) ? "true" : "false";
  28. }
  29. template<int VECTOR_SIZE, typename T>
  30. static void xml_read_float_array(T &value, xml_attribute attr)
  31. {
  32. vector<string> tokens;
  33. string_split(tokens, attr.value());
  34. if (tokens.size() % VECTOR_SIZE != 0) {
  35. return;
  36. }
  37. value.resize(tokens.size() / VECTOR_SIZE);
  38. for (size_t i = 0; i < value.size(); i++) {
  39. float *value_float = (float *)&value[i];
  40. for (size_t j = 0; j < VECTOR_SIZE; j++)
  41. value_float[j] = (float)atof(tokens[i * VECTOR_SIZE + j].c_str());
  42. }
  43. }
  44. void xml_read_node(XMLReader &reader, Node *node, xml_node xml_node)
  45. {
  46. xml_attribute name_attr = xml_node.attribute("name");
  47. if (name_attr) {
  48. node->name = ustring(name_attr.value());
  49. }
  50. foreach (const SocketType &socket, node->type->inputs) {
  51. if (socket.type == SocketType::CLOSURE || socket.type == SocketType::UNDEFINED) {
  52. continue;
  53. }
  54. if (socket.flags & SocketType::INTERNAL) {
  55. continue;
  56. }
  57. xml_attribute attr = xml_node.attribute(socket.name.c_str());
  58. if (!attr) {
  59. continue;
  60. }
  61. switch (socket.type) {
  62. case SocketType::BOOLEAN: {
  63. node->set(socket, xml_read_boolean(attr.value()));
  64. break;
  65. }
  66. case SocketType::BOOLEAN_ARRAY: {
  67. vector<string> tokens;
  68. string_split(tokens, attr.value());
  69. array<bool> value;
  70. value.resize(tokens.size());
  71. for (size_t i = 0; i < value.size(); i++)
  72. value[i] = xml_read_boolean(tokens[i].c_str());
  73. node->set(socket, value);
  74. break;
  75. }
  76. case SocketType::FLOAT: {
  77. node->set(socket, (float)atof(attr.value()));
  78. break;
  79. }
  80. case SocketType::FLOAT_ARRAY: {
  81. array<float> value;
  82. xml_read_float_array<1>(value, attr);
  83. node->set(socket, value);
  84. break;
  85. }
  86. case SocketType::INT: {
  87. node->set(socket, (int)atoi(attr.value()));
  88. break;
  89. }
  90. case SocketType::UINT: {
  91. node->set(socket, (uint)atoi(attr.value()));
  92. break;
  93. }
  94. case SocketType::INT_ARRAY: {
  95. vector<string> tokens;
  96. string_split(tokens, attr.value());
  97. array<int> value;
  98. value.resize(tokens.size());
  99. for (size_t i = 0; i < value.size(); i++) {
  100. value[i] = (int)atoi(attr.value());
  101. }
  102. node->set(socket, value);
  103. break;
  104. }
  105. case SocketType::COLOR:
  106. case SocketType::VECTOR:
  107. case SocketType::POINT:
  108. case SocketType::NORMAL: {
  109. array<float3> value;
  110. xml_read_float_array<3>(value, attr);
  111. if (value.size() == 1) {
  112. node->set(socket, value[0]);
  113. }
  114. break;
  115. }
  116. case SocketType::COLOR_ARRAY:
  117. case SocketType::VECTOR_ARRAY:
  118. case SocketType::POINT_ARRAY:
  119. case SocketType::NORMAL_ARRAY: {
  120. array<float3> value;
  121. xml_read_float_array<3>(value, attr);
  122. node->set(socket, value);
  123. break;
  124. }
  125. case SocketType::POINT2: {
  126. array<float2> value;
  127. xml_read_float_array<2>(value, attr);
  128. if (value.size() == 1) {
  129. node->set(socket, value[0]);
  130. }
  131. break;
  132. }
  133. case SocketType::POINT2_ARRAY: {
  134. array<float2> value;
  135. xml_read_float_array<2>(value, attr);
  136. node->set(socket, value);
  137. break;
  138. }
  139. case SocketType::STRING: {
  140. node->set(socket, attr.value());
  141. break;
  142. }
  143. case SocketType::ENUM: {
  144. ustring value(attr.value());
  145. if (socket.enum_values->exists(value)) {
  146. node->set(socket, value);
  147. }
  148. else {
  149. fprintf(stderr,
  150. "Unknown value \"%s\" for attribute \"%s\".\n",
  151. value.c_str(),
  152. socket.name.c_str());
  153. }
  154. break;
  155. }
  156. case SocketType::STRING_ARRAY: {
  157. vector<string> tokens;
  158. string_split(tokens, attr.value());
  159. array<ustring> value;
  160. value.resize(tokens.size());
  161. for (size_t i = 0; i < value.size(); i++) {
  162. value[i] = ustring(tokens[i]);
  163. }
  164. node->set(socket, value);
  165. break;
  166. }
  167. case SocketType::TRANSFORM: {
  168. array<Transform> value;
  169. xml_read_float_array<12>(value, attr);
  170. if (value.size() == 1) {
  171. node->set(socket, value[0]);
  172. }
  173. break;
  174. }
  175. case SocketType::TRANSFORM_ARRAY: {
  176. array<Transform> value;
  177. xml_read_float_array<12>(value, attr);
  178. node->set(socket, value);
  179. break;
  180. }
  181. case SocketType::NODE: {
  182. ustring value(attr.value());
  183. map<ustring, Node *>::iterator it = reader.node_map.find(value);
  184. if (it != reader.node_map.end()) {
  185. Node *value_node = it->second;
  186. if (value_node->type == *(socket.node_type))
  187. node->set(socket, it->second);
  188. }
  189. break;
  190. }
  191. case SocketType::NODE_ARRAY: {
  192. vector<string> tokens;
  193. string_split(tokens, attr.value());
  194. array<Node *> value;
  195. value.resize(tokens.size());
  196. for (size_t i = 0; i < value.size(); i++) {
  197. map<ustring, Node *>::iterator it = reader.node_map.find(ustring(tokens[i]));
  198. if (it != reader.node_map.end()) {
  199. Node *value_node = it->second;
  200. value[i] = (value_node->type == *(socket.node_type)) ? value_node : NULL;
  201. }
  202. else {
  203. value[i] = NULL;
  204. }
  205. }
  206. node->set(socket, value);
  207. break;
  208. }
  209. case SocketType::CLOSURE:
  210. case SocketType::UNDEFINED:
  211. break;
  212. }
  213. }
  214. if (!node->name.empty())
  215. reader.node_map[node->name] = node;
  216. }
  217. xml_node xml_write_node(Node *node, xml_node xml_root)
  218. {
  219. xml_node xml_node = xml_root.append_child(node->type->name.c_str());
  220. xml_node.append_attribute("name") = node->name.c_str();
  221. foreach (const SocketType &socket, node->type->inputs) {
  222. if (socket.type == SocketType::CLOSURE || socket.type == SocketType::UNDEFINED) {
  223. continue;
  224. }
  225. if (socket.flags & SocketType::INTERNAL) {
  226. continue;
  227. }
  228. if (node->has_default_value(socket)) {
  229. continue;
  230. }
  231. xml_attribute attr = xml_node.append_attribute(socket.name.c_str());
  232. switch (socket.type) {
  233. case SocketType::BOOLEAN: {
  234. attr = xml_write_boolean(node->get_bool(socket));
  235. break;
  236. }
  237. case SocketType::BOOLEAN_ARRAY: {
  238. std::stringstream ss;
  239. const array<bool> &value = node->get_bool_array(socket);
  240. for (size_t i = 0; i < value.size(); i++) {
  241. ss << xml_write_boolean(value[i]);
  242. if (i != value.size() - 1)
  243. ss << " ";
  244. }
  245. attr = ss.str().c_str();
  246. break;
  247. }
  248. case SocketType::FLOAT: {
  249. attr = (double)node->get_float(socket);
  250. break;
  251. }
  252. case SocketType::FLOAT_ARRAY: {
  253. std::stringstream ss;
  254. const array<float> &value = node->get_float_array(socket);
  255. for (size_t i = 0; i < value.size(); i++) {
  256. ss << value[i];
  257. if (i != value.size() - 1) {
  258. ss << " ";
  259. }
  260. }
  261. attr = ss.str().c_str();
  262. break;
  263. }
  264. case SocketType::INT: {
  265. attr = node->get_int(socket);
  266. break;
  267. }
  268. case SocketType::UINT: {
  269. attr = node->get_uint(socket);
  270. break;
  271. }
  272. case SocketType::INT_ARRAY: {
  273. std::stringstream ss;
  274. const array<int> &value = node->get_int_array(socket);
  275. for (size_t i = 0; i < value.size(); i++) {
  276. ss << value[i];
  277. if (i != value.size() - 1) {
  278. ss << " ";
  279. }
  280. }
  281. attr = ss.str().c_str();
  282. break;
  283. }
  284. case SocketType::COLOR:
  285. case SocketType::VECTOR:
  286. case SocketType::POINT:
  287. case SocketType::NORMAL: {
  288. float3 value = node->get_float3(socket);
  289. attr =
  290. string_printf("%g %g %g", (double)value.x, (double)value.y, (double)value.z).c_str();
  291. break;
  292. }
  293. case SocketType::COLOR_ARRAY:
  294. case SocketType::VECTOR_ARRAY:
  295. case SocketType::POINT_ARRAY:
  296. case SocketType::NORMAL_ARRAY: {
  297. std::stringstream ss;
  298. const array<float3> &value = node->get_float3_array(socket);
  299. for (size_t i = 0; i < value.size(); i++) {
  300. ss << string_printf(
  301. "%g %g %g", (double)value[i].x, (double)value[i].y, (double)value[i].z);
  302. if (i != value.size() - 1) {
  303. ss << " ";
  304. }
  305. }
  306. attr = ss.str().c_str();
  307. break;
  308. }
  309. case SocketType::POINT2: {
  310. float2 value = node->get_float2(socket);
  311. attr = string_printf("%g %g", (double)value.x, (double)value.y).c_str();
  312. break;
  313. }
  314. case SocketType::POINT2_ARRAY: {
  315. std::stringstream ss;
  316. const array<float2> &value = node->get_float2_array(socket);
  317. for (size_t i = 0; i < value.size(); i++) {
  318. ss << string_printf("%g %g", (double)value[i].x, (double)value[i].y);
  319. if (i != value.size() - 1) {
  320. ss << " ";
  321. }
  322. }
  323. attr = ss.str().c_str();
  324. break;
  325. }
  326. case SocketType::STRING:
  327. case SocketType::ENUM: {
  328. attr = node->get_string(socket).c_str();
  329. break;
  330. }
  331. case SocketType::STRING_ARRAY: {
  332. std::stringstream ss;
  333. const array<ustring> &value = node->get_string_array(socket);
  334. for (size_t i = 0; i < value.size(); i++) {
  335. ss << value[i];
  336. if (i != value.size() - 1) {
  337. ss << " ";
  338. }
  339. }
  340. attr = ss.str().c_str();
  341. break;
  342. }
  343. case SocketType::TRANSFORM: {
  344. Transform tfm = node->get_transform(socket);
  345. std::stringstream ss;
  346. for (int i = 0; i < 3; i++) {
  347. ss << string_printf("%g %g %g %g ",
  348. (double)tfm[i][0],
  349. (double)tfm[i][1],
  350. (double)tfm[i][2],
  351. (double)tfm[i][3]);
  352. }
  353. ss << string_printf("%g %g %g %g", 0.0, 0.0, 0.0, 1.0);
  354. attr = ss.str().c_str();
  355. break;
  356. }
  357. case SocketType::TRANSFORM_ARRAY: {
  358. std::stringstream ss;
  359. const array<Transform> &value = node->get_transform_array(socket);
  360. for (size_t j = 0; j < value.size(); j++) {
  361. const Transform &tfm = value[j];
  362. for (int i = 0; i < 3; i++) {
  363. ss << string_printf("%g %g %g %g ",
  364. (double)tfm[i][0],
  365. (double)tfm[i][1],
  366. (double)tfm[i][2],
  367. (double)tfm[i][3]);
  368. }
  369. ss << string_printf("%g %g %g %g", 0.0, 0.0, 0.0, 1.0);
  370. if (j != value.size() - 1) {
  371. ss << " ";
  372. }
  373. }
  374. attr = ss.str().c_str();
  375. break;
  376. }
  377. case SocketType::NODE: {
  378. Node *value = node->get_node(socket);
  379. if (value) {
  380. attr = value->name.c_str();
  381. }
  382. break;
  383. }
  384. case SocketType::NODE_ARRAY: {
  385. std::stringstream ss;
  386. const array<Node *> &value = node->get_node_array(socket);
  387. for (size_t i = 0; i < value.size(); i++) {
  388. if (value[i]) {
  389. ss << value[i]->name.c_str();
  390. }
  391. if (i != value.size() - 1) {
  392. ss << " ";
  393. }
  394. }
  395. attr = ss.str().c_str();
  396. break;
  397. }
  398. case SocketType::CLOSURE:
  399. case SocketType::UNDEFINED:
  400. break;
  401. }
  402. }
  403. return xml_node;
  404. }
  405. CCL_NAMESPACE_END