Blender V4.5
object_random.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_layer_types.h"
10#include "DNA_object_types.h"
11
12#include "BLI_math_vector.h"
13#include "BLI_rand.hh"
14
15#include "BKE_context.hh"
16#include "BKE_layer.hh"
17
18#include "RNA_access.hh"
19#include "RNA_define.hh"
20
21#include "WM_api.hh"
22#include "WM_types.hh"
23
24#include "ED_object.hh"
25#include "ED_transverts.hh"
26
27#include "object_intern.hh"
28
29namespace blender::ed::object {
30
34
36 const float offset,
37 const float uniform,
38 const float normal_factor,
39 const uint seed)
40{
41 bool use_normal = (normal_factor != 0.0f);
42 TransVert *tv;
43 int a;
44
45 if (!tvs || !(tvs->transverts)) {
46 return false;
47 }
48
50
51 tv = tvs->transverts;
52 for (a = 0; a < tvs->transverts_tot; a++, tv++) {
53 const float t = max_ff(0.0f, uniform + ((1.0f - uniform) * rng.get_float()));
54 float3 vec = rng.get_unit_float3();
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
73 return true;
74}
75
77{
78 const Scene *scene = CTX_data_scene(C);
79 ViewLayer *view_layer = CTX_data_view_layer(C);
80 Object *ob_active = CTX_data_edit_object(C);
81 const int ob_mode = ob_active->mode;
82
83 const float offset = RNA_float_get(op->ptr, "offset");
84 const float uniform = RNA_float_get(op->ptr, "uniform");
85 const float normal_factor = RNA_float_get(op->ptr, "normal");
86 const uint seed = RNA_int_get(op->ptr, "seed");
87
88 bool changed_multi = false;
90 scene, view_layer, CTX_wm_view3d(C), eObjectMode(ob_mode));
91 for (const int ob_index : objects.index_range()) {
92 Object *ob_iter = objects[ob_index];
93
94 TransVertStore tvs = {nullptr};
95
96 if (ob_iter) {
97 int mode = TM_ALL_JOINTS;
98
99 if (normal_factor != 0.0f) {
100 mode |= TX_VERT_USE_NORMAL;
101 }
102
103 if (shape_key_report_if_locked(ob_iter, op->reports)) {
104 continue;
105 }
106
107 ED_transverts_create_from_obedit(&tvs, ob_iter, mode);
108 if (tvs.transverts_tot == 0) {
109 continue;
110 }
111
112 int seed_iter = seed;
113 /* This gives a consistent result regardless of object order. */
114 if (ob_index) {
115 seed_iter += BLI_ghashutil_strhash_p(ob_iter->id.name);
116 }
117
118 object_rand_transverts(&tvs, offset, uniform, normal_factor, seed_iter);
119
120 ED_transverts_update_obedit(&tvs, ob_iter);
121 ED_transverts_free(&tvs);
122
124 changed_multi = true;
125 }
126 }
127
128 return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
129}
130
132{
133 /* identifiers */
134 ot->name = "Randomize";
135 ot->description = "Randomize vertices";
136 ot->idname = "TRANSFORM_OT_vertex_random";
137
138 /* API callbacks. */
140 ot->poll = ED_transverts_poll;
141
142 /* flags */
144
145 /* props */
147 ot->srna, "offset", 0.0f, -FLT_MAX, FLT_MAX, "Amount", "Distance to offset", -10.0f, 10.0f);
149 "uniform",
150 0.0f,
151 0.0f,
152 1.0f,
153 "Uniform",
154 "Increase for uniform offset distance",
155 0.0f,
156 1.0f);
158 "normal",
159 0.0f,
160 0.0f,
161 1.0f,
162 "Normal",
163 "Align offset direction to normals",
164 0.0f,
165 1.0f);
167 ot->srna, "seed", 0, 0, 10000, "Random Seed", "Seed for the random number generator", 0, 50);
168
169 /* Set generic modal callbacks. */
171}
172
173} // namespace blender::ed::object
Scene * CTX_data_scene(const bContext *C)
Object * CTX_data_edit_object(const bContext *C)
View3D * CTX_wm_view3d(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
blender::Vector< Object * > BKE_view_layer_array_from_objects_in_mode_unique_data(const Scene *scene, ViewLayer *view_layer, const View3D *v3d, eObjectMode mode)
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)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
unsigned int uint
eObjectMode
Object is a sort of wrapper for general info.
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
bool ED_transverts_poll(bContext *C)
void ED_transverts_create_from_obedit(TransVertStore *tvs, const Object *obedit, int mode)
void ED_transverts_free(TransVertStore *tvs)
void ED_transverts_update_obedit(TransVertStore *tvs, Object *obedit)
@ TX_VERT_USE_NORMAL
@ TM_ALL_JOINTS
#define C
Definition RandGen.cpp:29
#define ND_DRAW
Definition WM_types.hh:458
#define NC_OBJECT
Definition WM_types.hh:376
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
static unsigned long seed
Definition btSoftBody.h:39
IndexRange index_range() const
bool shape_key_report_if_locked(const Object *obedit, ReportList *reports)
static wmOperatorStatus object_rand_verts_exec(bContext *C, wmOperator *op)
static bool object_rand_transverts(TransVertStore *tvs, const float offset, const float uniform, const float normal_factor, const uint seed)
void TRANSFORM_OT_vertex_random(wmOperatorType *ot)
VecBase< float, 3 > float3
int RNA_int_get(PointerRNA *ptr, const char *name)
float RNA_float_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
PropertyRNA * RNA_def_float_distance(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
#define FLT_MAX
Definition stdcycles.h:14
char name[66]
Definition DNA_ID.h:415
TransVert * transverts
float * loc
float normal[3]
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition wm_files.cc:4226
void WM_operator_type_modal_from_exec_for_object_edit_coords(wmOperatorType *ot)