[Bf-blender-cvs] [f2c0bbed1ce] master: Python: Add to_curve method to the object API

Omar Emara noreply at git.blender.org
Sat Feb 20 17:03:33 CET 2021


Commit: f2c0bbed1ce5270eee1332a02da02c1819bb230c
Author: Omar Emara
Date:   Sat Feb 20 18:05:13 2021 +0200
Branches: master
https://developer.blender.org/rBf2c0bbed1ce5270eee1332a02da02c1819bb230c

Python: Add to_curve method to the object API

This patch adds a to_curve method to the Object ID. This method is
analogous to the to_mesh method. The method can operate on curve and
text objects. For text objects, the text is converted into a 3D Curve ID
and that curve is returned. For curve objects, if apply_modifiers is
true, the spline deform modifiers will be applied and a Curve ID with
the result will be returned, otherwise a copy of the curve will be
returned.

The goal of this addition is to allow the developer to access the splines
of text objects and to get the result of modifier applications which was
otherwise not possible.

Reviewed By: Brecht

Differential Revision: https://developer.blender.org/D10354

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

A	doc/python_api/examples/bpy.types.Depsgraph.7.py
M	source/blender/blenkernel/BKE_curve.h
M	source/blender/blenkernel/BKE_displist.h
M	source/blender/blenkernel/BKE_object.h
M	source/blender/blenkernel/CMakeLists.txt
A	source/blender/blenkernel/intern/curve_convert.c
M	source/blender/blenkernel/intern/displist.c
M	source/blender/blenkernel/intern/object.c
M	source/blender/makesdna/DNA_object_types.h
M	source/blender/makesrna/intern/rna_object_api.c

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

