Skip to content

Commit 9f261a3

Browse files
committed
fixes bug where meshbodies with less than 20 vertices caused a division by 0 in the progress dialog. fixes #3
Also adds new experimental dimension settings for value noise.
1 parent 1add7b2 commit 9f261a3

9 files changed

+36
-30
lines changed

Noise, Patterns and Textures.manifest

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"": ""
88
},
99
"version": "0.1",
10-
"runOnStartup": false,
10+
"runOnStartup": true,
1111
"supportedOS": "windows|mac",
1212
"editEnabled": true
1313
}

Noise, Patterns and Textures.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ def notify(self, args):
148148
resolutionYField = inputs.addIntegerSpinnerCommandInput('resolutionYField', 'ResolutionY', 2, 1000, 1, 10)
149149
resolutionYField.tooltip = "Sets the y-resolution of the applied noise function. \nThe higher the resolution, the more features are visible."
150150

151+
resolutionZField = inputs.addIntegerSpinnerCommandInput('resolutionZField', 'ResolutionZ', 2, 1000, 1, 10)
152+
resolutionZField.tooltip = "Sets the z-resolution of the applied noise function. \nThe higher the resolution, the more features are visible."
153+
151154
# Plane input for 2D noise
152155
#planeInput = inputs.addSelectionInput('planeInput', 'Plane', 'Select Plane to apply noise to.')
153156
#planeInput.addSelectionFilter('ConstructionPlanes')
@@ -249,6 +252,7 @@ def notify(self, args):
249252
advancedGroup = inputs.itemById('advancedGroup')
250253
resolutionField = inputs.itemById('resolutionField')
251254
resolutionYField = inputs.itemById('resolutionYField')
255+
resolutionZField = inputs.itemById('resolutionZField')
252256
frequencyField = inputs.itemById('frequencyField')
253257
imageField = inputs.itemById('imageField')
254258
fileDialogButton = inputs.itemById('fileDialogButton')
@@ -271,6 +275,7 @@ def notify(self, args):
271275
smoothBox.isVisible = True
272276
resolutionField.isVisible = True
273277
resolutionYField.isVisible = True
278+
resolutionZField.isVisible = True
274279
#frequencyField.isVisible = True
275280
algDescriptionBox.text = "Generates noise based on a continous function. The dimension of the function can be specified as well as the resolution."
276281
elif changedInput.selectedItem.name == 'Perlin Noise':
@@ -370,7 +375,7 @@ def notify(self, args):
370375
product = app.activeProduct #the fusion tab that is active
371376
rootComp = product.rootComponent # the root component of the active product
372377
meshBodies = rootComp.meshBodies
373-
algorithm, seed, degree, dimension3, dimension2, signed, smooth, resolution, resolutionY, frequency, inverse, stepActive, stepPadding, planeString = getInputs(inputs)
378+
algorithm, seed, degree, dimension3, dimension2, signed, smooth, resolution, resolutionY, resolutionZ, frequency, inverse, stepActive, stepPadding, planeString = getInputs(inputs)
374379

375380
currentPreview = []
376381
currentPreviewMesh = []
@@ -382,13 +387,13 @@ def notify(self, args):
382387
mesh = selection.mesh
383388
body = meshHelper.fusionPolygonMeshToBody(mesh)
384389

385-
computeNoise(progressDialog, algorithm, seed, degree, dimension3, dimension2, signed, smooth, resolution, resolutionY, frequency, inverse, stepActive, stepPadding, planeString, body)
390+
computeNoise(progressDialog, algorithm, seed, degree, dimension3, dimension2, signed, smooth, resolution, resolutionY, resolutionZ, frequency, inverse, stepActive, stepPadding, planeString, body)
386391

