Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import os
- import c4d
- import operator
- class Utility(object):
- @staticmethod
- def __is_texture_relative(texture_path):
- if not len(os.path.split(texture_path)[0]):
- return False
- else:
- if texture_path[:1] == "." or texture_path[:1] == os.path.sep or texture_path[:1] == "/":
- return True
- return False
- def __get_all_texture(self):
- doc = c4d.documents.GetActiveDocument()
- all_texture = []
- mat = doc.GetFirstMaterial()
- while mat:
- sha = mat.GetFirstShader()
- all_texture += self.__recurse_find_bitmap(sha)
- mat = mat.GetNext()
- return all_texture
- def __get_tex_path_from_bmp_shader(self, sha):
- doc = c4d.documents.GetActiveDocument()
- path_tex = sha[c4d.BITMAPSHADER_FILENAME]
- if path_tex:
- if len(path_tex):
- if self.__is_texture_relative(path_tex):
- return os.path.join(doc.GetDocumentPath(), path_tex)
- else:
- if len(os.path.split(path_tex)):
- return path_tex.decode('utf8')
- return None
- def __recurse_find_bitmap(self, sha):
- tex = []
- while sha:
- if sha.CheckType(c4d.Xbitmap):
- buffer_path = self.__get_tex_path_from_bmp_shader(sha)
- if buffer_path:
- buffer_tex = {}
- buffer_tex["path"] = buffer_path
- buffer_tex["material"] = sha.GetMain()
- buffer_tex["shader"] = sha
- buffer_tex["channel"] = self.__get_channel_from_shader(sha)
- tex.append(buffer_tex)
- tex += self.__recurse_find_bitmap(sha.GetDown())
- sha = sha.GetNext()
- return tex
- def __test_valid_picture(self, picture_path):
- if not picture_path:
- return False
- if not os.path.exists(picture_path) and not os.path.isabs(picture_path):
- return False
- bmp = c4d.bitmaps.BaseBitmap()
- result = bmp.InitWith(picture_path.encode('utf8'))
- if result[0] == c4d.IMAGERESULT_OK:
- return True
- else:
- return False
- def __add_texture_to_list(self, texture_list, real_path, texture_name, sha_path, mat, sha, channel):
- buffer_tex = {}
- buffer_tex["absolute_path"] = real_path
- buffer_tex["tex_name"] = texture_name
- buffer_tex["relative_path"] = sha_path
- buffer_tex["material"] = mat
- buffer_tex["shader"] = sha
- buffer_tex["channel"] = channel
- texture_list.append(buffer_tex)
- def test_esc(self):
- state = c4d.BaseContainer()
- if c4d.gui.GetInputState(c4d.BFM_INPUT_KEYBOARD, c4d.KEY_ESC, state):
- if state[c4d.BFM_INPUT_VALUE]:
- return True
- return False
- def get_texture_data(self):
- debug = False
- all_tex = self.__get_all_texture()
- c4d.StatusClear()
- texture_found = list()
- texture_to_relocate = list()
- texture_not_found = list()
- doc = c4d.documents.GetActiveDocument()
- docpath = doc.GetDocumentPath()
- suggestedfolder = str()
- for tex in all_tex:
- if self.test_esc():
- break
- current_id = all_tex.index(tex)
- percent = current_id * 100 / len(all_tex)
- color = c4d.utils.MixVec(c4d.Vector(0.79, 0, 0), c4d.Vector(0.297, 0.72, 0.13), percent/100.00)
- c4d.StatusSetNetBar(percent, color)
- texture = c4d.GenerateTexturePath(docpath, tex["path"], suggestedfolder)
- if texture:
- c4d.StatusSetText(texture)
- texture = texture.decode('utf8')
- else:
- c4d.StatusSetText(tex["path"].encode('utf8'))
- # If file path from GenerateTexturePath is the same than than the one in the shader
- if texture and texture == tex["path"]:
- if debug: print "identical Generate"
- # Is in tex folder
- if os.path.split(texture)[1] in os.listdir(os.path.join(docpath, "tex")):
- if debug: print os.listdir(os.path.join(docpath, "tex"))
- self.__add_texture_to_list(texture_to_relocate,
- os.path.join(docpath, "tex", os.path.split(texture)[1]),
- os.path.split(tex["path"])[1],
- tex["path"], tex["material"],
- tex["shader"],
- tex["channel"])
- elif texture[:6] == "preset":
- if debug: print "Is_preset"
- if self.__test_valid_picture(texture):
- if debug: print "valid picture"
- self.__add_texture_to_list(texture_found, texture, os.path.split(texture)[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- else:
- if debug: print "picture corrupted"
- self.__add_texture_to_list(texture_not_found, None, os.path.split(tex["path"])[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- #if it's not a preset
- else:
- if os.path.exists(texture):
- self.__add_texture_to_list(texture_found, texture, os.path.split(texture)[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- #If file didn't exist
- else:
- self.__add_texture_to_list(texture_not_found, None, os.path.split(tex["path"])[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- # If file path from GenerateTexturePath is not the same than the one in the shader, but GenerateTexturePath return a file
- elif texture and texture != tex["path"]:
- if debug: print "no identical"
- #If picture from shader is valid
- if self.__test_valid_picture(tex["path"]):
- if debug: print "shader_picture_valid"
- self.__add_texture_to_list(texture_found, tex["path"], os.path.split(tex["path"])[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- # If picture from GenerateTexturePath is valid and exist (absolute)
- elif self.__test_valid_picture(texture):
- if debug: print "generate_picture_valid"
- if not os.path.isabs(tex["path"]) and self.__test_valid_picture(tex["path"]):
- if debug: print "No Absolute path"
- self.__add_texture_to_list(texture_found, tex["path"], os.path.split(tex["path"])[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- else:
- if debug: print "Absolute path"
- self.__add_texture_to_list(texture_found, texture, os.path.split(tex["path"])[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- # If picture is relative we test if it's in the search path
- elif c4d.IsInSearchPath(os.path.split(tex["path"])[1], docpath) and self.__test_valid_picture(texture):
- if debug: print "In search path and valid generate"
- self.__add_texture_to_list(texture_to_relocate, texture, os.path.split(tex["path"])[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- #If picture is not valid
- else:
- if debug: print "generate picture not in search path or not valid"
- #Si l'image est dans les search path on relocate
- if c4d.IsInSearchPath(tex["path"], docpath) or os.path.exists(texture):
- if debug: print "generate picture not in search path"
- self.__add_texture_to_list(texture_to_relocate, texture, os.path.split(tex["path"])[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- #If we are here, picture is not found
- else:
- if debug: print "generate picture not valid"
- self.__add_texture_to_list(texture_not_found, None, os.path.split(tex["path"])[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- # If path is different and we didn't found a texture
- else:
- self.__add_texture_to_list(texture_not_found, None, os.path.split(tex["path"])[1],
- tex["path"], tex["material"], tex["shader"], tex["channel"])
- texture_found.sort(key=operator.itemgetter('tex_name'))
- texture_to_relocate.sort(key=operator.itemgetter('tex_name'))
- texture_not_found.sort(key=operator.itemgetter('tex_name'))
- c4d.StatusNetClear()
- return texture_found, texture_to_relocate, texture_not_found
- def __get_first_parent_shader(self, shader, mat):
- while mat:
- buffer_sha = shader.GetUp()
- if buffer_sha:
- shader = buffer_sha
- else:
- break
- return shader
- def __get_linked_shader(self, shader, all_shader_list=list()):
- linkedData = self.__get_linked_data(shader)
- all_shader_list += linkedData
- for sha in linkedData:
- linkedData = self.__get_linked_shader(sha, all_shader_list)
- for linked_sha in linkedData:
- if linked_sha not in all_shader_list:
- all_shader_list.append(linked_sha)
- return all_shader_list
- def __get_linked_data(self, shader):
- buffer = list()
- doc = c4d.documents.GetActiveDocument()
- bc = shader.GetDataInstance()
- for i in xrange(len(bc)):
- index = bc.GetIndexId(i)
- linked = bc.GetLink(index, doc, c4d.Xbase)
- if linked:
- buffer.append(linked)
- return buffer
- def __get_channel_from_shader(self, shader):
- mat = shader.GetMain()
- parent_shader = self.__get_first_parent_shader(shader, mat)
- text = "Unknown"
- data = list()
- if mat.CheckType(1020295):
- text = "VrayMat::"
- data = [
- {"link_id": [
- c4d.VRAYMATERIAL_SSS_OVERALLCOLORSHADER,
- c4d.VRAYMATERIAL_SSS_SSSCOLORSHADER,
- c4d.VRAYMATERIAL_SSS_SCATTERSHADER,
- c4d.VRAYMATERIAL_SSS_SCATTERMULTSHADER],
- "value": "SSS"},
- {"link_id": [
- c4d.VRAYMATERIAL_COLOR2_SHADER,
- c4d.VRAYMATERIAL_COLOR2_TRANSPSHADER,
- c4d.VRAYMATERIAL_COLOR2_ROUGHNESSTEX],
- "value": "Color2"},
- {"link_id": [
- c4d.VRAYMATERIAL_COLOR1_SHADER,
- c4d.VRAYMATERIAL_COLOR1_TRANSPSHADER,
- c4d.VRAYMATERIAL_COLOR1_ROUGHNESSTEX],
- "value": "Color1"},
- {"link_id": [
- c4d.VRAYMATERIAL_SPECULAR1_SHADER,
- c4d.VRAYMATERIAL_SPECULAR1_TRANSPSHADER,
- c4d.VRAYMATERIAL_SPECULAR1_FRESNELREFLSHADER,
- c4d.VRAYMATERIAL_SPECULAR1_FRESNELREFRSHADER,
- c4d.VRAYMATERIAL_SPECULAR1_REFLECTIONGLOSSSHADER,
- c4d.VRAYMATERIAL_SPECULAR1_HIGHLIGHTGLOSSSHADER,
- c4d.VRAYMATERIAL_SPECULAR1_ANISOTROPYSHADER,
- c4d.VRAYMATERIAL_SPECULAR1_ANISOTROPYROTSHADER],
- "value": "Specular1"},
- {"link_id": [
- c4d.VRAYMATERIAL_SPECULAR2_SHADER,
- c4d.VRAYMATERIAL_SPECULAR2_TRANSPSHADER,
- c4d.VRAYMATERIAL_SPECULAR2_FRESNELREFLSHADER,
- c4d.VRAYMATERIAL_SPECULAR2_FRESNELREFRSHADER,
- c4d.VRAYMATERIAL_SPECULAR2_REFLECTIONGLOSSSHADER,
- c4d.VRAYMATERIAL_SPECULAR2_HIGHLIGHTGLOSSSHADER,
- c4d.VRAYMATERIAL_SPECULAR2_ANISOTROPYSHADER,
- c4d.VRAYMATERIAL_SPECULAR2_ANISOTROPYROTSHADER],
- "value": "Specular2"},
- {"link_id": [
- c4d.VRAYMATERIAL_SPECULAR3_SHADER,
- c4d.VRAYMATERIAL_SPECULAR3_TRANSPSHADER,
- c4d.VRAYMATERIAL_SPECULAR3_FRESNELREFLSHADER,
- c4d.VRAYMATERIAL_SPECULAR3_FRESNELREFRSHADER,
- c4d.VRAYMATERIAL_SPECULAR3_REFLECTIONGLOSSSHADER,
- c4d.VRAYMATERIAL_SPECULAR3_HIGHLIGHTGLOSSSHADER,
- c4d.VRAYMATERIAL_SPECULAR3_ANISOTROPYSHADER,
- c4d.VRAYMATERIAL_SPECULAR3_ANISOTROPYROTSHADER],
- "value": "Specular3"},
- {"link_id": [
- c4d.VRAYMATERIAL_SPECULAR4_SHADER,
- c4d.VRAYMATERIAL_SPECULAR4_TRANSPSHADER,
- c4d.VRAYMATERIAL_SPECULAR4_FRESNELREFLSHADER,
- c4d.VRAYMATERIAL_SPECULAR4_FRESNELREFRSHADER,
- c4d.VRAYMATERIAL_SPECULAR4_REFLECTIONGLOSSSHADER,
- c4d.VRAYMATERIAL_SPECULAR4_HIGHLIGHTGLOSSSHADER,
- c4d.VRAYMATERIAL_SPECULAR4_ANISOTROPYSHADER,
- c4d.VRAYMATERIAL_SPECULAR4_ANISOTROPYROTSHADER],
- "value": "Specular4"},
- {"link_id": [
- c4d.VRAYMATERIAL_SPECULAR5_SHADER,
- c4d.VRAYMATERIAL_SPECULAR5_TRANSPSHADER,
- c4d.VRAYMATERIAL_SPECULAR5_FRESNELREFLSHADER,
- c4d.VRAYMATERIAL_SPECULAR5_FRESNELREFRSHADER,
- c4d.VRAYMATERIAL_SPECULAR5_REFLECTIONGLOSSSHADER,
- c4d.VRAYMATERIAL_SPECULAR5_HIGHLIGHTGLOSSSHADER,
- c4d.VRAYMATERIAL_SPECULAR5_ANISOTROPYSHADER,
- c4d.VRAYMATERIAL_SPECULAR5_ANISOTROPYROTSHADER],
- "value": "Specular5"},
- {"link_id": [
- c4d.VRAYMATERIAL_FLAKES_COLORSHADER,
- c4d.VRAYMATERIAL_FLAKES_GLOSSINESSSHADER,
- c4d.VRAYMATERIAL_FLAKES_ORIENTATIONSHADER],
- "value": "Flakes"},
- {"link_id": [
- c4d.VRAYMATERIAL_LUMINANCE_SHADER,
- c4d.VRAYMATERIAL_LUMINANCE_TRANSPSHADER],
- "value": "Luminance"},
- {"link_id": [
- c4d.VRAYMATERIAL_BUMP_SHADER],
- "value": "Bump"},
- {"link_id": [
- c4d.VRAYMATERIAL_MATTE_OPSHADER,
- c4d.VRAYMATERIAL_MATTE_ALPHASHADER,
- c4d.VRAYMATERIAL_WEIGHT_SHADER],
- "value": "Matte"}
- ]
- #Vray 1.9
- try:
- data.append(
- {"link_id": [
- c4d.VRAYMATERIAL_TRANSPARENCY_SHADER,
- c4d.VRAYMATERIAL_TRANSPARENCY_GLOSSINESSSHADER,
- c4d.VRAYMATERIAL_VOLUME_COLORTEX,
- c4d.VRAYMATERIAL_COLOR1_SSSBACKSHADER],
- "value": "Refraction"}
- )
- data.append(
- {"link_id": [
- c4d.VRAYMATERIAL_TRANSP_SHADER],
- "value": "Transparency"}
- )
- except:
- pass
- #Vray 3.4
- try:
- data.append(
- {"link_id": [
- c4d.VRAYMATERIAL_REFRACT_SHADER,
- c4d.VRAYMATERIAL_REFRACT_IORTEX,
- c4d.VRAYMATERIAL_REFRACT_GLOSSINESSSHADER,
- c4d.VRAYMATERIAL_VOLUME_COLORTEX,
- c4d.VRAYMATERIAL_COLOR1_SSSBACKSHADER],
- "value": "Refraction"})
- except:
- pass
- elif mat.CheckType(1022593):
- text = "VrayDisplace::"
- data = [
- {"link_id": [
- c4d.VRAYDISPLACEMATERIAL_TEXTURE],
- "value": "Displace"}
- ]
- elif mat.CheckType(c4d.Mmaterial):
- text = "c4dMat::"
- data = [
- {"link_id": [
- c4d.MATERIAL_COLOR_SHADER],
- "value": "Color"},
- {"link_id": [
- c4d.MATERIAL_DIFFUSION_SHADER],
- "value": "Diffusion"},
- {"link_id": [
- c4d.MATERIAL_LUMINANCE_SHADER],
- "value": "Luminance"},
- {"link_id": [
- c4d.MATERIAL_TRANSPARENCY_SHADER],
- "value": "Transparency"},
- {"link_id": [
- c4d.REFLECTION_LAYER_COLOR_TEXTURE,
- c4d.REFLECTION_LAYER_TRANS_TEXTURE],
- "value": "Reflexion"},
- {"link_id": [
- c4d.MATERIAL_ENVIRONMENT_SHADER],
- "value": "Environment"},
- {"link_id": [
- c4d.MATERIAL_BUMP_SHADER],
- "value": "Bump"},
- {"link_id": [
- c4d.MATERIAL_NORMAL_SHADER],
- "value": "Normal"},
- {"link_id": [
- c4d.MATERIAL_ALPHA_SHADER],
- "value": "Alpha"},
- {"link_id": [
- c4d.MATERIAL_DISPLACEMENT_SHADER],
- "value": "Displace"}
- ]
- leave = False
- for x in data:
- for link in x["link_id"]:
- if mat[link]:
- list_linked = self.__get_linked_shader(mat[link])
- if not parent_shader.IsAlive():
- continue
- if parent_shader in list_linked or parent_shader == mat[link]:
- text += str(x["value"])
- leave = True
- break
- if leave:
- break
- return text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement