[Bf-blender-cvs] [f2b04302cde] master: Fluid: Refactored Python pointer update function

Sebastián Barschkis noreply at git.blender.org
Fri Jul 31 16:36:52 CEST 2020


Commit: f2b04302cdecb511879cde972e314bca934a32dd
Author: Sebastián Barschkis
Date:   Fri Jul 31 15:27:01 2020 +0200
Branches: master
https://developer.blender.org/rBf2b04302cdecb511879cde972e314bca934a32dd

Fluid: Refactored Python pointer update function

Use static_cast() where possible and refresh pointers for every frame when in replay mode. The latter is particularly important as this seems to have caused the issue where smoke in the viewport was flickering when loading data from pointers after loading them from disk for the frame before (e.g. when resuming a bake job).

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

M	intern/mantaflow/extern/manta_fluid_API.h
M	intern/mantaflow/intern/MANTA_main.cpp
M	intern/mantaflow/intern/MANTA_main.h
M	intern/mantaflow/intern/manta_fluid_API.cpp
M	source/blender/blenkernel/intern/fluid.c

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

diff --git a/intern/mantaflow/extern/manta_fluid_API.h b/intern/mantaflow/extern/manta_fluid_API.h
index 3da1d8f53f0..f3fa01b5121 100644
--- a/intern/mantaflow/extern/manta_fluid_API.h
+++ b/intern/mantaflow/extern/manta_fluid_API.h
@@ -77,6 +77,7 @@ int manta_get_frame(struct MANTA *fluid);
 float manta_get_timestep(struct MANTA *fluid);
 void manta_adapt_timestep(struct MANTA *fluid);
 bool manta_needs_realloc(struct MANTA *fluid, struct FluidModifierData *fmd);
+void manta_update_pointers(struct MANTA *fluid, struct FluidModifierData *fmd);
 
 /* Fluid accessors */
 size_t manta_get_index(int x, int max_x, int y, int max_y, int z /*, int max_z */);
diff --git a/intern/mantaflow/intern/MANTA_main.cpp b/intern/mantaflow/intern/MANTA_main.cpp
index 5b2cbb09979..cbf61d6ff77 100644
--- a/intern/mantaflow/intern/MANTA_main.cpp
+++ b/intern/mantaflow/intern/MANTA_main.cpp
@@ -282,7 +282,8 @@ MANTA::MANTA(int *res, FluidModifierData *fmd) : mCurrentID(++solverID)
         initColorsHigh();
     }
   }
-  updatePointers();
+
+  updatePointers(fmd);
 }
 
 void MANTA::initDomain(FluidModifierData *fmd)