387392
selection.isLightBulbOn = False
388393
currentPreview.append(body)
389394
currentPreviewMesh.append(mesh)
390395
showMeshPreview(body,mesh)
391-
#app.activeViewport.refresh()
396+
app.activeViewport.refresh()
392397
except ValueError as err:
393398
if 'CanceledProgress'in err.args:
394399
currentPreview = []
@@ -421,7 +426,7 @@ def notify(self, args):
421426
eventArgs = adsk.core.CommandEventArgs.cast(args)
422427
# Get the values from the command inputs.
423428
inputs = eventArgs.command.commandInputs
424-
algorithm, seed, degree, dimension3, dimension2, signed, smooth, resolution, resolutionY, frequency, inverse, stepActive, stepPadding, planeString = getInputs(inputs)
429+
algorithm, seed, degree, dimension3, dimension2, signed, smooth, resolution, resolutionY, resolutionZ, frequency, inverse, stepActive, stepPadding, planeString = getInputs(inputs)
425430

426431
selectionList = []
427432
for i in range(inputs.itemById('body_input').selectionCount):
@@ -433,7 +438,7 @@ def notify(self, args):
433438
if len(currentPreview) > 0:
434439
body = currentPreview[i]
435440
else:
436-
computeNoise(progressDialog, algorithm, seed, degree, dimension3, dimension2, signed, smooth, resolution, resolutionY, frequency, inverse, stepActive, stepPadding, planeString, body)
441+
computeNoise(progressDialog, algorithm, seed, degree, dimension3, dimension2, signed, smooth, resolution, resolutionY, resolutionZ, frequency, inverse, stepActive, stepPadding, planeString, body)
437442

438443
# Hide the original meshBody, add and name the new one
439444
selection.isLightBulbOn = False
@@ -459,6 +464,7 @@ def getInputs(inputs):
459464
smooth = inputs.itemById('smoothBox').value
460465
resolution = inputs.itemById('resolutionField').value
461466
resolutionY = inputs.itemById('resolutionYField').value
467+
resolutionZ = inputs.itemById('resolutionZField').value
462468
frequency = inputs.itemById('frequencyField').value
463469
inverse = inputs.itemById('inverseBox').value
464470
stepActive = inputs.itemById('stepGroup').isEnabledCheckBoxChecked
@@ -473,9 +479,9 @@ def getInputs(inputs):
473479
planeString = 'xZ'
474480
elif planeInput.selectedItem.name == "yZ":
475481
planeString = 'yZ'
476-
return algorithm,seed,degree,dimension3,dimension2,signed,smooth,resolution,resolutionY,frequency,inverse,stepActive,stepPadding,planeString
482+
return algorithm,seed,degree,dimension3,dimension2,signed,smooth,resolution,resolutionY,resolutionZ,frequency,inverse,stepActive,stepPadding,planeString
477483

