Blender  V3.3
curves_add.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BLI_rand.hh"
8 
9 #include "BKE_context.h"
10 #include "BKE_curves.hh"
11 #include "BKE_node.h"
12 #include "BKE_node_runtime.hh"
13 
14 #include "ED_curves.h"
15 #include "ED_node.h"
16 #include "ED_object.h"
17 
18 #include "DNA_modifier_types.h"
19 #include "DNA_node_types.h"
20 #include "DNA_object_types.h"
21 
23 
25 {
26  LISTBASE_FOREACH (const bNode *, node, &ntree.nodes) {
28  return true;
29  }
30  if (node->type == NODE_GROUP) {
31  if (node->id != nullptr) {
32  if (has_surface_deformation_node(*reinterpret_cast<const bNodeTree *>(node->id))) {
33  return true;
34  }
35  }
36  }
37  }
38  return false;
39 }
40 
41 static bool has_surface_deformation_node(const Object &curves_ob)
42 {
43  LISTBASE_FOREACH (const ModifierData *, md, &curves_ob.modifiers) {
44  if (md->type != eModifierType_Nodes) {
45  continue;
46  }
47  const NodesModifierData *nmd = reinterpret_cast<const NodesModifierData *>(md);
48  if (nmd->node_group == nullptr) {
49  continue;
50  }
52  return true;
53  }
54  }
55  return false;
56 }
57 
59 {
60  if (has_surface_deformation_node(curves_ob)) {
61  return;
62  }
63 
64  Main *bmain = CTX_data_main(&C);
66 
68  nullptr, bmain, scene, &curves_ob, "Surface Deform", eModifierType_Nodes);
69  NodesModifierData &nmd = *reinterpret_cast<NodesModifierData *>(md);
70  nmd.node_group = ntreeAddTree(bmain, "Surface Deform", "GeometryNodeTree");
71 
72  bNodeTree *ntree = nmd.node_group;
73  ntreeAddSocketInterface(ntree, SOCK_IN, "NodeSocketGeometry", "Geometry");
74  ntreeAddSocketInterface(ntree, SOCK_OUT, "NodeSocketGeometry", "Geometry");
75  bNode *group_input = nodeAddStaticNode(&C, ntree, NODE_GROUP_INPUT);
76  bNode *group_output = nodeAddStaticNode(&C, ntree, NODE_GROUP_OUTPUT);
78 
80 
82  group_input,
83  static_cast<bNodeSocket *>(group_input->outputs.first),
84  deform_node,
85  nodeFindSocket(deform_node, SOCK_IN, "Curves"));
87  deform_node,
88  nodeFindSocket(deform_node, SOCK_OUT, "Curves"),
89  group_output,
90  static_cast<bNodeSocket *>(group_output->inputs.first));
91 
92  group_input->locx = -200;
93  group_output->locx = 200;
94  deform_node->locx = 0;
95 
97 }
98 
99 bke::CurvesGeometry primitive_random_sphere(const int curves_size, const int points_per_curve)
100 {
101  bke::CurvesGeometry curves(points_per_curve * curves_size, curves_size);
102 
103  MutableSpan<int> offsets = curves.offsets_for_write();
104  MutableSpan<float3> positions = curves.positions_for_write();
105 
106  float *radius_data = (float *)CustomData_add_layer_named(
107  &curves.point_data, CD_PROP_FLOAT, CD_DEFAULT, nullptr, curves.point_num, "radius");
108  MutableSpan<float> radii{radius_data, curves.points_num()};
109 
110  for (const int i : offsets.index_range()) {
111  offsets[i] = points_per_curve * i;
112  }
113 
115 
116  for (const int i : curves.curves_range()) {
117  const IndexRange curve_range = curves.points_for_curve(i);
118  MutableSpan<float3> curve_positions = positions.slice(curve_range);
119  MutableSpan<float> curve_radii = radii.slice(curve_range);
120 
121  const float theta = 2.0f * M_PI * rng.get_float();
122  const float phi = saacosf(2.0f * rng.get_float() - 1.0f);
123 
124  float3 no = {std::sin(theta) * std::sin(phi), std::cos(theta) * std::sin(phi), std::cos(phi)};
125  no = math::normalize(no);
126 
127  float3 co = no;
128  for (int key = 0; key < points_per_curve; key++) {
129  float t = key / (float)(points_per_curve - 1);
130  curve_positions[key] = co;
131  curve_radii[key] = 0.02f * (1.0f - t);
132 
133  float3 offset = float3(rng.get_float(), rng.get_float(), rng.get_float()) * 2.0f - 1.0f;
134  co += (offset + no) / points_per_curve;
135  }
136  }
137 
138  return curves;
139 }
140 
141 } // namespace blender::ed::curves
typedef float(TangentPoint)[2]
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
Low-level operations for curves.
@ CD_DEFAULT
void * CustomData_add_layer_named(struct CustomData *data, int type, eCDAllocType alloctype, void *layer, int totelem, const char *name)
Definition: customdata.cc:2792
#define GEO_NODE_DEFORM_CURVES_ON_SURFACE
Definition: BKE_node.h:1508
struct bNodeLink * nodeAddLink(struct bNodeTree *ntree, struct bNode *fromnode, struct bNodeSocket *fromsock, struct bNode *tonode, struct bNodeSocket *tosock)
Definition: node.cc:2296
struct bNodeSocket * nodeFindSocket(const struct bNode *node, eNodeSocketInOut in_out, const char *identifier)
struct bNodeTree * ntreeAddTree(struct Main *bmain, const char *name, const char *idname)
Definition: node.cc:2674
struct bNodeSocket * ntreeAddSocketInterface(struct bNodeTree *ntree, eNodeSocketInOut in_out, const char *idname, const char *name)
Definition: node.cc:3349
struct bNode * nodeAddStaticNode(const struct bContext *C, struct bNodeTree *ntree, int type)
Definition: node.cc:2151
#define NODE_GROUP_INPUT
Definition: BKE_node.h:987
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
MINLINE float saacosf(float f)
#define M_PI
Definition: BLI_math_base.h:20
@ CD_PROP_FLOAT
@ eModifierType_Nodes
@ SOCK_OUT
@ SOCK_IN
Object is a sort of wrapper for general info.
void ED_node_tree_propagate_change(const struct bContext *C, struct Main *bmain, struct bNodeTree *ntree)
struct ModifierData * ED_object_modifier_add(struct ReportList *reports, struct Main *bmain, struct Scene *scene, struct Object *ob, const char *name, int type)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
NODE_GROUP_OUTPUT
NODE_GROUP
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to curves
#define C
Definition: RandGen.cpp:25
constexpr IndexRange index_range() const
Definition: BLI_span.hh:661
OperationNode * node
Scene scene
bNodeTree * ntree
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:311
static bool has_surface_deformation_node(const bNodeTree &ntree)
Definition: curves_add.cc:24
bke::CurvesGeometry primitive_random_sphere(const int curves_size, const int points_per_curve)
Definition: curves_add.cc:99
void ensure_surface_deformation_node_exists(bContext &C, Object &curves_ob)
Definition: curves_add.cc:58
vec_base< T, Size > normalize(const vec_base< T, Size > &v)
vec_base< float, 3 > float3
MutableSpan< float3 > positions
MutableSpan< float > radii
void * first
Definition: DNA_listBase.h:31
Definition: BKE_main.h:121
struct bNodeTree * node_group
ListBase modifiers
ListBase nodes
ListBase inputs
float locx
ListBase outputs