Hi all, I have made a tool to swap bones around in the skin modifier. And it works great. However now I want to give it the possibility to swap more than 1 couple of bones at the time and I am encountering one difficulty.
Here’s the bit :
AllVerts = skinOps.GetNumberVertices MyObject.modifiers[#skin]
For vert = 1 to AllVerts do
(
–Empty Arrays
BoneArray = #()
WeightArray = #()
BoneArray2 = #()
WeightArray2 = #()
– Get number of bones per verts
MyBonesPerVert = skinOps.GetVertexWeightCount MyObject.modifiers[#skin] vert
for Mybone = 1 to MyBonesPerVert do
(
– get bones index and weights
BoneIndex = skinOps.GetVertexWeightBoneID MyObject.modifiers[#skin] vert Mybone
BoneWeight = skinOps.GetVertexWeight MyObject.modifiers[#skin] vert Mybone
–iif this bone index is == to the stored MySelectedFromBoneIndex then swap
if BoneIndex == MySelectedFromBoneIndex do
(
BoneIndex = MySelectedToBoneIndex
)
Append BoneArray BoneIndex
Append WeightArray BoneWeight
)
–1 Swap Bones
skinOps.ReplaceVertexWeights MyObject.modifiers[#skin] vert BoneArray WeightArray
–///////2ND LOOP///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
for Mybone = 1 to MyBonesPerVert do
(
– get bones index and weights
BoneIndex2 = skinOps.GetVertexWeightBoneID MyObject.modifiers[#skin] vert Mybone
BoneWeight2 = skinOps.GetVertexWeight MyObject.modifiers[#skin] vert Mybone
–iif this bone index is == to the stored MySelectedFromBoneIndex then swap
if BoneIndex2 == MySelectedFromBoneIndex2 do
(
BoneIndex2 = MySelectedToBoneIndex2
)
Append BoneArray2 BoneIndex2
Append WeightArray2 BoneWeight2
)
–2 Swap Bones
skinOps.ReplaceVertexWeights MyObject.modifiers[#skin] vert BoneArray2 WeightArray2
)
As you can see I have two loops one after the other. Both are almost identical, if I put one in comment the other one left will works. Both together they just empty all my weights.
The code is perfectly working with a single loop, but with two loops they seems to conflict each other and I simply can’t figure out why.
Is anyone have an idea ?
The code is perfectly working with a single loop, but with two loops they seems to conflict each other and I simply can’t figure out why.
Is anyone have an idea ?
run both loops but print out the array counts in the second loop.
My guess is MyBonesPerVert is altered by this line
--1 Swap Bones
skinOps.ReplaceVertexWeights MyObject.modifiers[#skin] vert BoneArray WeightArray
and will need to be re-read.
when you set a variable via function that reads scene objects, it tends to be ‘live’ and can change when scene objects change.
[QUOTE=Mambo4;23770]run both loops but print out the array counts in the second loop.
My guess is MyBonesPerVert is altered by this line
--1 Swap Bones
skinOps.ReplaceVertexWeights MyObject.modifiers[#skin] vert BoneArray WeightArray
and will need to be re-read.
when you set a variable via function that reads scene objects, it tends to be ‘live’ and can change when scene objects change.[/QUOTE]
Thanks for your answer :
I have tryed to reinitialize MyBonesPerVert just before the second loop and now I get this error :
>> MAXScript Rollout Handler Exception:
– Runtime error: Exceeded the vertex weight list countSkin:Skin <<
here : BoneIndex2 = skinOps.GetVertexWeightBoneID MyObject.modifiers[#skin] vert Mybone
Which makes no sens to me as I am never altering the number of vert anywhere, whats work for the first loop should also for the second. Those number of vert is define right at the beginning before the loops and never get touched after.
In the same idea I dont understand why I would need to reset MyBonesPerVert anywhere since I never change the number of bones per vert. I am just playing with their ID.
Then after the skinOps.ReplaceVertexWeights runs your skinOps.GetVertexWeightCount must be returning zero.
It looks like Your weights are being removed by the results of BoneArray and WeightArray the first loop.
according to the help for skinOps.ReplaceVertexWeights
“Any influence weights for the bone(s) that are not specified are erased.”
so it is likely that somehow you are failing to specify the weights (or the bones) in the expected manner.
you should be outputting some numbers to the listener as you go.
Then you can see where it actually is going wrong.
for Mybone = 1 to MyBonesPerVert do
(
-- get bones index and weights
BoneIndex = skinOps.GetVertexWeightBoneID MyObject.modifiers[#skin] vert Mybone
BoneWeight = skinOps.GetVertexWeight MyObject.modifiers[#skin] vert Mybone
--if this bone index is == to the stored MySelectedFromBoneIndex then swap
if BoneIndex == MySelectedFromBoneIndex do
(
BoneIndex = MySelectedToBoneIndex
)
format "vertex:% BoneIndex:% BoneWeight:%
" vert BoneIndex BoneWeight
Append BoneArray BoneIndex
Append WeightArray BoneWeight
)
Thanks Mambo4, but actually all the code was fine (I have still modify it so I use only one loop, changing the input data each loop, instead of having several loop one after each other, but its the same logic ).
As you can see the two loops are identical, so if I was failing to specify any weights even the first loop wont have worked. That’s why I was so stuck, because it was just making no sens I get the first loop working but not the second knowing both was the same…
All I had to do for making it work was to force the skin modifier to “refresh”. I dont have my code with me now but basically the skin modifier, by default, wasnt updated after my first loop whish was causing my problems. All I had to do was to putt this one line of skinops after my first loop to get it re-actualized and it then worked like a charm… one things quite difficult to guess… -_-