Advertisement
Guest User

SearchBitmap Python script by Oli_D

a guest
May 4th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. import c4d
  2. import os.path
  3.  
  4. class MonDlg(c4d.gui.GeDialog):
  5.     """Dialogue pour le choix de l'image, avec liste dérouante de toutes les images
  6.         utilisées dans les différents matériaux du doc"""
  7.     def __init__(self,lst):
  8.         self.lst = lst
  9.         self.choix = lst[0]
  10.  
  11.     def CreateLayout(self):
  12.         self.SetTitle("Selecteur de materiau par image ")
  13.  
  14.         self.GroupBegin(1000,flags=c4d.BFH_SCALEFIT, cols=2, rows=1)
  15.         self.GroupBorderSpace(10, 10, 10, 0)
  16.         self.AddStaticText(1001,name="Image : ", flags=c4d.BFH_MASK, initw=100)
  17.         self.AddComboBox(1002,flags=c4d.BFH_MASK, initw=250)
  18.         self.GroupEnd()
  19.  
  20.         self.GroupBegin(2000,flags=c4d.BFH_CENTER, cols=2, rows=1)
  21.         self.GroupBorderSpace(10, 10, 10, 0)
  22.         self.AddButton(2001, flags=c4d.BFH_CENTER, initw=100, name="OK")
  23.         self.AddButton(2002, flags=c4d.BFH_CENTER, initw=100, name="Annuler")
  24.         self.GroupEnd()
  25.  
  26.         return True
  27.  
  28.     def InitValues(self):
  29.         for i,n in enumerate(self.lst):
  30.             self.AddChild(1002,i,n)
  31.         return True
  32.  
  33.     def Command(self,id,msg):
  34.         if id==1002 :#choix dans la liste
  35.             self.choix =    self.lst[self.GetLong(1002)]
  36.  
  37.         if id==2001:#bouton ok
  38.             selectMatImg(self.choix)
  39.             self.Close()
  40.  
  41.         if id==2002:#bouton annuler
  42.             self.Close()
  43.  
  44.         return True
  45.  
  46. def searchBitmap(shd, liste):
  47.     """fonction récursive qui renvoie une liste avec les noms des fichiers images
  48.         utilisés dans le shader"""
  49.     while shd:
  50.         if shd.CheckType(c4d.Xbitmap):
  51.             fn = shd[c4d.BITMAPSHADER_FILENAME]
  52.             name = os.path.basename(fn)
  53.             liste.append(name)
  54.         searchBitmap(shd.GetDown(), liste)
  55.         shd = shd.GetNext()
  56.  
  57. def getListImg(docu=doc):
  58.     """renvoie la liste de toutes les images contenue dasn les materiaux du doc"""
  59.     res = []
  60.     mat = docu .GetFirstMaterial()
  61.     while mat:
  62.         shd = mat.GetFirstShader()
  63.         searchBitmap(shd, res)
  64.         mat = mat.GetNext()
  65.     res = list(set(res))
  66.     return res
  67.  
  68. def selectMatImg(nom_img, docu=doc):
  69.     """selectionne les materiaux qui utilisent l'image"""
  70.     if not nom_img : return
  71.     mat = docu .GetFirstMaterial()
  72.     while mat:
  73.         lst = []
  74.         shd = mat.GetFirstShader()
  75.         searchBitmap(shd, lst)
  76.         if nom_img in lst :
  77.             mat.SetBit(c4d.BIT_ACTIVE)
  78.         else : mat.DelBit(c4d.BIT_ACTIVE)
  79.         mat = mat.GetNext()
  80.     c4d.EventAdd()
  81.  
  82. def main():
  83.     imgs = getListImg(docu=doc)
  84.     if not len(imgs):
  85.         c4d.gui.MessageDialog("Il n'y a aucune image utilisée")
  86.         return
  87.     dlg = MonDlg(imgs)
  88.     dlg.Open(c4d.DLG_TYPE_MODAL)
  89.     return
  90.  
  91. if __name__=='__main__':
  92.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement