Fix #105067: Node Wrangler: cannot preview multi-user material group
[blender-addons.git] / add_mesh_extra_objects / add_mesh_rocks / utils.py
blobeb748b09d25fe6718836e349259651be01889d19
1 # SPDX-FileCopyrightText: 2019-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # Converts a formatted string to a float tuple:
6 # IN - '(0.5, 0.2)' -> CONVERT -> OUT - (0.5, 0.2)
7 def toTuple(stringIn):
8 sTemp = str(stringIn)[1:len(str(stringIn)) - 1].split(', ')
9 fTemp = []
10 for i in sTemp:
11 fTemp.append(float(i))
12 return tuple(fTemp)
15 # Converts a formatted string to a float tuple:
16 # IN - '[0.5, 0.2]' -> CONVERT -> OUT - [0.5, 0.2]
17 def toList(stringIn):
18 sTemp = str(stringIn)[1:len(str(stringIn)) - 1].split(', ')
19 fTemp = []
20 for i in sTemp:
21 fTemp.append(float(i))
22 return fTemp
25 # Converts each item of a list into a float:
26 def toFloats(inList):
27 outList = []
28 for i in inList:
29 outList.append(float(i))
30 return outList
33 # Converts each item of a list into an integer:
34 def toInts(inList):
35 outList = []
36 for i in inList:
37 outList.append(int(i))
38 return outList
41 # Sets all faces smooth. Done this way since I can't
42 # find a simple way without using bpy.ops:
43 def smooth(mesh):
44 import bmesh
45 bm = bmesh.new()
46 bm.from_mesh(mesh)
47 for f in bm.faces:
48 f.smooth = True
49 bm.to_mesh(mesh)
50 return mesh
53 # This try block allows for the script to psudo-intelligently select the
54 # appropriate random to use. If Numpy's random is present it will use that.
55 # If Numpy's random is not present, it will through a "module not found"
56 # exception and instead use the slower built-in random that Python has.
57 try:
58 # from numpy.random import random_integers as randint
59 from numpy.random import normal as gauss
60 # from numpy.random import (beta,
61 # uniform,
62 # seed,
63 # weibull)
64 # print("Rock Generator: Numpy found.")
65 numpy = True
66 except:
67 from random import (
68 # randint,
69 gauss,
70 # uniform,
71 # seed
73 # from random import betavariate as beta
74 # from random import weibullvariate as weibull
75 print("Rock Generator: Numpy not found. Using Python's random.")
76 numpy = False
77 # Artificially skews a normal (gaussian) distribution. This will not create
78 # a continuous distribution curve but instead acts as a piecewise finction.
79 # This linearly scales the output on one side to fit the bounds.
81 # Example output histograms:
83 # Upper skewed: Lower skewed:
84 # | ▄ | _
85 # | █ | █
86 # | █_ | █
87 # | ██ | _█
88 # | _██ | ██
89 # | _▄███_ | ██ _
90 # | ▄██████ | ▄██▄█▄_
91 # | _█▄███████ | ███████
92 # | _██████████_ | ████████▄▄█_ _
93 # | _▄▄████████████ | ████████████▄█_
94 # | _▄_ ▄███████████████▄_ | _▄███████████████▄▄_
95 # ------------------------- -----------------------
96 # |mu |mu
97 # Histograms were generated in R (http://www.r-project.org/) based on the
98 # calculations below and manually duplicated here.
100 # param: mu - mu is the mean of the distribution.
101 # sigma - sigma is the standard deviation of the distribution.
102 # bounds - bounds[0] is the lower bound and bounds[1]
103 # is the upper bound.
104 # upperSkewed - if the distribution is upper skewed.
105 # return: out - Rondomly generated value from the skewed distribution.
107 # @todo: Because NumPy's random value generators are faster when called
108 # a bunch of times at once, maybe allow this to generate and return
109 # multiple values at once?
112 def skewedGauss(mu, sigma, bounds, upperSkewed=True):
113 raw = gauss(mu, sigma)
115 # Quicker to check an extra condition than do unnecessary math. . . .
116 if raw < mu and not upperSkewed:
117 out = ((mu - bounds[0]) / (3 * sigma)) * raw + ((mu * (bounds[0] - (mu - 3 * sigma))) / (3 * sigma))
118 elif raw > mu and upperSkewed:
119 out = ((mu - bounds[1]) / (3 * -sigma)) * raw + ((mu * (bounds[1] - (mu + 3 * sigma))) / (3 * -sigma))
120 else:
121 out = raw
123 return out
126 # @todo create a def for generating an alpha and beta for a beta distribution
127 # given a mu, sigma, and an upper and lower bound. This proved faster in
128 # profiling in addition to providing a much better distribution curve
129 # provided multiple iterations happen within this function; otherwise it was
130 # slower.
131 # This might be a scratch because of the bounds placed on mu and sigma:
133 # For alpha > 1 and beta > 1:
134 # mu^2 - mu^3 mu^3 - mu^2 + mu
135 # ----------- < sigma < ----------------
136 # 1 + mu 2 - mu
138 # def generateBeta(mu, sigma, scale, repitions=1):
139 # results = []
141 # return results