SearchBitmap Python script by Oli_D

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 05 May 2013   #1
SearchBitmap Python script by Oli_D

Hi, a little heads up for a nice script by Oli_D, a well known member in Frenchcinema4D forum...

This python script is really usefull!
You know the name of an image but how to find it among all the materials that you created .. With this script, you give the name of the image, and is ready for selecting all materials / shaders that use this file. Simple as that!

Thanks Oli_D !!
import c4d
  import os.path
  	
  def searchBitmap(shd, liste):
  	
  	while shd:
  		if shd.CheckType(c4d.Xbitmap):
  			fn = shd[c4d.BITMAPSHADER_FILENAME]
  			name = os.path.basename(fn)
  			liste.append(name)
  		searchBitmap(shd.GetDown(), liste)
  		shd = shd.GetNext()
  
  def main():
  	nom_img = c4d.gui.InputDialog("Put the name of the pîcture :")
  	mat = doc .GetFirstMaterial()
  	while mat:
  		lst = []
  		shd = mat.GetFirstShader()
  		searchBitmap(shd, lst)
  		if nom_img in lst :
  			mat.SetBit(c4d.BIT_ACTIVE)
  		else : mat.DelBit(c4d.BIT_ACTIVE)
  		mat = mat.GetNext()
  	c4d.EventAdd()
  
  if __name__=='__main__':
  	main()  
__________________
Frenchcinema4D Moderator http://www.frenchcinema4d.fr
my website : http://www.lev-communication.fr
Kamigaz® Project on : Pinterest
 
Old 05 May 2013   #2
wow thats great for broken links.

thanks a lot.
Jops
 
Old 05 May 2013   #3
exactly ! I hate to check each material/shader to find exactly where i put these f#&cK@ng picture :-)
Thanks for the reply..
__________________
Frenchcinema4D Moderator http://www.frenchcinema4d.fr
my website : http://www.lev-communication.fr
Kamigaz® Project on : Pinterest
 
Old 05 May 2013   #4
for broken links it's better to use this method
 
Old 05 May 2013   #5
A nice update from Oli_D
Now the plugin return all the pictures used in the materials inside a menu list.


import c4d
  import os.path
  
  class MonDlg(c4d.gui.GeDialog):
  	"""Dialogue pour le choix de l'image, avec liste dérouante de toutes les images
  	   utilisées dans les différents matériaux du doc"""
  	def __init__(self,lst):
  		self.lst = lst
  		self.choix = lst[0]
  
  	def CreateLayout(self):
  		self.SetTitle("Selecteur de materiau par image ")
  		
  		self.GroupBegin(1000,flags=c4d.BFH_SCALEFIT, cols=2, rows=1)
  		self.GroupBorderSpace(10, 10, 10, 0)
  		self.AddStaticText(1001,name="Image : ", flags=c4d.BFH_MASK, initw=100)								  
  		self.AddComboBox(1002,flags=c4d.BFH_MASK, initw=250)
  		self.GroupEnd()
  				 
  		self.GroupBegin(2000,flags=c4d.BFH_CENTER, cols=2, rows=1)
  		self.GroupBorderSpace(10, 10, 10, 0)
  		self.AddButton(2001, flags=c4d.BFH_CENTER, initw=100, name="OK")
  		self.AddButton(2002, flags=c4d.BFH_CENTER, initw=100, name="Annuler")
  		self.GroupEnd()
  		
  		return True
  		
  	def InitValues(self):
  		for i,n in enumerate(self.lst):
  			self.AddChild(1002,i,n)
  		return True
  		
  	def Command(self,id,msg): 
  		if id==1002 :#choix dans la liste
  			self.choix =  self.lst[self.GetLong(1002)]
  		
  		if id==2001:#bouton ok
  			selectMatImg(self.choix)
  			self.Close()
  		
  		if id==2002:#bouton annuler
  			self.Close()	
  	
  		return True
  	
  def searchBitmap(shd, liste):
  	"""fonction récursive qui renvoie une liste avec les noms des fichiers images
  	   utilisés dans le shader"""
  	while shd:
  		if shd.CheckType(c4d.Xbitmap):
  			fn = shd[c4d.BITMAPSHADER_FILENAME]
  			name = os.path.basename(fn)
  			liste.append(name)
  		searchBitmap(shd.GetDown(), liste)
  		shd = shd.GetNext()
  		
  def getListImg(docu=doc):
  	"""renvoie la liste de toutes les images contenue dasn les materiaux du doc"""
  	res = []
  	mat = docu .GetFirstMaterial()
  	while mat:
  		shd = mat.GetFirstShader()
  		searchBitmap(shd, res)
  		mat = mat.GetNext()
  	res = list(set(res))
  	return res
  
  def selectMatImg(nom_img, docu=doc):
  	"""selectionne les materiaux qui utilisent l'image"""
  	if not nom_img : return
  	mat = docu .GetFirstMaterial()
  	while mat:
  		lst = []
  		shd = mat.GetFirstShader()
  		searchBitmap(shd, lst)
  		if nom_img in lst :
  			mat.SetBit(c4d.BIT_ACTIVE)
  		else : mat.DelBit(c4d.BIT_ACTIVE)
  		mat = mat.GetNext()
  	c4d.EventAdd()
  	
  def main():
  	imgs =  getListImg(docu=doc)
  	if not len(imgs):
  		c4d.gui.MessageDialog("Il n'y a aucune image utilisée")
  		return
  	dlg = MonDlg(imgs)
  	dlg.Open(c4d.DLG_TYPE_MODAL)
  	return
  
  if __name__=='__main__':
  	main()  
__________________
Frenchcinema4D Moderator http://www.frenchcinema4d.fr
my website : http://www.lev-communication.fr
Kamigaz® Project on : Pinterest
 
Old 05 May 2013   #6
I am getting this error:

File "scriptmanager",line 2
import os.path
^
IndentationError:unexpected indent

Any ideas?
 
Old 05 May 2013   #7
The board often messes up the indentation, here's a clean version: http://pastebin.com/n6W6ne9S

Btw, there is missing a line: c4d.EventAdd() at the end of the script. I've added it here: http://pastebin.com/jHqPCFS7
__________________
Cinema 4D Plugin Developer
niklasrosenstein.com/ | Freebies | Plugins

Last edited by NiklasR : 05 May 2013 at 08:19 AM.
 
Old 05 May 2013   #8
Thanks for the quick response.

All fixed and working well.

This is a great tool, and just have to wonder why such things are not built in to the software.

Thanks again.
 
Old 05 May 2013   #9
Thanks for sharing this, it works great.
 
Old 05 May 2013   #10
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright ©2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 12:41 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.