Python Puzzle

Interesting subsequent comparison: >>> a = 1 >>> b = 10 >>> c = 100 >>> a < b < c True >>> (a < b) < c True The following 2 code snippets are equal, which can explain why the above code works in that way. if a < b < c: doSomething() if (a < b) and (b < c): doSomething() How about booleans? Is True bigger than False? ...

January 20, 2009 · 2 分鐘 · Drake Guan

Delete unknown nodes in Maya

Sometimes you need to delete unknown nodes in a Maya scene. You can do it in the following python snippet. from pymel import * def deleteUnknownNodes(): # 2 things to take care of: # 1) you can't delete nodes from references. # 2) "delete" will delete all children nodes (hierarchy) in default. unknown = ls(type="unknown") unknown = filter(lambda node: not node.isReferenced(), unknown) for node in unknown: if not objExists(node): continue delete(node) Compare it to the MEL way: ...

December 30, 2008 · 2 分鐘 · Drake Guan

Nuke scripting 初體驗:Nuke + Alfred

大約三週之前,開發 Nuke 的 The Foundry ,由 CEO, Bill Collis, 偕同技術人員 Matt Plec 與兩位美麗的華裔女士來台,參與了 SIGGRAPH 2008 Taipei Chapter 的活動,會後立即趕來太極,面對太極的幾位 compositors,給了一個更加深入一點的 talk + demonstration。 ...

December 25, 2008 · 2 分鐘 · Drake Guan

活躍於 3D 軟體之間的程式語言:Python

Guido van Rossum 於 1991 年創造出 Python 這個 scripting language 時,應該是沒有料想到它的風行全球,更不可能會猜到 3D 軟體產業是多麼的愛戴這個語言: Maya 在被 Autodesk 併購了後,於 2007 年年初釋出了 8.5 版,支援 Python 的環境。 相傳,原本和 Maya 拼得你死我活的 Softimage (不過現在和 Maya 成了親家了@@ 還真的是世事難料呀~)也早就有了 Python 的 binding。 台灣新聞台的最愛 Poser 軟體,2000 的 4.0 版支援 Python。 特效軟體界的傳說 Houdini (也是目前我知道最貴的軟體之一,不過據說 Massive 更貴 @@)於 2007 年投入 Python 的懷抱。 流體模擬(Fluid & Dynamic Simulation)界的第一把交椅 RealFlow 也在最近發表的 RF4 (?) 新增了對 Python 的支援。 因為與 Peter Jackson 的魔戒合作下問世的 Massive ,一樣有提供 Python(不過也一樣是有 TCL)。 趁著被 Apple 收購的 Shake 不會有下一版,然後挾天子以令諸侯(啊,不是,是有 Digital Domain 當靠山)的 Nuke ,在今年(2008)推出 5.0 的同時,一口氣把 GUI 換成 Qt 且同時支援 Python(不過還是有支援 TCL,大家都很愛 TCL 也 :o)。 自 2006 丟出第一部 Elephant’s Dream,以 Open Movie 為名的動畫而開始大紫大紅的 Blender ,因為是 open source 起家的,當然(?)和 Python 有掛勾。 繼 Zbrush 之後出現的 Mudbox 與 modo (或應該說,大有幹掉 Zbrush 潛力的軟體生力軍!?)。 open source 界很有名的 cgkit ,一個主要針對 Maya 與 Renderman 撰寫的 python modules。他們這一群人實在很有遠見,根本是在催生 Maya with Python 了。 (Game Engine)3D 軟體界被淘汰掉的 Truespace 一樣有支援(請原諒我對這個 project 興致缺缺)。 (Game Engine)open source 裏頭,很古老(1998~)的 3D game engine: Crystal Space 提供了個 PyCrystal 。 (Game Engine)open source 後起之秀的 3D game engine: OGRE 3D (非常適合入門與學習一個 game engine 的玩意兒)也有個所謂的 Python-Ogre 。 (Game Engine)另一個後起之秀: Irrlicht Engine 有個 Pyrr (是台大資工的學弟做的?!)。 還有很多……很多…… ,而且未來只會更多…… 不管你是要寫 Game 還是做 Animation 的 TD,你都應該去學習這個 3D 產業不約而同,挑上的的程式語言:Python。