diff --git a/doc/python_api/examples/bpy.types.Depsgraph.7.py b/doc/python_api/examples/bpy.types.Depsgraph.7.py
new file mode 100644
index 00000000000..61209a6b9d2
--- /dev/null
+++ b/doc/python_api/examples/bpy.types.Depsgraph.7.py
@@ -0,0 +1,64 @@
+"""
+Dependency graph: Object.to_curve()
++++++++++++++++++++++++++++++++++++
+
+Function to get a curve from text and curve objects. It is typically used by exporters, render
+engines, and tools that need to access the curve representing the object.
+
+The function takes the evaluated dependency graph as a required parameter and optionally a boolean
+apply_modifiers which defaults to false. If apply_modifiers is true and the object is a curve object,
+the spline deform modifiers are applied on the control points. Note that constructive modifiers and
+modifiers that are not spline-enabled will not be applied. So modifiers like Array will not be applied
+and deform modifiers that have Apply On Spline disabled will not be applied.
+
+If the object is a text object. The text will be converted into a 3D curve and returned. Modifiers are
+never applied on text objects and apply_modifiers will be ignored. If the object is neither a curve nor
+a text object, an error will be reported.
+
+.. note:: The resulting curve is owned by the object. It can be freed by calling `object.to_curve_clear()`.
+.. note::
+   The resulting curve must be treated as temporary, and can not be referenced from objects in the main
+   database.
+"""
+import bpy
+
+
+class OBJECT_OT_object_to_curve(bpy.types.Operator):
+    """Convert selected object to curve and show number of splines"""
+    bl_label = "DEG Object to Curve"
+    bl_idname = "object.object_to_curve"
+
+    def execute(self, context):
+        # Access input original object.
+        obj = context.object
+        if obj is None:
+            self.report({'INFO'}, "No active object to convert to curve")
+            return {'CANCELLED'}
+        if obj.type not in {'CURVE', 'FONT'}:
+            self.report({'INFO'}, "Object can not be converted to curve")
+            return {'CANCELLED'}
+        depsgraph = context.evaluated_depsgraph_get()
+        # Invoke to_curve() without applying modifiers.
+        curve_without_modifiers = obj.to_curve(depsgraph)
+        self.report({'INFO'}, f"{len(curve_without_modifiers.splines)} splines in a new curve without modifiers.")
+        # Remove temporary curve.
+        obj.to_curve_clear()
+        # Invoke to_curve() with applying modifiers.
+        curve_with_modifiers = obj.to_curve(depsgraph, apply_modifiers = True)
+        self.report({'INFO'}, f"{len(curve_with_modifiers.splines)} splines in new curve with modifiers.")
+        # Remove temporary curve.
+        obj.to_curve_clear()
+        return {'FINISHED'}
+
+
+def register():
+    bpy.utils.register_class(OBJECT_OT_object_to_curve)
+
+
+def unregister():
+    bpy.utils.unregister_class(OBJECT_OT_object_to_curve)
+
+
+if __name__ == "__main__":
+    register()
+
diff --git a/source/blender/blenkernel/BKE_curve.h b/source/blender/blenkernel/BKE_curve.h
index 881b93fe709..660e7c08062 100644
--- a/source/blender/blenkernel/BKE_curve.h
+++ b/source/blender/blenkernel/BKE_curve.h
@@ -338,6 +338,18 @@ void BKE_curve_deform_co(const struct Object *ob_curve,
 
 /** \} */
 
+/* curve_convert.c */
+
+/* Create a new curve from the given object at its current state. This only works for curve and
+ * text objects, otherwise NULL is returned.
+ *
+ * If apply_modifiers is true and the object is a curve one, then spline deform modifiers are
+ * applied on the control points of the splines.
+ */
+struct Curve *BKE_curve_new_from_object(struct Object *object,
+                                        struct Depsgraph *depsgraph,
+                                        bool apply_modifiers);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenkernel/BKE_displist.h b/source/blender/blenkernel/BKE_displist.h
index 83adbf6f1fd..05e60d38487 100644
--- a/source/blender/blenkernel/BKE_displist.h
+++ b/source/blender/blenkernel/BKE_displist.h
@@ -112,6 +112,13 @@ void BKE_displist_make_mball_forRender(struct Depsgraph *depsgraph,
                                        struct Object *ob,
                                        struct ListBase *dispbase);
 
+bool BKE_curve_calc_modifiers_pre(struct Depsgraph *depsgraph,
+                                  struct Scene *scene,
+                                  struct Object *ob,
+                                  struct ListBase *source_nurb,
+                                  struct ListBase *target_nurb,
+                                  const bool for_render);
+
 bool BKE_displist_surfindex_get(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4);
 void BKE_displist_fill(const struct ListBase *dispbase,
                        struct ListBase *to,
diff --git a/source/blender/blenkernel/BKE_object.h b/source/blender/blenkernel/BKE_object.h
index 904db053717..12c40e891c9 100644
--- a/source/blender/blenkernel/BKE_object.h
+++ b/source/blender/blenkernel/BKE_object.h
@@ -32,6 +32,7 @@ extern "C" {
 
 struct Base;
 struct BoundBox;
+struct Curve;
 struct Depsgraph;
 struct GpencilModifierData;
 struct HookGpencilModifierData;
@@ -424,6 +425,21 @@ struct Mesh *BKE_object_to_mesh(struct Depsgraph *depsgraph,
 
 void BKE_object_to_mesh_clear(struct Object *object);
 
+/* This is an utility function for Python's object.to_curve().
+ * The result is owned by the object.
+ *
+ * The curve will be freed when object is re-evaluated or is destroyed. It is possible to force
+ * clear memory used by this curve by calling BKE_object_to_curve_clear().
+ *
+ * If apply_modifiers is true and the object is a curve one, then spline deform modifiers are
+ * applied on the curve control points.
+ */
+struct Curve *BKE_object_to_curve(struct Object *object,
+                                  struct Depsgraph *depsgraph,
+                                  bool apply_modifiers);
+
+void BKE_object_to_curve_clear(struct Object *object);
+
 void BKE_object_check_uuids_unique_and_report(const struct Object *object);
 
 void BKE_object_modifiers_lib_link_common(void *userData,
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index f288bf9aabc..325744f4006 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -107,6 +107,7 @@ set(SRC
   intern/cryptomatte.cc
   intern/curve.c
   intern/curve_bevel.c
+  intern/curve_convert.c
   intern/curve_decimate.c
   intern/curve_deform.c
   intern/curveprofile.c
diff --git a/source/blender/blenkernel/intern/curve_convert.c b/source/blender/blenkernel/intern/curve_convert.c
new file mode 100644
index 00000000000..988ddb3bbc0
--- /dev/null
+++ b/source/blender/blenkernel/intern/curve_convert.c
@@ -0,0 +1,81 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup bke
+ */
+
+#include "DNA_curve_types.h"
+#include "DNA_object_types.h"
+#include "DNA_vfont_types.h"
+
+#include "BLI_utildefines.h"
+
+#include "BKE_curve.h"
+#include "BKE_displist.h"
+#include "BKE_font.h"
+#include "BKE_lib_id.h"
+#include "BKE_modifier.h"
+
+#include "DEG_depsgraph.h"
+#include "DEG_depsgraph_query.h"
+
+static Curve *curve_from_font_object(Object *object, Depsgraph *depsgraph)
+{
+  Curve *curve = (Curve *)object->data;
+  Curve *new_curve = (Curve *)BKE_id_copy_ex(NULL, &curve->id, NULL, LIB_ID_COPY_LOCALIZE);
+
+  Object *evaluated_object = DEG_get_evaluated_object(depsgraph, object);
+  BKE_vfont_to_curve_nubase(evaluated_object, FO_EDIT, &new_curve->nurb);
+
+  new_curve->type = OB_CURVE;
+
+  new_curve->flag &= ~CU_3D;
+  BKE_curve_curve_dimension_update(new_curve);
+
+  return new_curve;
+}
+
+static Curve *curve_from_curve_object(Object *object, Depsgraph *depsgraph, bool apply_modifiers)
+{
+  Object *evaluated_object = DEG_get_evaluated_object(depsgraph, object);
+  Curve *curve = (Curve *)evaluated_object->data;
+  Curve *new_curve = (Curve *)BKE_id_copy_ex(NULL, &curve->id, NULL, LIB_ID_COPY_LOCALIZE);
+
+  if (apply_modifiers) {
+    BKE_curve_calc_modifiers_pre(depsgraph,
+                                 DEG_get_input_scene(depsgraph),
+                                 evaluated_object,
+                                 BKE_curve_nurbs_get(curve),
+                                 &new_curve->nurb,
+                                 DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
+  }
+
+  return new_curve;
+}
+
+Curve *BKE_curve_new_from_object(Object *object, Depsgraph *depsgraph, bool apply_modifiers)
+{
+  if (!ELEM(object->type, OB_FONT, OB_CURVE)) {
+    return NULL;
+  }
+
+  if (object->type == OB_FONT) {
+    return curve_from_font_object(object, depsgraph);
+  }
+
+  return curve_from_curve_object(object, depsgraph, apply_modifiers);
+}
diff --git a/source/blender/blenkernel/intern/displist.c b/source/blender/blenkernel/intern/displist.c
index 1fcc1b1bcef..c860e57520d 100644
--- a/source/blender/blenkernel/intern/displist.c
+++ b/source/blender/blenkernel/intern/displist.c
@@ -770,8 +770,12 @@ static ModifierData *curve_get_tessellate_point(Scene *scene,
 }
 
 /* Return true if any modifier was applied. */
-static bool curve_calc_modifiers_pre(
-    Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *nurb, const bool for_render)
+bool BKE_curve_calc_modifiers_pre(Depsgraph *depsgraph,
+                                  Scene *scene,
+                                  Object *ob,
+              

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list