Yimin Chen

2021年6月4日1 分鐘

[教學]利用GHPython達成批次彩現運算

更新日期:2021年6月5日

在教學開始之前,我們可以先來看看成果影片:

透過此方法我們便可以讓電腦自動按順序去運算所有的圖,此時我們就可以同時去處理其他事情,達到多工的目的。

整個步驟如下:

  1. 前置設定,包括:

    1. 成果圖片尺寸

    2. 圖片品質

    3. 燈光

    4. 材質

    5. 後處理效果(如景深、對比、高光...等等)

  2. 改變Slider數值

  3. 更新視圖

  4. 暫停

  5. 開始執行彩現運算

  6. 儲存運算完的圖片

  7. 關閉彩現視窗

  8. 重複步驟2~7直到所有圖片運算完成

上述步驟的5、6、7皆可以透過Rhino的指令達成

分別是

_render //步驟5
 
_-SaveRenderWindowAs full_file_path //步驟6
 
_CloseRenderWindow //步驟7

full_file_path為完整的圖片儲存路徑,必須包含副檔名

因此我們可以透過rs.Command()與for迴圈來重複這幾個步驟

此處我們先假設總共有10張圖要輸出

其輸出的檔名固定為3位數字+副檔名,例「023.png」

完整程式碼如下:

import rhinoscriptsyntax as rs
 
import scriptcontext as sc
 
import time
 
import Rhino
 

 
grass = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
 

 
folderPath = r"C:\Users\username\Desktop\TargetFolderName\" #the folder where the pic will be saved in
 

 
sliderGUID = "dd455a20-c67f-4ab8-9096-27f4f3906855" #you can get this by copy the target slider and paste to a plain notepad file
 

 
currentGHFileName = "test.gh"
 

 
for i in range(10):
 
grass.SetSliderValue(sliderGUID, i) #change the slider value to make transformation to the target objects
 

 
grass.RunSolver(currentGHFileName) #commit the change
 

 
time.sleep(1) #pause 1 sec to ensure the Rhino has enough time to change the scene
 

 
rs.Command("_render") #call Rhino to execute render command
 
rs.Command("_-SaveRenderWindowAs " + "{0}{1}.png".format(folderPath, format(i, "03"))) #save the final render image to the target folder
 

 
rs.Command("_CloseRenderWindow") #close the render window

如果有任何問題,歡迎留言~

    1100
    0