478-
def computeNoise(progressDialog, algorithm, seed, degree, dimension3, dimension2, signed, smooth, resolution, resolutionY, frequency, inverse, stepActive, stepPadding, planeString, body):
484+
def computeNoise(progressDialog, algorithm, seed, degree, dimension3, dimension2, signed, smooth, resolution, resolutionY, resolutionZ, frequency, inverse, stepActive, stepPadding, planeString, body):
479485
progressDialog.show('Computing Noise...', 'Percentage: %p% - %v/%m steps completed',0,len(body.vertices),2)
480486
if algorithm == 'Adaptive Noise':
481487
progressDialog.show('Computing Noise...', 'Percentage: %p% - %v/%m steps completed',0,len(body.facets),2)
@@ -488,7 +494,7 @@ def computeNoise(progressDialog, algorithm, seed, degree, dimension3, dimension2
488494
elif dimension3 == '2D':
489495
valueNoise2D(body,resolution,resolutionY,degree,frequency,signed,smooth,seed, progressDialog)
490496
elif dimension3 == '3D':
491-
valueNoise3D(body,resolution,degree,frequency,signed,smooth,seed, progressDialog)
497+
valueNoise3D(body,resolution,resolutionY,resolutionZ,degree,frequency,signed,smooth,seed, progressDialog)
492498
elif algorithm == 'Perlin Noise':
493499
if dimension2 == '2D':
494500
perlinNoise2D(body,resolution,degree,frequency,signed,smooth,seed,progressDialog)

noises/grungeMapNoise.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def getNoiseValue(x,y):
5858
if progressDialog:
5959
if progressDialog.wasCancelled:
6060
raise ValueError('CanceledProgress')
61-
if i%int(allSteps/20)==0:
61+
if i%(int(allSteps/20)+1)==0:
6262
progressDialog.progressValue = i+1
6363
elif i == 0:
6464
progressDialog.progressValue = i+1

noises/perlinNoise.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def getNoiseValue(x,y,z):
100100
if progressDialog:
101101
if progressDialog.wasCancelled:
102102
raise ValueError('CanceledProgress')
103-
if i%int(allSteps/20)==0:
103+
if i%(int(allSteps/20)+1)==0:
104104
progressDialog.progressValue = i+1
105105
elif i > maxSteps:
106106
progressDialog.progressValue = progressDialog.maximumValue
@@ -188,7 +188,7 @@ def getNoiseValue(x,y):
188188
if progressDialog:
189189
if progressDialog.wasCancelled:
190190
raise ValueError('CanceledProgress')
191-
if i%int(allSteps/20)==0:
191+
if i%(int(allSteps/20)+1)==0:
192192
progressDialog.progressValue = i+1
193193
elif i > maxSteps:
194194
progressDialog.progressValue = progressDialog.maximumValue

noises/randomNoise.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def adaptiveVertexDistortion(body, degree:float, inverse=False, seed:int=None, p
7676
if progressDialog:
7777
if progressDialog.wasCancelled:
7878
raise ValueError('CanceledProgress')
79-
if i%int(allSteps/20)==0:
79+
if i%(int(allSteps/20)+1)==0:
8080
progressDialog.progressValue = i+1
8181
elif i > maxSteps:
8282
progressDialog.progressValue = progressDialog.maximumValue

noises/valueNoise.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33
from ..helpers.mathHelper import *
44
from adsk.core import ProgressDialog, Vector2D, Vector3D
55

6-
def valueNoise3D(body:Body, resolution:int, amplitude:float=1, frequency:float=1, signed:bool=True, smooth:bool=True, seed:int=None, progressDialog:ProgressDialog=None) -> Body:
6+
def valueNoise3D(body:Body, resolutionX:int, resolutionY:int, resolutionZ:int, amplitude:float=1, frequency:float=1, signed:bool=True, smooth:bool=True, seed:int=None, progressDialog:ProgressDialog=None) -> Body:
77
if seed:
88
random.seed(seed)
99
xValues = [v[0] for v in body.vertices]
1010
yValues = [v[1] for v in body.vertices]
1111
zValues = [v[2] for v in body.vertices]
1212
#make a lattice grid
1313
lattice = []
14-
for i in range(resolution): # This is the ugliest matrix init of all time
14+
for i in range(resolutionX): # This is the ugliest matrix init of all time
1515
lattice.append([])
16-
for j in range(resolution):
16+
for j in range(resolutionY):
1717
lattice[i].append([])
18-
for k in range(resolution):
18+
for k in range(resolutionZ):
1919
if signed:
2020
lattice[i][j].append(random.uniform(-1,1))
2121
else:
2222
lattice[i][j].append(random.uniform(0,1))
2323

2424
def getNoiseValue(x,y,z):
2525
xMin = min(int(x),len(lattice)-1)
26-
yMin = min(int(y),len(lattice)-1)
27-
zMin = min(int(z),len(lattice)-1)
26+
yMin = min(int(y),len(lattice[0])-1)
27+
zMin = min(int(z),len(lattice[0][0])-1)
2828
tx = x - xMin
2929
ty = y - yMin
3030
tz = z - zMin
@@ -36,9 +36,9 @@ def getNoiseValue(x,y,z):
3636
rx0 = xMin
3737
rx1 = min(xMin+1,len(lattice)-1)
3838
ry0 = yMin
39-
ry1 = min(yMin+1,len(lattice)-1)
39+
ry1 = min(yMin+1,len(lattice[0])-1)
4040
rz0 = zMin
41-
rz1 = min(zMin+1,len(lattice)-1)
41+
rz1 = min(zMin+1,len(lattice[0][0])-1)
4242

4343
c000 = lattice[rx0][ry0][rz0]
4444
c100 = lattice[rx1][ry0][rz0]
@@ -66,10 +66,10 @@ def getNoiseValue(x,y,z):
6666
minZ = min(zValues)
6767
maxZ = max(zValues)
6868

69-
xValuesScaled = [(x- minX)/(maxX-minX) * (resolution-1) for x in xValues]
70-
yValuesScaled = [(y- minY)/(maxY-minY) * (resolution-1) for y in yValues]
69+
xValuesScaled = [(x- minX)/(maxX-minX) * (resolutionX-1) for x in xValues]
70+
yValuesScaled = [(y- minY)/(maxY-minY) * (resolutionY-1) for y in yValues]
7171
if not maxZ == minZ:
72-
zValuesScaled = [(z- minZ)/(maxZ-minZ) * (resolution-1) for z in zValues]
72+
zValuesScaled = [(z- minZ)/(maxZ-minZ) * (resolutionZ-1) for z in zValues]
7373
else:
7474
zValuesScaled = zValues
7575
xyzValuesScaled = list(zip(xValuesScaled,yValuesScaled,zValuesScaled))
@@ -81,7 +81,7 @@ def getNoiseValue(x,y,z):
8181
if progressDialog:
8282
if progressDialog.wasCancelled:
8383
raise ValueError('CanceledProgress')
84-
if i%int(allSteps/20)==0:
84+
if i%(int(allSteps/20)+1)==0:
8585
progressDialog.progressValue = i+1
8686
elif i > maxSteps:
8787
progressDialog.progressValue = progressDialog.maximumValue
@@ -106,7 +106,7 @@ def valueNoise2D(body:Body, resolutionX:int, resolutionY:int, amplitude:float=1,
106106
random.seed(seed)
107107
xValues = [v[0] for v in body.vertices]
108108
yValues = [v[1] for v in body.vertices]
109-
zValues = [v[2] for v in body.vertices]
109+
#zValues = [v[2] for v in body.vertices]
110110

111111
#make a lattice grid
112112
lattice = []
@@ -157,7 +157,7 @@ def getNoiseValue(x,y):
157157
if progressDialog:
158158
if progressDialog.wasCancelled:
159159
raise ValueError('CanceledProgress')
160-
if i%int(allSteps/20)==0:
160+
if i%(int(allSteps/20)+1)==0:
161161
progressDialog.progressValue = i+1
162162
elif i > maxSteps:
163163
progressDialog.progressValue = progressDialog.maximumValue
@@ -214,7 +214,7 @@ def getNoiseValue(x):
214214
if progressDialog:
215215
if progressDialog.wasCancelled:
216216
raise ValueError('CanceledProgress')
217-
if i%int(allSteps/20)==0:
217+
if i%(int(allSteps/20)+1)==0:
218218
progressDialog.progressValue = i+1
219219
elif i > maxSteps:
220220
progressDialog.progressValue = progressDialog.maximumValue

noises/worleyNoise.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def distance3D(a,b):
4040
if progressDialog:
4141
if progressDialog.wasCancelled:
4242
raise ValueError('CanceledProgress')
43-
if i%int(allSteps/20)==0:
43+
if i%(int(allSteps/20)+1)==0:
4444
progressDialog.progressValue = i+1
4545
elif i > maxSteps:
4646
progressDialog.progressValue = progressDialog.maximumValue
@@ -107,7 +107,7 @@ def distance2D(a,b):
107107
if progressDialog:
108108
if progressDialog.wasCancelled:
109109
raise ValueError('CanceledProgress')
110-
if i%int(allSteps/20)==0:
110+
if i%(int(allSteps/20)+1)==0:
111111
progressDialog.progressValue = i+1
112112
elif i > maxSteps:
113113
progressDialog.progressValue = progressDialog.maximumValue

resources/readme/sphereNoise.png

791 KB
Loading

resources/usage.mov

-1.31 MB
Binary file not shown.

0 commit comments

Comments
 (0)