@@ -1823,12 +1824,18 @@ static PyObject *callPythonFunction(string varName, string functionName, bool is
   /* Be sure to initialize Python before using it. */
   Py_Initialize();
 
-  // Get pyobject that holds result value
+  /* Get pyobject that holds result value. */
   if (!manta_main_module) {
     PyGILState_Release(gilstate);
     return nullptr;
   }
 
+  /* Ensure that requested variable is present in module - avoid attribute errors later on. */
+  if (!PyObject_HasAttrString(manta_main_module, varName.c_str())) {
+    PyGILState_Release(gilstate);
+    return nullptr;
+  }
+
   var = PyObject_GetAttrString(manta_main_module, varName.c_str());
   if (!var) {
     PyGILState_Release(gilstate);
@@ -1911,6 +1918,11 @@ static long pyObjectToLong(PyObject *inputObject)
   return result;
 }
 
+template<class T> static T *getPointer(string pyObjectName, string pyFunctionName)
+{
+  return static_cast<T *>(pyObjectToPointer(callPythonFunction(pyObjectName, pyFunctionName)));
+}
+
 int MANTA::getFrame()
 {
   if (with_debug)
@@ -1955,137 +1967,137 @@ void MANTA::adaptTimestep()
   runPythonString(pythonCommands);
 }
 
-void MANTA::updatePointers()
+void MANTA::updatePointers(FluidModifierData *fmd)
 {
   if (with_debug)
     cout << "MANTA::updatePointers()" << endl;
 
+  FluidDomainSettings *fds = fmd->domain;
+
+  bool liquid = (fds->type == FLUID_DOMAIN_TYPE_LIQUID);
+  bool smoke = (fds->type == FLUID_DOMAIN_TYPE_GAS);
+  bool noise = smoke && fds->flags & FLUID_DOMAIN_USE_NOISE;
+  bool heat = smoke && fds->active_fields & FLUID_DOMAIN_ACTIVE_HEAT;
+  bool colors = smoke && fds->active_fields & FLUID_DOMAIN_ACTIVE_COLORS;
+  bool fire = smoke && fds->active_fields & FLUID_DOMAIN_ACTIVE_FIRE;
+  bool obstacle = fds->active_fields & FLUID_DOMAIN_ACTIVE_OBSTACLE;
+  bool guiding = fds->active_fields & FLUID_DOMAIN_ACTIVE_GUIDE;
+  bool invel = fds->active_fields & FLUID_DOMAIN_ACTIVE_INVEL;
+  bool outflow = fds->active_fields & FLUID_DOMAIN_ACTIVE_OUTFLOW;
+  bool drops = liquid && fds->particle_type & FLUID_DOMAIN_PARTICLE_SPRAY;
+  bool bubble = liquid && fds->particle_type & FLUID_DOMAIN_PARTICLE_BUBBLE;
+  bool floater = liquid && fds->particle_type & FLUID_DOMAIN_PARTICLE_FOAM;
+  bool tracer = liquid && fds->particle_type & FLUID_DOMAIN_PARTICLE_TRACER;
+  bool parts = liquid && (drops | bubble | floater | tracer);
+  bool mesh = liquid && fds->flags & FLUID_DOMAIN_USE_MESH;
+  bool meshvel = liquid && mesh && fds->flags & FLUID_DOMAIN_USE_SPEED_VECTORS;
+
   string func = "getDataPointer";
   string funcNodes = "getNodesDataPointer";
   string funcTris = "getTrisDataPointer";
 
   string id = to_string(mCurrentID);
-  string solver = "s" + id;
-  string parts = "pp" + id;
-  string snd = "sp" + id;
-  string mesh = "sm" + id;
-  string mesh2 = "mesh" + id;
-  string noise = "sn" + id;
-  string solver_ext = "_" + solver;
-  string parts_ext = "_" + parts;
-  string snd_ext = "_" + snd;
-  string mesh_ext = "_" + mesh;
-  string mesh_ext2 = "_" + mesh2;
-  string noise_ext = "_" + noise;
-
-  mFlags = (int *)pyObjectToPointer(callPythonFunction("flags" + solver_ext, func));
-  mPhiIn = (float *)pyObjectToPointer(callPythonFunction("phiIn" + solver_ext, func));
-  mPhiStaticIn = (float *)pyObjectToPointer(callPythonFunction("phiSIn" + solver_ext, func));
-  mVelocityX = (float *)pyObjectToPointer(callPythonFunction("x_vel" + solver_ext, func));
-  mVelocityY = (float *)pyObjectToPointer(callPythonFunction("y_vel" + solver_ext, func));
-  mVelocityZ = (float *)pyObjectToPointer(callPythonFunction("z_vel" + solver_ext, func));
-  mForceX = (float *)pyObjectToPointer(callPythonFunction("x_force" + solver_ext, func));
-  mForceY = (float *)pyObjectToPointer(callPythonFunction("y_force" + solver_ext, func));
-  mForceZ = (float *)pyObjectToPointer(callPythonFunction("z_force" + solver_ext, func));
-
-  if (mUsingOutflow) {
-    mPhiOutIn = (float *)pyObjectToPointer(callPythonFunction("phiOutIn" + solver_ext, func));
-    mPhiOutStaticIn = (float *)pyObjectToPointer(
-        callPythonFunction("phiOutSIn" + solver_ext, func));
-  }
-  if (mUsingObstacle) {
-    mPhiObsIn = (float *)pyObjectToPointer(callPythonFunction("phiObsIn" + solver_ext, func));
-    mPhiObsStaticIn = (float *)pyObjectToPointer(
-        callPythonFunction("phiObsSIn" + solver_ext, func));
-    mObVelocityX = (float *)pyObjectToPointer(callPythonFunction("x_obvel" + solver_ext, func));
-    mObVelocityY = (float *)pyObjectToPointer(callPythonFunction("y_obvel" + solver_ext, func));
-    mObVelocityZ = (float *)pyObjectToPointer(callPythonFunction("z_obvel" + solver_ext, func));
-    mNumObstacle = (float *)pyObjectToPointer(callPythonFunction("numObs" + solver_ext, func));
-  }
-  if (mUsingGuiding) {
-    mPhiGuideIn = (float *)pyObjectToPointer(callPythonFunction("phiGuideIn" + solver_ext, func));
-    mGuideVelocityX = (float *)pyObjectToPointer(
-        callPythonFunction("x_guidevel" + solver_ext, func));
-    mGuideVelocityY = (float *)pyObjectToPointer(
-        callPythonFunction("y_guidevel" + solver_ext, func));
-    mGuideVelocityZ = (float *)pyObjectToPointer(
-        callPythonFunction("z_guidevel" + solver_ext, func));
-    mNumGuide = (float *)pyObjectToPointer(callPythonFunction("numGuides" + solver_ext, func));
-  }
-  if (mUsingInvel) {
-    mInVelocityX = (float *)pyObjectToPointer(callPythonFunction("x_invel" + solver_ext, func));
-    mInVelocityY = (float *)pyObjectToPointer(callPythonFunction("y_invel" + solver_ext, func));
-    mInVelocityZ = (float *)pyObjectToPointer(callPythonFunction("z_invel" + solver_ext, func));
-  }
-  if (mUsingSmoke) {
-    mDensity = (float *)pyObjectToPointer(callPythonFunction("density" + solver_ext, func));
-    mDensityIn = (float *)pyObjectToPointer(callPythonFunction("densityIn" + solver_ext, func));
-    mShadow = (float *)pyObjectToPointer(callPythonFunction("shadow" + solver_ext, func));
-    mEmissionIn = (float *)pyObjectToPointer(callPythonFunction("emissionIn" + solver_ext, func));
-  }
-  if (mUsingSmoke && mUsingHeat) {
-    mHeat = (float *)pyObjectToPointer(callPythonFunction("heat" + solver_ext, func));
-    mHeatIn = (float *)pyObjectToPointer(callPythonFunction("heatIn" + solver_ext, func));
-  }
-  if (mUsingSmoke && mUsingFire) {
-    mFlame = (float *)pyObjectToPointer(callPythonFunction("flame" + solver_ext, func));
-    mFuel = (float *)pyObjectToPointer(callPythonFunction("fuel" + solver_ext, func));
-    mReact = (float *)pyObjectToPointer(callPythonFunction("react" + solver_ext, func));
-    mFuelIn = (float *)pyObjectToPointer(callPythonFunction("fuelIn" + solver_ext, func));
-    mReactIn = (float *)pyObjectToPointer(callPythonFunction("reactIn" + solver_ext, func));
-  }
-  if (mUsingSmoke && mUsingColors) {
-    mColorR = (float *)pyObjectToPointer(callPythonFunction("color_r" + solver_ext, func));
-    mColorG = (float *)pyObjectToPointer(callPythonFunction("color_g" + solver_ext, func));
-    mColorB = (float *)pyObjectToPointer(callPythonFunction("color_b" + solver_ext, func));
-    mColorRIn = (float *)pyObjectToPointer(callPythonFunction("color_r_in" + solver_ext, func));
-    mColorGIn = (float *)pyObjectToPointer(callPythonFunction("color_g_in" + solver_ext, func));
-    mColorBIn = (float *)pyObjectToPointer(callPythonFunction("color_b_in" + solver_ext, func));
-  }
-  if (mUsingSmoke && mUsingNoise) {
-    mDensityHigh = (float *)pyObjectToPointer(callPythonFunction("density" + noise_ext, func));
-    mTextureU = (float *)pyObjectToPointer(callPythonFunction("texture_u" + solver_ext, func));
-    mTextureV = (float *)pyObjectToPointer(callPythonFunction("texture_v" + solver_ext, func));
-    mTextureW = (float *)pyObjectToPointer(callPythonFunction("texture_w" + solver_ext, func));
-    mTextureU2 = (float *)pyObjectToPointer(callPythonFunction("texture_u2" + solver_ext, func));
-    mTextureV2 = (float *)pyObjectToPointer(callPythonFunction("texture_v2" + solver_ext, func));
-    mTextureW2 = (float *)pyObjectToPointer(callPythonFunction("texture_w2" + solver_ext, func));
-  }
-  if (mUsingSmoke && mUsingNoise && mUsingFire) {
-    mFlameHigh = (float *)pyObjectToPointer(callPythonFunction("flame" + noise_ext, func));
-    mFuelHigh = (float *)pyObjectToPointer(callPythonFunction("fuel" + noise_ext, func));
-    mReactHigh = (float *)pyObjectToPointer(callPythonFunction("react" + noise_ext, func));
-  }
-  if (mUsingSmoke && mUsingNoise && mUsingColors) {
-    mColorRHigh = (float *)pyObjectToPointer(callPythonFunction("color_r" + noise_ext, func));
-    mColorGHigh = (float *)pyObjectToPointer(callPythonFunction("color_g" + noise_ext, func));
-    mColorBHigh = (float *)pyObjectToPointer(callPythonFunction("color_b" + noise_ext, func));
-  }
-  if (mUsingLiquid) {
-    mPhi = (float *)pyObjectToPointer(callPythonFunction("phi" + solver_ext, func));
-    mFlipParticleData = (vector<pData> *)pyObjectToPointer(
-        callPythonFunction("pp" + solver_ext, func));
-    mFlipParticleVelocity = (vector<pVel> *)pyObjectToPointer(
-        callPythonFunction("pVel" + parts_ext, func));
- 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list