Forum Groups
  All forums
    Help & Feedback
      Work in progress
      Finished Art
      Non-Max related

Featured Threads
  inspiration alert!!!
(37 replies)
  Indespensible MaxScripts, Plugins and 3rd Party Tools
(37 replies)
  The allmighty FREE Resources Thread !
(17 replies)
  spam alert!!!
(4886 replies)
  Maxforums member photo gallery index
(114 replies)
  Maxforums Member Tutorials
(89 replies)
  three cheers to maxforums...
(240 replies)
  101 Things you didnt know in Max...
(198 replies)
  A Face tutorial from MDB101 :D
(95 replies) Members Gallery
(516 replies)
(637 replies)
  Dub's Maxscript Tutorial Index
(119 replies)

Maxunderground news unavailable

Maxscript says what
show user profile  Mr_Stabby
Working on a little utility for skinning, I encountered this weirdness

relevant code:

WeightMap = map<2> --map is an array passed to the function, the value of map<2> originates from GetVertexWeightMap function

if vertID == 132 do (
print "dbg"

print "old weights"
print WeightMap
print "new weights"
print NewWeightMap

format "\n\n"

NewWeightMap = GetVertexWeightMap mSkin vertID

if vertID == 132 do (
print "dbg"

print "old weights"
print WeightMap
print "new weights"
print NewWeightMap

format "\n\n"

the output is as follows:

"old weights"
"new weights"

so far so good

"old weights"
0.72 <-what the fuck is this??
"new weights"

the GetVertexWeightMap function looks like this

fn GetVertexWeightMap mSkin vertID = (
WeightMap = #()
for i = 1 to (skinOps.getVertexWeightCount mSkin vertID) do (
append WeightMap (skinOps.GetVertexWeight mSkin vertID i)

Ok, so, the weird part is that somehow, filling the "NewWeightMap" modifies the old "WeightMap" variable. The only thing I can think of is that the "WeightMap" variable in GetVertexWeightMap must be static and is returned by reference? or what?

also, didn't there used to be a little code box spawned by [code][/code] in this forum? I can't remember.

EDIT: yeah.. nvm. adding "local" to the WeightMap in the function fixes it. Are variabled declared as global by default?

read 390 times
3/26/2015 3:42:22 AM (last edit: 3/26/2015 3:54:27 AM)
show user profile  ccampbell

A global must be explicitly declared to exist and be accessible from any scope( and other scripts). Otherwise it is accessible from the variables current and lower scopes only.

I looks like this happend because the first time you create the WeightMap variable its at the highest level scope of the code you provided.

Its not a global but its accessible at the highest level of your code which means any variable with the same name in a lower scope will look outward for the initial value i.e. you had been creating the value before the function was run so your function was using the existing variable.

You have to explicitly declare local if you want to create a second instance of an existing variable within a lower scope without overwriting the original. So when calling it a second time from inside another scope its reaching out to grab the the value front he initial variable. By adding local you've limited the new variable to the new scope and your function holds the initially created value passed to it front he the first WeightMap variable.

Hope this helps to understand.

$Entrepreneur = if((Designer + Engineer)*Programmer){Problem Solver};

read 357 times
3/27/2015 12:55:35 AM (last edit: 3/27/2015 12:55:35 AM)
show user profile  br0t
Regarding the global thing: Maxscript is a bit weird with that. I can only highly recommend to use the global and local keywords, I had to learn that the hard way.

never get low & slow & out of ideas

read 330 times
3/27/2015 8:52:37 PM (last edit: 3/27/2015 8:52:48 PM)
show user profile  Garp
One thing to note in regard to scopes in mxs is that scripts should always be enclosed in their own block. Otherwise their local scope IS the global scope and any variable declared at their upper level (not within a block) ends up being global, even if explicitely declared local.

As brOt said, better be specific when declaring variables to avoid surprises.

read 323 times
3/27/2015 9:22:13 PM (last edit: 3/27/2015 9:23:33 PM)
#Maxforums IRC
Open chat window