Blender  V3.3
object_random.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2014 Blender Foundation. All rights reserved. */
3 
8 #include "MEM_guardedalloc.h"
9 
10 #include "DNA_layer_types.h"
11 #include "DNA_object_types.h"
12 
13 #include "BLI_math.h"
14 #include "BLI_rand.h"
15 
16 #include "BKE_context.h"
17 #include "BKE_layer.h"
18 
19 #include "RNA_access.h"
20 #include "RNA_define.h"
21 
22 #include "WM_api.h"
23 #include "WM_types.h"
24 
25 #include "ED_transverts.h"
26 
27 #include "object_intern.h"
28 
34  const float offset,
35  const float uniform,
36  const float normal_factor,
37  const uint seed)
38 {
39  bool use_normal = (normal_factor != 0.0f);
40  struct RNG *rng;
41  TransVert *tv;
42  int a;
43 
44  if (!tvs || !(tvs->transverts)) {
45  return false;
46  }
47 
48  rng = BLI_rng_new(seed);
49 
50  tv = tvs->transverts;
51  for (a = 0; a < tvs->transverts_tot; a++, tv++) {
52  const float t = max_ff(0.0f, uniform + ((1.0f - uniform) * BLI_rng_get_float(rng)));
53  float vec[3];
55 
56  if (use_normal && (tv->flag & TX_VERT_USE_NORMAL)) {
57  float no[3];
58 
59  /* avoid >90d rotation to align with normal */
60  if (dot_v3v3(vec, tv->normal) < 0.0f) {
61  negate_v3_v3(no, tv->normal);
62  }
63  else {
64  copy_v3_v3(no, tv->normal);
65  }
66 
67  interp_v3_v3v3_slerp_safe(vec, vec, no, normal_factor);
68  }
69 
70  madd_v3_v3fl(tv->loc, vec, offset * t);
71  }
72 
74 
75  return true;
76 }
77 
79 {
80  ViewLayer *view_layer = CTX_data_view_layer(C);
81  Object *ob_active = CTX_data_edit_object(C);
82  const int ob_mode = ob_active->mode;
83 
84  const float offset = RNA_float_get(op->ptr, "offset");
85  const float uniform = RNA_float_get(op->ptr, "uniform");
86  const float normal_factor = RNA_float_get(op->ptr, "normal");
87  const uint seed = RNA_int_get(op->ptr, "seed");
88 
89  bool changed_multi = false;
90  uint objects_len = 0;
92  view_layer, CTX_wm_view3d(C), &objects_len, ob_mode);
93  for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
94  Object *ob_iter = objects[ob_index];
95 
96  TransVertStore tvs = {NULL};
97 
98  if (ob_iter) {
99  int mode = TM_ALL_JOINTS;
100 
101  if (normal_factor != 0.0f) {
102  mode |= TX_VERT_USE_NORMAL;
103  }
104 
105  ED_transverts_create_from_obedit(&tvs, ob_iter, mode);
106  if (tvs.transverts_tot == 0) {
107  continue;
108  }
109 
110  int seed_iter = seed;
111  /* This gives a consistent result regardless of object order. */
112  if (ob_index) {
113  seed_iter += BLI_ghashutil_strhash_p(ob_iter->id.name);
114  }
115 
116  object_rand_transverts(&tvs, offset, uniform, normal_factor, seed_iter);
117 
118  ED_transverts_update_obedit(&tvs, ob_iter);
119  ED_transverts_free(&tvs);
120 
122  changed_multi = true;
123  }
124  }
125  MEM_freeN(objects);
126 
127  return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
128 }
129 
131 {
132  /* identifiers */
133  ot->name = "Randomize";
134  ot->description = "Randomize vertices";
135  ot->idname = "TRANSFORM_OT_vertex_random";
136 
137  /* api callbacks */
140 
141  /* flags */
143 
144  /* props */
146  ot->srna, "offset", 0.0f, -FLT_MAX, FLT_MAX, "Amount", "Distance to offset", -10.0f, 10.0f);
148  "uniform",
149  0.0f,
150  0.0f,
151  1.0f,
152  "Uniform",
153  "Increase for uniform offset distance",
154  0.0f,
155  1.0f);
157  "normal",
158  0.0f,
159  0.0f,
160  1.0f,
161  "Normal",
162  "Align offset direction to normals",
163  0.0f,
164  1.0f);
165  RNA_def_int(
166  ot->srna, "seed", 0, 0, 10000, "Random Seed", "Seed for the random number generator", 0, 50);
167 
168  /* Set generic modal callbacks. */
170 }
struct Object * CTX_data_edit_object(const bContext *C)
Definition: context.c:1370
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:784
#define BKE_view_layer_array_from_objects_in_mode_unique_data(view_layer, v3d, r_len, mode)
Definition: BKE_layer.h:560
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
MINLINE float max_ff(float a, float b)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3_v3(float r[3], const float a[3])
void interp_v3_v3v3_slerp_safe(float target[3], const float a[3], const float b[3], float t)
Definition: math_vector.c:92
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
Random number functions.
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition: rand.cc:58
struct RNG * BLI_rng_new(unsigned int seed)
Definition: rand.cc:39
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: rand.cc:93
void void BLI_rng_get_float_unit_v3(struct RNG *rng, float v[3]) ATTR_NONNULL(1
unsigned int uint
Definition: BLI_sys_types.h:67
Object is a sort of wrapper for general info.
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
void ED_transverts_update_obedit(TransVertStore *tvs, struct Object *obedit)
Definition: ed_transverts.c:37
void ED_transverts_create_from_obedit(TransVertStore *tvs, const struct Object *obedit, int mode)
bool ED_transverts_poll(struct bContext *C)
void ED_transverts_free(TransVertStore *tvs)
@ TM_ALL_JOINTS
Definition: ED_transverts.h:53
@ TX_VERT_USE_NORMAL
Definition: ED_transverts.h:67
_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
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
#define ND_DRAW
Definition: WM_types.h:410
#define NC_OBJECT
Definition: WM_types.h:329
static unsigned long seed
Definition: btSoftBody.h:39
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
static unsigned a[3]
Definition: RandGen.cpp:78
void TRANSFORM_OT_vertex_random(struct wmOperatorType *ot)
static int object_rand_verts_exec(bContext *C, wmOperator *op)
Definition: object_random.c:78
static bool object_rand_transverts(TransVertStore *tvs, const float offset, const float uniform, const float normal_factor, const uint seed)
Definition: object_random.c:33
int RNA_int_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4910
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4957
PropertyRNA * RNA_def_float_distance(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:4052
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:4144
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, int default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3597
char name[66]
Definition: DNA_ID.h:378
Definition: rand.cc:33
blender::RandomNumberGenerator rng
Definition: rand.cc:34
struct TransVert * transverts
Definition: ED_transverts.h:25
float * loc
Definition: ED_transverts.h:18
float normal[3]
Definition: ED_transverts.h:20
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
PropertyRNA * prop
Definition: WM_types.h:981
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3479
void WM_operator_type_modal_from_exec_for_object_edit_coords(wmOperatorType *ot)