[Closed] Recurring error in one of my loops…Need some help Diagnosing Problem:
--Collects all Editable Poly Objects into an array
PolyObjects = for i in geometry where classof i.baseobject == Editable_Poly and not i.ishidden and i.renderable collect i
--Gets the Material IDs of each obj in "PolyObjects"
InitialpolygonMatIds = for i in PolyObjects collect
(
num_faces = polyop.getNumFaces i.baseobject
for f in 1 to num_faces collect polyOp.getFaceMatID i.baseobject f
)
--TEST: Assigns a random Material ID to each face of each obj in "PolyObjects"
for i in PolyObjects do
(
local num_faces = polyop.getNumFaces i.baseobject
for f in 1 to num_faces do
(
polyop.setfacematid i f (random 1 100)
)
)
--Supposed to Reassign the Material IDs originally stored in "InitialpolygonMatIds" but only works when
--objects have equal face count.
for i in PolyObjects do
(
for j in 1 to PolyObjects.count do
(
num_faces = polyop.getNumFaces i.baseobject
for f in 1 to num_faces do polyop.setfacematid i f InitialpolygonMatIds[j][f]
)
)
This works with only one object, or with multiple objects with an equal number of faces.
If you create two objects with disimilar face counts, and run this script you will get an error.
I have been over these lines of code more times than I can count, and it seems that it should work. What appears to be happening, is that the code is attempting to apply MatIDs to an object when it’s face count is out of range for that object. At first, I thought it was a problem with the “num_faces” variable, but the error seems to suggests differently. For instance:
-- Error occurred in f loop
-- Frame:
-- f: 7
-- called in j loop
-- Frame:
-- num_faces: 512
-- j: 2
-- called in i loop
-- Frame:
-- i: $Editable_Poly:Sphere01 @ [-22.015244,54.452343,0.000000]
-- Unable to convert: undefined to type: Integer
Thanks for taking the time to read all that.
This is a part of my script that is restorative. I do a bunch of operations to the objects’s matIDs because of a bug in max, and I later want to restore the user’s material IDs. My first rule in creating this monster was to “Do no harm”. I could omit this step, but I really think it can be done. Perhaps my loops are too complicated…I honestly am out of ideas.
Any thoughts?
-Dave
--Supposed to Reassign the Material IDs originally stored in "InitialpolygonMatIds" but only works when
--objects have equal face count.
for i in PolyObjects do
(
for j in 1 to PolyObjects.count do
(
num_faces = polyop.getNumFaces i.baseobject
for f in 1 to num_faces do polyop.setfacematid i f InitialpolygonMatIds[j][f]
)
)
Because of the nested loop, each editable poly in PolyObjects gets assigned the stored MatIds of EACH editable poly in PolyObjects… So in the end this means that every editable poly has the matIds of the last editable poly in the PolyObjects array. (unless the script errors out because of different facecounts ofcourse)
To solve this problem, you only need to remove one of the two loops, like this:
for i in 1 to PolyObjects.count do
(
num_faces = polyop.getNumFaces PolyObjects[i].baseobject
for f in 1 to num_faces do polyop.setfacematid PolyObjects[i] f InitialpolygonMatIds[i][f]
)
I’m not sure if my explanation was very clear, but I hope you see the problem
- Martijn
Thats it, I’m flying to Rotterdam and buying you a beer.
Dude, I can’t believe I did’nt see that. It was driving me mad for the last few nights. I hereby declare you to be awsome.
Tested, implemented, and working fine now. Ahhhhhhhh…Soothing.
-Dave