[Bf-blender-cvs] [7d813598a7e] sybren-usd-experiments: Abstracted hierarchy walking of Alembic exporter

Sybren A. Stüvel noreply at git.blender.org
Wed Jun 19 17:59:15 CEST 2019


Commit: 7d813598a7e7b687994c008125194889b5ecdc0a
Author: Sybren A. Stüvel
Date:   Fri Jun 14 16:41:40 2019 +0200
Branches: sybren-usd-experiments
https://developer.blender.org/rB7d813598a7e7b687994c008125194889b5ecdc0a

Abstracted hierarchy walking of Alembic exporter

===================================================================

M	source/blender/usd/CMakeLists.txt
A	source/blender/usd/intern/abstract_hierarchy_iterator.cc
A	source/blender/usd/intern/abstract_hierarchy_iterator.h
M	source/blender/usd/intern/usd_exporter.cc
M	source/blender/usd/intern/usd_exporter.h
M	source/blender/usd/intern/usd_exporter_context.h
M	source/blender/usd/intern/usd_hierarchy_iterator.cc
M	source/blender/usd/intern/usd_hierarchy_iterator.h
M	source/blender/usd/intern/usd_writer_abstract.cc
M	source/blender/usd/intern/usd_writer_abstract.h
M	source/blender/usd/intern/usd_writer_mesh.cc
M	source/blender/usd/intern/usd_writer_mesh.h
M	source/blender/usd/intern/usd_writer_transform.cc

===================================================================

