From 51b6f00666d2b44bc5f978bb6cea515b0695e5f2 Mon Sep 17 00:00:00 2001
From: Bastien Montagne <montagne29@wanadoo.fr>
Date: Wed, 29 Oct 2014 10:08:15 +0100
Subject: [PATCH] Fix T42138: division by zero in sapling addon

Note sapling code probably has other places where that kind of issues may happen,
would need a full recheck, but that's outside of scope of basic bugfix.

Patch by mangostaniko (Nikolaus Leopold) with minor changes, thanks!

Revision: D844
---
 add_curve_sapling/utils.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/add_curve_sapling/utils.py b/add_curve_sapling/utils.py
index acc95b4e2..f17ecb8b8 100644
--- a/add_curve_sapling/utils.py
+++ b/add_curve_sapling/utils.py
@@ -196,7 +196,7 @@ def interpStem(stem,tVals,lPar,parRad):
     # Loop through all the parametric values to be determined
     for t in tVals:
         if (t >= checkVal) and (t < 1.0):
-            scaledT = (t-checkVal)/(tVals[-1]-checkVal)
+            scaledT = (t - checkVal) / max(tVals[-1] - checkVal, 1e-6)
             length = (numPoints-1)*t#scaledT
             index = int(length)
             if scaledT == 1.0:
@@ -586,13 +586,13 @@ def fabricate_stems(addsplinetobone, addstem, baseSize, branches, childP, cu, cu
         tempPos.rotate(rotMat)
         tempPos.rotate(p.quat)
         newPoint.handle_right = p.co + tempPos
-        if (storeN>= levels-1):
+        if (storeN >= levels):
             # If this is the last level before leaves then we need to generate the child points differently
             branchL = (length[n] + uniform(-lengthV[n], lengthV[n])) * (p.lengthPar - 0.6 * p.offset)
             if leaves < 0:
                 childStems = False
             else:
-                childStems = leaves * shapeRatio(leafDist, p.offset / p.lengthPar)
+                childStems = leaves * shapeRatio(leafDist, p.offset / max(p.lengthPar, 1e-6))
         elif n == 1:
             # If this is the first level of branching then upward attraction has no effect and a special formula is used to find branch length and the number of child stems
             vertAtt = 0.0
-- 
GitLab