[Bf-blender-cvs] [5114a48fd7c] functions: correct usage of attribute offsets that are not available
Jacques Lucke
noreply at git.blender.org
Thu Jul 11 17:15:11 CEST 2019
Commit: 5114a48fd7c048cbd9521e340b1e9bef03090aa8
Author: Jacques Lucke
Date: Thu Jul 11 10:15:56 2019 +0200
Branches: functions
https://developer.blender.org/rB5114a48fd7c048cbd9521e340b1e9bef03090aa8
correct usage of attribute offsets that are not available
===================================================================
M source/blender/simulations/bparticles/actions.cpp
M source/blender/simulations/bparticles/attributes.hpp
M source/blender/simulations/bparticles/emitters.cpp
===================================================================
diff --git a/source/blender/simulations/bparticles/actions.cpp b/source/blender/simulations/bparticles/actions.cpp
index 073df6faf83..8cc736687da 100644
--- a/source/blender/simulations/bparticles/actions.cpp
+++ b/source/blender/simulations/bparticles/actions.cpp
@@ -25,8 +25,8 @@ class ChangeDirectionAction : public Action {
{
ParticleSet particles = interface.particles();
auto velocities = particles.attributes().get_float3("Velocity");
- auto position_offsets = interface.attribute_offsets().get_float3("Position");
- auto velocity_offsets = interface.attribute_offsets().get_float3("Velocity");
+ auto position_offsets = interface.attribute_offsets().try_get_float3("Position");
+ auto velocity_offsets = interface.attribute_offsets().try_get_float3("Velocity");
auto caller = m_compute_inputs.get_caller(particles.attributes(), interface.event_info());
@@ -40,8 +40,13 @@ class ChangeDirectionAction : public Action {
float3 direction = fn_out.get<float3>(0);
velocities[pindex] = direction;
- position_offsets[pindex] = direction * interface.remaining_time_in_step(pindex);
- velocity_offsets[pindex] = float3(0);
+
+ if (position_offsets.has_value()) {
+ position_offsets.value()[pindex] = direction * interface.remaining_time_in_step(pindex);
+ }
+ if (velocity_offsets.has_value()) {
+ velocity_offsets.value()[pindex] = float3(0);
+ }
}
m_post_action->execute(interface);
diff --git a/source/blender/simulations/bparticles/attributes.hpp b/source/blender/simulations/bparticles/attributes.hpp
index 295a3676372..830e2435308 100644
--- a/source/blender/simulations/bparticles/attributes.hpp
+++ b/source/blender/simulations/bparticles/attributes.hpp
@@ -9,12 +9,14 @@
#include "BLI_range.hpp"
#include "BLI_small_set_vector.hpp"
#include "BLI_array_allocator.hpp"
+#include "BLI_optional.hpp"
namespace BParticles {
using BLI::ArrayAllocator;
using BLI::ArrayRef;
using BLI::float3;
+using BLI::Optional;
using BLI::Range;
using BLI::SmallSetVector;
using BLI::SmallVector;
@@ -130,6 +132,24 @@ class AttributesInfo {
return m_indices.index(name.to_std_string());
}
+ /**
+ * Get the index corresponding to an attribute with the given name and type.
+ * Returns -1 when the attribute does not exist.
+ */
+ bool attribute_index_try(StringRef name, AttributeType type) const
+ {
+ int index = this->attribute_index_try(name);
+ if (index == -1) {
+ return -1;
+ }
+ else if (this->type_of((uint)index) == type) {
+ return index;
+ }
+ else {
+ return -1;
+ }
+ }
+
/**
* Get the index corresponding to an attribute name.
* Asserts when the attribute does not exist.
@@ -323,7 +343,8 @@ class AttributeArrays {
void init_default(StringRef name);
/**
- * Get access do the underlying attribute arrays.
+ * Get access to the underlying attribute arrays.
+ * Asserts when the attribute does not exists.
*/
ArrayRef<uint8_t> get_byte(uint index) const;
ArrayRef<uint8_t> get_byte(StringRef name);
@@ -332,6 +353,14 @@ class AttributeArrays {
ArrayRef<float3> get_float3(uint index) const;
ArrayRef<float3> get_float3(StringRef name);
+ /**
+ * Get access to the arrays.
+ * Does not assert when the attribute does not exist.
+ */
+ Optional<ArrayRef<uint8_t>> try_get_byte(StringRef name);
+ Optional<ArrayRef<float>> try_get_float(StringRef name);
+ Optional<ArrayRef<float3>> try_get_float3(StringRef name);
+
/**
* Get a continuous slice of the attribute arrays.
*/
@@ -463,6 +492,39 @@ inline ArrayRef<float3> AttributeArrays::get_float3(StringRef name)
return this->get_float3(this->attribute_index(name));
}
+inline Optional<ArrayRef<uint8_t>> AttributeArrays::try_get_byte(StringRef name)
+{
+ int index = this->info().attribute_index_try(name, AttributeType::Byte);
+ if (index == -1) {
+ return {};
+ }
+ else {
+ return this->get_byte((uint)index);
+ }
+}
+
+inline Optional<ArrayRef<float>> AttributeArrays::try_get_float(StringRef name)
+{
+ int index = this->info().attribute_index_try(name, AttributeType::Float);
+ if (index == -1) {
+ return {};
+ }
+ else {
+ return this->get_float((uint)index);
+ }
+}
+
+inline Optional<ArrayRef<float3>> AttributeArrays::try_get_float3(StringRef name)
+{
+ int index = this->info().attribute_index_try(name, AttributeType::Float3);
+ if (index == -1) {
+ return {};
+ }
+ else {
+ return this->get_float3((uint)index);
+ }
+}
+
inline AttributeArrays AttributeArrays::slice(uint start, uint size) const
{
return AttributeArrays(m_core, m_start + start, size);
diff --git a/source/blender/simulations/bparticles/emitters.cpp b/source/blender/simulations/bparticles/emitters.cpp
index 1c2ca82695c..c416782c4ba 100644
--- a/source/blender/simulations/bparticles/emitters.cpp
+++ b/source/blender/simulations/bparticles/emitters.cpp
@@ -33,6 +33,7 @@ class PointEmitter : public Emitter {
auto target = interface.particle_allocator().request(m_particle_type_name, 1);
target.set_float3("Position", {m_point});
target.set_float3("Velocity", {float3{-1, -1, 0}});
+ target.fill_float("Size", 0.01f);
target.fill_float("Birth Time", interface.time_span().end());
}
};
More information about the Bf-blender-cvs
mailing list