[Closed] changing several objects value
ih all,
I want apply this script to several object in one time. Actually it works fine for one object.
What it does: specify a reactor Mass value depending on its volume.
scalevalue = 100
theobjs = selection as arrayfor i in 1 to selection.count do
(setUserProp selection[i] “Mass” (
fn CalculateVolume theobjs =
(
local Volume= 0.0
local Center= [0.0, 0.0, 0.0]
local theMesh = snapshotasmesh theobjs
local numFaces = theMesh.numfaces
for i = 1 to numFaces do
(
local Face= getFace theMesh i
local vert2 = getVert theMesh Face.z
local vert1 = getVert theMesh Face.y
local vert0 = getVert theMesh Face.x
local dV = Dot (Cross (vert1 – vert0) (vert2 – vert0)) vert0
Volume+= dV
)
delete theMesh
Volume /= 6
Center /= 24
Center /= Volume
Volume
)
(CalculateVolume $ )/scalevalue
)
)
First, you should comment your script ;). That makes it easier for us to see, what it should do and where…
Second: Why are you declaring a function in a loop? Pull that thing to the top of the script and just call it in a loop.
Third: Don’t loop twice about the same index. I know one loop is in the function and might not interfere with the outside loop, but since the function is created inside the loop, change the index to j or k or anything else but i. (And pull the function out if the loop! ;))
Fourth: You are calling the function with $ as variable. That works well as long as you have only one object selected, but if you have multiple objects selected, it doesn’t work (since the variable doesn’t contain the objects, but a selection set). Use Selection[i] instead.
Fifth: You’re storing the selection into an array, but never use that…you use selection[i] instead. (No! Giving a variable in a Function the same name, doesn’t count. That is a different variable!!!)
In essence, that would be the revised script:
fn CalculateVolume theobj =
(
local Volume= 0.0
local Center= [0.0, 0.0, 0.0]
local theMesh = snapshotasmesh theobj
for j = 1 to theMesh.numfaces do
(
local Face= getFace theMesh j
local vert2 = getVert theMesh Face.z
local vert1 = getVert theMesh Face.y
local vert0 = getVert theMesh Face.x
local dV = Dot (Cross (vert1 - vert0) (vert2 - vert0)) vert0
Volume+= dV
)
delete theMesh
Volume /= 6
Center /= 24
Center /= Volume
Volume
)
scalevalue = 100
theobjs = selection as array
for i in theobjs do
(
setUserProp i "Mass"
(
(CalculateVolume i)/scalevalue
)
)
Haven’t tested it, but it should work…
Edit: To be clear: Part Four is why it doesn’t work for multiple selected objects. The others are just commenting ‘Bad Form’ ;).
Thank you for your help Piflik!
I don’t know why your script doesn’t work but I have modified mine following your advices and works fine now!
thank you
scaleValue = 300
fn CalculateVolume theobjs =
(
local Volume= 0.0
local Center= [0.0, 0.0, 0.0]
local theMesh = snapshotasmesh theobjs
local numFaces = theMesh.numfaces
for i = 1 to numFaces do
(
local Face= getFace theMesh i
local vert2 = getVert theMesh Face.z
local vert1 = getVert theMesh Face.y
local vert0 = getVert theMesh Face.x
local dV = Dot (Cross (vert1 – vert0) (vert2 – vert0)) vert0
Volume+= dV
)
delete theMesh
Volume /= 6
Center /= 24
Center /= Volume
Volume
)for i in 1 to selection.count do
(
setUserProp selection[i] “Mass” ((CalculateVolume selection[i] )/scaleValue)
)