星期二, 7月 12, 2011

Classof 原來可以這樣用…

今天看到Bobo講了才知道,原來 classof 還有這等妙用。

我之前寫某個工具時剛好也為了 modifier 的狀態不更新而煩腦,當時是用其他的方法解決了,等等再拿這個方式去試試看可以不可以用這個方式解。

 

This is because when you put the code in a function, the code becomes the body and is run as a multiline expression within a single undo record. During this, scene updates, viewport redraws etc. are suspended until the whole body has finished executing. As result, the modifier stack is not updated until the function has finished executing, and the modifier is in the wrong state.

In contrast, when you run the code within the global scope, each line is executed individually and causes the modifier stack to be updated too.

You can force a modifier stack update by adding the line 'classof theShape' right after the addModifier() call. When you ask for the class of an object, MAXScript checks to see if the stack needs updating because it needs to figure out what the class is ON TOP of the stack. (You could have a shape turned into a mesh turned into EPoly turned into EPatch and back to EMesh within the stack, so without updating the whole stack Max cannot know what the result would be). So classof() is a nice workaround in almost all cases where a modifier was added but has not been updated yet.

You could also call 'select theShape', but that would force a scene update, too, which can be slower than just updating the stack.

 

->出處<-


大意是,把code包起來之後,code 就會以一個 undo 的單位下去執行,沒包之前則是一行就會可以做一次undo。
而在一個undo單位下執行中,場景更新,視窗重繪等等的會全部暫停更新。而 modifier stack也是等到執行完再一次更新 ,
這時要是去找modifier stack拿資訊就會拿到未更新狀態的資訊。

而 classof 這個函式會強迫 modifier stack 更新狀態,因為有可以該物體是mesh後轉成editpoy後又轉成 edit patch最後再轉回mesh,為了查得該個物體的 class,所以一定要先更 modifier stack,不更新就查不到。 

還有另一個選擇是用 select ,不過 select 會有場景更新,相對而言的就比較慢。