diff --git a/source/blender/usd/CMakeLists.txt b/source/blender/usd/CMakeLists.txt
index a77d20366ed..fafe03aa2ce 100644
--- a/source/blender/usd/CMakeLists.txt
+++ b/source/blender/usd/CMakeLists.txt
@@ -47,14 +47,18 @@ set(INC_SYS
 )
 
 set(SRC
+  intern/abstract_hierarchy_iterator.cc
   intern/usd_capi.cc
   intern/usd_exporter.cc
+  intern/usd_hierarchy_iterator.cc
   intern/usd_writer_abstract.cc
   intern/usd_writer_mesh.cc
   intern/usd_writer_transform.cc
 
   usd.h
+  intern/abstract_hierarchy_iterator.h
   intern/usd_exporter.h
+  intern/usd_hierarchy_iterator.h
   intern/usd_writer_abstract.h
   intern/usd_writer_mesh.h
   intern/usd_writer_transform.h
diff --git a/source/blender/usd/intern/usd_hierarchy_iterator.cc b/source/blender/usd/intern/abstract_hierarchy_iterator.cc
similarity index 63%
copy from source/blender/usd/intern/usd_hierarchy_iterator.cc
copy to source/blender/usd/intern/abstract_hierarchy_iterator.cc
index 2f3da814f85..513d31960cc 100644
--- a/source/blender/usd/intern/usd_hierarchy_iterator.cc
+++ b/source/blender/usd/intern/abstract_hierarchy_iterator.cc
@@ -1,12 +1,12 @@
-#include "usd_hierarchy_iterator.h"
+#include "abstract_hierarchy_iterator.h"
 
 #include <string>
 
-#include <pxr/base/tf/stringUtils.h>
-
 extern "C" {
 #include "BKE_anim.h"
+
 #include "BLI_assert.h"
+#include "BLI_utildefines.h"
 
 #include "DEG_depsgraph_query.h"
 
@@ -15,22 +15,48 @@ extern "C" {
 #include "DNA_object_types.h"
 }
 
-static std::string get_id_name(const Object *const ob)
+AbstractHierarchyIterator::AbstractHierarchyIterator(Depsgraph *depsgraph)
+    : depsgraph(depsgraph), writers()
+{
+}
+
+AbstractHierarchyIterator::~AbstractHierarchyIterator()
+{
+}
+
+void AbstractHierarchyIterator::release()
+{
+  for (WriterMap::value_type it : writers) {
+    delete_object_writer(it.second);
+  }
+  writers.clear();
+}
+
+void AbstractHierarchyIterator::iterate()
+{
+  ViewLayer *view_layer = DEG_get_input_view_layer(depsgraph);
+  for (Base *base = static_cast<Base *>(view_layer->object_bases.first); base; base = base->next) {
+    Object *ob = base->object;
+    visit_object(base, ob, ob->parent, NULL);
+  }
+}
+
+std::string AbstractHierarchyIterator::get_object_name(const Object *const object) const
 {
-  if (!ob) {
+  if (object == NULL) {
     return "";
   }
 
-  return get_id_name(&ob->id);
+  return get_id_name(&object->id);
 }
 
-static std::string get_id_name(const ID *const id)
+std::string AbstractHierarchyIterator::get_id_name(const ID *const id) const
 {
-  if (!id)
+  if (id == NULL) {
     return "";
+  }
 
-  std::string name(id->name + 2);
-  return pxr::TfMakeValidIdentifier(name);
+  return std::string(id->name + 2);
 }
 
 /**
@@ -42,31 +68,33 @@ static std::string get_id_name(const ID *const id)
  * \param dupli_parent:
  * \return
  */
-static std::string get_object_dag_path_name(const Object *const ob, Object *dupli_parent)
+std::string AbstractHierarchyIterator::get_object_dag_path_name(
+    const Object *const ob, const Object *const dupli_parent) const
 {
-  std::string name = get_id_name(ob);
-
-  Object *p = ob->parent;
+  std::string name = get_object_name(ob);
 
-  while (p) {
-    name = get_id_name(p) + "/" + name;
-    p = p->parent;
+  for (Object *parent = ob->parent; parent; parent = parent->parent) {
+    name = get_object_name(parent) + "/" + name;
   }
 
   if (dupli_parent && (ob != dupli_parent)) {
-    name = get_id_name(dupli_parent) + "/" + name;
+    // TODO(Sybren): shouldn't this call get_object_dag_path_name()?
+    name = get_object_name(dupli_parent) + "/" + name;
   }
 
   return name;
 }
 
-void AbstractHierarchyIterator::iterate()
+bool AbstractHierarchyIterator::should_visit_object(const Base *const UNUSED(base),
+                                                    bool UNUSED(is_duplicated)) const
 {
-  ViewLayer *view_layer = DEG_get_input_view_layer(depsgraph);
-  for (Base *base = static_cast<Base *>(view_layer->object_bases.first); base; base = base->next) {
-    Object *ob = base->object;
-    visit_object(base, ob, ob->parent, NULL);
-  }
+  return true;
+}
+
+bool AbstractHierarchyIterator::should_visit_duplilink(const DupliObject *const link) const
+{
+  // Removing link->no_draw hides things like custom bone shapes.
+  return !link->no_draw && link->type == OB_DUPLICOLLECTION;
 }
 
 void AbstractHierarchyIterator::visit_object(Base *base,
@@ -91,17 +119,13 @@ void AbstractHierarchyIterator::visit_object(Base *base,
     Object *dupli_parent = NULL;
 
     for (; link; link = link->next) {
-      //   /* This skips things like custom bone shapes. */
-      //   if (m_settings.renderable_only && link->no_draw) {
-      //     continue;
-      //   }
-
-      if (link->type == OB_DUPLICOLLECTION) {
-        dupli_ob = link->ob;
-        dupli_parent = (dupli_ob->parent) ? dupli_ob->parent : ob;
-
-        visit_object(base, dupli_ob, dupli_parent, ob);
+      if (!should_visit_duplilink(link)) {
+        continue;
       }
+
+      dupli_ob = link->ob;
+      dupli_parent = (dupli_ob->parent) ? dupli_ob->parent : ob;
+      visit_object(base, dupli_ob, dupli_parent, ob);
     }
 
     free_object_duplilist(lb);
@@ -125,9 +149,9 @@ TEMP_WRITER_TYPE *AbstractHierarchyIterator::export_object_and_parents(Object *o
   //   }
 
   /* check if we have already created a transform writer for this object */
-  TEMP_WRITER_TYPE *my_writer = get_writer(name);
-  if (my_writer != NULL) {
-    return my_writer;
+  TEMP_WRITER_TYPE *xform_writer = get_writer(name);
+  if (xform_writer != NULL) {
+    return xform_writer;
   }
 
   TEMP_WRITER_TYPE *parent_writer = NULL;
@@ -163,15 +187,23 @@ TEMP_WRITER_TYPE *AbstractHierarchyIterator::export_object_and_parents(Object *o
     BLI_assert(parent_writer);
   }
 
-  my_writer = create_object_writer(name, ob, parent_writer);
-
+  xform_writer = create_xform_writer(name, ob, parent_writer);
   //   /* When flattening, the matrix of the dupliobject has to be added. */
   //   if (m_settings.flatten_hierarchy && dupliObParent) {
-  //     my_writer->m_proxy_from = dupliObParent;
+  //     xform_writer->m_proxy_from = dupliObParent;
   //   }
+  if (xform_writer != NULL) {
+    writers[name] = xform_writer;
+  }
 
-  writers[name] = my_writer;
-  return my_writer;
+  if (ob->data != NULL) {
+    TEMP_WRITER_TYPE *data_writer = create_data_writer(name, ob, xform_writer);
+    if (data_writer != NULL) {
+      writers[name] = data_writer;
+    }
+  }
+
+  return xform_writer;
 }
 
 TEMP_WRITER_TYPE *AbstractHierarchyIterator::get_writer(const std::string &name)
@@ -182,4 +214,4 @@ TEMP_WRITER_TYPE *AbstractHierarchyIterator::get_writer(const std::string &name)
     return NULL;
   }
   return it->second;
-}
\ No newline at end of file
+}
diff --git a/source/blender/usd/intern/abstract_hierarchy_iterator.h b/source/blender/usd/intern/abstract_hierarchy_iterator.h
new file mode 100644
index 00000000000..a4756d2222f
--- /dev/null
+++ b/source/blender/usd/intern/abstract_hierarchy_iterator.h
@@ -0,0 +1,58 @@
+#ifndef __USD__ABSTRACT_HIERARCHY_ITERATOR_H__
+#define __USD__ABSTRACT_HIERARCHY_ITERATOR_H__
+
+#include <map>
+#include <string>
+
+struct Base;
+struct Depsgraph;
+struct DupliObject;
+struct ID;
+struct Object;
+struct ViewLayer;
+
+typedef void TEMP_WRITER_TYPE;
+
+class AbstractHierarchyIterator {
+ protected:
+  Depsgraph *depsgraph;
+
+  typedef std::map<std::string, TEMP_WRITER_TYPE *> WriterMap;
+  WriterMap writers;
+
+ public:
+  explicit AbstractHierarchyIterator(Depsgraph *depsgraph);
+  virtual ~AbstractHierarchyIterator();
+
+  void iterate();
+
+ private:
+  void release();
+
+  void visit_object(Base *base, Object *object, Object *parent, Object *dupliObParent);
+
+  std::string get_object_name(const Object *const object) const;
+  std::string get_object_dag_path_name(const Object *const ob,
+                                       const Object *const dupli_parent) const;
+
+  TEMP_WRITER_TYPE *export_object_and_parents(Object *ob, Object *parent, Object *dupliObParent);
+  TEMP_WRITER_TYPE *get_writer(const std::string &name);
+
+ protected:
+  /* Not visiting means not exporting and also not expanding its duplis. */
+  virtual bool should_visit_object(const Base *const base, bool is_duplicated) const;
+  virtual bool should_visit_duplilink(const DupliObject *const link) const;
+
+  virtual TEMP_WRITER_TYPE *create_xform_writer(const std::string &name,
+                                                Object *object,
+                                                void *parent_writer) = 0;
+  virtual TEMP_WRITER_TYPE *create_data_writer(const std::string &name,
+                                               Object *object,
+                                               void *parent_writer) = 0;
+
+  virtual void delete_object_writer(TEMP_WRITER_TYPE *writer) = 0;
+
+  virtual std::string get_id_name(const ID *const id) const = 0;
+};
+
+#endif /* __USD__ABSTRACT_HIERARCHY_ITERATOR_H__ */
diff --git a/source/blender/usd/intern/usd_exporter.cc b/source/blender/usd/intern/usd_exporter.cc
index f5c54e2a850..2908f5a892b 100644
--- a/source/blender/usd/intern/usd_exporter.cc
+++ b/source/blender/usd/intern/usd_exporter.cc
@@ -21,6 +21,7 @@
 #include "usd_exporter.h"
 #include "usd_writer_mesh.h"
 #include "usd_writer_transform.h"
+#include "usd_hierarchy_iterator.h"
 
 #include <pxr/usd/usd/stage.h>
 #include <pxr/usd/usdGeom/tokens.h>
@@ -56,213 +57,9 @@ void USDExporter::operator()(float &r_progress, bool &r_was_canceled)
   m_stage = pxr::UsdStage::CreateNew(m_filename);
   m_stage->SetMetadata(pxr::UsdGeomTokens->upAxis, pxr::VtValue(pxr::UsdGeomTokens->z));
 
-  for (Base *base = static_cast<Base *>(m_settings.view_layer->object_bases.first); base;
-       base = base->next) {
-    Object *ob = base->object;
-    Object *ob_eval = DEG_get_evaluated_object(m_settings.depsgraph, ob);
-
-    export_or_queue(ob_eval, NULL, root);
-  }
-
-  // DEG_OBJECT_ITER_BEGIN (m_settings.depsgraph,
-  //                        ob_eval,
-  //                        DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
-  //                            DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | DEG_ITER_OBJECT_FLAG_VISIBLE
-  //                            | DEG

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list