December 17, 2008 · 2 分鐘 · Drake Guan

How do I determine which Shading Group is associated with a Material?

Pymel solution: def getSGFromMaterial(mat): mat = PyNode(mat) return mat.shadingGroups() The following is the original article and mel codes by Bryan Ewert. To determine which Material is associated to a Shading Group, get a list of all connection from the Material’s “.outColor” attribute. Iterate this array to find all Shading Groups. The 『nodeType』 command will identify a Shading Group with the string “shadingEngine”. // //////////////////////////////////////////////////////////////////// // getSGFromMaterial // // Description: Returns the Shading Group set whose '.surfaceShader' // attribute is being fed by the specified Material. // // Returns an array for all Shading Groups using the Material. global proc string[] getSGFromMaterial( string $material ) { string $SG[]; // First, assert that $material is a Material. // (Returns a string array, unfortunately, so requires variable.) string $class[] = getClassification( `nodeType $material` ); if ( $class[0] == "shader/surface" ) { // Assert that there is a connection from the Material's .outColor if ( `connectionInfo -is ( $material + ".outColor" )` ) { // There may be more than one connection... string $dests[] = `connectionInfo -dfs ( $material + ".outColor" )`; for ( $dest in $dests ) { // Iterate through connections and identify ShadingGroup sets. if ( "shadingEngine" == `nodeType $dest` ) { $SG[`size $SG`] = rootNode( $dest ); break; } } } } return $SG; } Note: The following 『rootNode()』 procedure is required by the procedures above. // ////////////////////////////////////////////////////////////// // rootNode // // Description: Strips the dot-suffix of the specified string. // e.g. "object.attribute" is returned as "object" proc string rootNode( string $object ) { string $buffer[]; tokenize $object "." $buffer; return $buffer[0]; }

November 11, 2008 · 2 分鐘 · Drake Guan

Cancellation Error

今天聽莊永裕教授的 Digital Image Synthesis 課時,聽到一個 Cancellation Error 的東西,是個與 IEEE floating point 有關的「數值計算問題」。 投影片上如是說: Cancellation error: devastating loss of precision when small numbers are computed from large numbers by addition or subtraction. 寫了個簡單的 c 程式親身試驗一下。 ...

October 2, 2008 · 1 分鐘 · Drake Guan

How can I check if an objects attributes are connected?

Quick Answer: from pymel import * # Is the object.attr a source of a connection? len(object.tx.inputs()) > 0 # Is the object.attr the destination of a connection? len(object.tx.outputs()) > 0 # List an object.attr's source connection, meaning, list the input connection. # This will return a single object.attr. object.tx.inputs() # List an object.attr's destination connection, meaning, list the all output connections: # This could return multiple object.attrs. object.tx.outputs() # In other words, you can just use inputs() and outputs() to get if there is a connection # (source or destination), how many connections there are, and what they are! The following is the quotation of post from mel wiki . ...

July 6, 2008 · 1 分鐘 · Drake Guan

How to retrieve lights from the selected nodes?

The quick answer is as below: from pymel import * filter( lambda n: 'light' in n.classification(), ls(sl=True, dag=True, leaf=True)) In pymel’s google group, there are 2 people giving different opinions for this and the following are from their post: take out the ‘sl=True’ by using pymel’s “selected()” function by Ofer Koren from pymel import * filter( lambda n: 'light' in n.classification(), selected(dag=True, leaf=True)) Doing it through the API requires more code but is considerably faster by Dean Edmonds ...

June 26, 2008 · 3 分鐘 · Drake Guan