Unable import image to textures
-
On 26/03/2014 at 00:16, xxxxxxxx wrote:
import c4d
import osdef main() :
fn = c4d.storage.GeGetC4DPath(c4d.C4D_PATH_DESKTOP) #Gets the desktop path
pathToTexture = os.path.join(fn,'Feet.jpg') #Gets the specific texture image on your desktopmat = doc.GetActiveMaterial() #Assign the active material a variable
shdr_texture = c4d.BaseList2D(c4d.Xbitmap) #Create a bitmap shader in memory
shdr_texture[c4d.BITMAPSHADER_FILENAME] = pathToTexture #Assign the path to the texture image to your shader
mat[c4d.MATERIAL_COLOR_SHADER]= shdr_texture #Assign the shader to the color channel in memory only
mat.InsertShader(shdr_texture) #Insert the shader into the color channel
mat.Update(True, True) #Re-calculate the thumbnails of the materialif __name__=='__main__':
main()I'm trying to import jpg and above code throw error as per below:
Traceback (most recent call last) :
** File "'scriptmanager'", line 17, in <module>**
** File "'scriptmanager'", line 12, in main**
TypeError: 'NoneType' object does not support item assignmentPlease help.
-
On 26/03/2014 at 07:59, xxxxxxxx wrote:
Hi,
It looks like you're using some of my code.
The reason I can tell is because I write some very specific kinds variables and notes in my code.The code you posted works fine for me in Windows R13. But you have to make sure that the material is active first.
Anytime you see: TypeError: 'NoneType' in the Console. It usually means there is no active object, material, tag, etc.-ScottA
-
On 26/03/2014 at 19:35, xxxxxxxx wrote:
I've modified the code to be like this :
import c4d
import osdef main() :
** fn = c4d.storage.GeGetC4DPath(c4d.C4D_PATH_DESKTOP) #Gets the desktop path**
** pathToTexture = os.path.join(fn,'Feet.jpg') #Gets the specific texture image on your desktop**
** #print pathToTexture**
** **
** mat = c4d.BaseMaterial(c4d.Mmaterial)**
** mat.SetName(pathToTexture)**
** mat[c4d.MATERIAL_USE_ALPHA] = True**
** **
** shader = c4d.BaseShader(c4d.Xbitmap)**
** shader[c4d.BITMAPSHADER_FILENAME] = pathToTexture**
** mat[c4d.MATERIAL_COLOR_SHADER] = shader**
** mat.InsertShader(shader)**
** shader = shader.GetClone()**
** mat[c4d.MATERIAL_ALPHA_SHADER] = shader**
** mat.InsertShader(shader)**
** **
** plane = c4d.BaseObject(5100)**
** plane.SetName('Feet.jpg')**
** tex = plane.MakeTag(c4d.Ttexture)**
** tex[c4d.TEXTURETAG_MATERIAL] = mat**
** tex[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW**
** **
** doc.InsertObject(plane, None)**
** doc.AddUndo(c4d.UNDOTYPE_NEW, plane)**
** doc.InsertMaterial(mat)**
** doc.AddUndo(c4d.UNDOTYPE_NEW, mat)**
** **
if __name__=='__main__':
** main()**
I managed to import the jpg file but how I can replace the existing texture. Let say I have a sofa and jpg for fabric. How I can replace the fabric jpg with lets say leather.jpg. Above code managed to leather.jpg in but it not replacing the fabric.jpg -
On 26/03/2014 at 21:03, xxxxxxxx wrote:
The workflow for that is to check if the material already exists.
If it does exist. Then you only change the image paths.
If the material doesn't exist. Then you create it.Example:
I used the first material as the target material here because it's the simplest one to use.
You can also target a material by it's name, or whatever else you want.import c4d import os def main() : fn = c4d.storage.GeGetC4DPath(c4d.C4D_PATH_DESKTOP) #Gets the desktop path #If we already have an existing first material and just want to change the images in it #Then we just update it with the new image paths if doc.GetFirstMaterial() : firstMat = doc.GetFirstMaterial() pathToTexture = os.path.join(fn,'myNewImage.jpg') colShader = firstMat[c4d.MATERIAL_COLOR_SHADER] colShader[c4d.BITMAPSHADER_FILENAME] = pathToTexture AlphaShader = firstMat[c4d.MATERIAL_ALPHA_SHADER] AlphaShader[c4d.BITMAPSHADER_FILENAME] = pathToTexture firstMat.Message(c4d.MSG_UPDATE) firstMat.Update(True, True) #Recalculate the thumbnails of the material #Otherwise... #We create the new material from scratch else: pathToTexture = os.path.join(fn,'myimage.jpg') mat = c4d.BaseMaterial(c4d.Mmaterial) mat.SetName(pathToTexture) mat[c4d.MATERIAL_USE_ALPHA] = True shader = c4d.BaseShader(c4d.Xbitmap) shader[c4d.BITMAPSHADER_FILENAME] = pathToTexture mat[c4d.MATERIAL_COLOR_SHADER] = shader mat.InsertShader(shader) shader = shader.GetClone() mat[c4d.MATERIAL_ALPHA_SHADER] = shader mat.Message( c4d.MSG_UPDATE ) mat.Update( True, True ) #Recalculate the thumbnails of the material mat.InsertShader(shader) plane = c4d.BaseObject(5100) plane.SetName('Feet.jpg') tex = plane.MakeTag(c4d.Ttexture) tex[c4d.TEXTURETAG_MATERIAL] = mat tex[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW doc.InsertObject(plane, None) doc.AddUndo(c4d.UNDOTYPE_NEW, plane) doc.InsertMaterial(mat) doc.AddUndo(c4d.UNDOTYPE_NEW, mat) c4d.EventAdd() if __name__=='__main__': main()
-ScottA
-
On 26/03/2014 at 23:04, xxxxxxxx wrote:
import c4d import os def main() : fn = c4d.storage.GeGetC4DPath(c4d.C4D_PATH_DESKTOP) #Gets the desktop path pathToTexture = os.path.join(fn,'Feet.jpg') #Gets the specific texture image on your desktop #print pathToTexture mat = c4d.BaseMaterial(c4d.Mmaterial) print mat mat.SetName(pathToTexture) mat[c4d.MATERIAL_USE_ALPHA] = True shader = c4d.BaseShader(c4d.Xbitmap) shader[c4d.BITMAPSHADER_FILENAME] = pathToTexture mat[c4d.MATERIAL_COLOR_SHADER] = shader mat.InsertShader(shader) # shader = shader.GetClone() #mat[c4d.MATERIAL_ALPHA_SHADER] = shader #mat.InsertShader(shader) #plane1 = c4d.BaseObject(5616) # pl#ane1.Remove() # firstMat = c4d.BaseMaterial(c4d.Mmaterial) #colShader = firstMat[c4d.MATERIAL_COLOR_SHADER] #colShader[c4d.BITMAPSHADER_FILENAME] = pathToTexture # AlphaShader = mat[c4d.MATERIAL_COLOR_SHADER] shader[c4d.BITMAPSHADER_FILENAME] = pathToTexture mat.Message(c4d.MSG_UPDATE) mat.Update(True,True) plane = c4d.BaseObject(5140) #plane = c4d.BaseMaterial(c4d.Mmaterial) plane.SetName('Sofa') tex = plane.MakeTag(c4d.Ttexture) print tex tex[c4d.TEXTURETAG_MATERIAL] = mat tex[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW doc.InsertObject(plane, None) doc.AddUndo(c4d.UNDOTYPE_NEW, plane) doc.InsertMaterial(mat) doc.AddUndo(c4d.UNDOTYPE_NEW, mat) c4d.EventAdd() if \__name\_\_=='\__main\_\_': main() I've changed the code as above. Import working fine, but it creates another object instead replacing the existing jpg. Image <http://postimg.org/image/fusbopj9z/>
-
On 27/03/2014 at 07:58, xxxxxxxx wrote:
The code I posted works fine for me when running it in an empty scene. Try it out in an empty scene.
And be sure to change the image names (myimage & myNewImage) to the image names you're using.The first time it's executed it creates a new material and puts images in the color and alpha channels.
The next time it's run. It just replaces the two images by changing the image path.
If you are working on a material that's not the first one. Then you'll have to change the if statement code to look for it by it's name, or some other means. Instead of using GetFirstMaterial().You have to use an if/else statement in your code to check if the material already exists or not.
If the material already exists. Then it only runs the code under the if statement. Which will only change the file paths to the images. A does nothing else.
If the material does not exist. Then the code underneath the else statement runs. Which has the code in it to create the new material, add images to the color & alpha channels, And create a polygon object.If you don't use if/else in your code. C4D has no way of making any decisions.
And you're going to get undesired results.-ScottA
-
On 30/03/2014 at 23:24, xxxxxxxx wrote:
import c4d
import osdef main() :
pathToTexture = r'newPath'
firstMat = doc.GetFirstMaterial()
print firstMat
colShader = firstMat[c4d.MATERIAL_COLOR_SHADER]
colShader[c4d.BITMAPSHADER_FILENAME] = pathToTexture
firstMat.Message(c4d.MSG_UPDATE)
firstMat.Update(True, True)
if __name__=='__main__':
main()The above code works fine for replace texture in cinema 4D if I only have one texture, but I have 2 texture in my project. So I try to do as below:
import c4d
import osdef main() :
pathToTexture = r'newpath'
myMaterials = doc.GetAllTextures()
for(i,item)in myMaterials:
print "old item: "+item
newItemArray = item.split('.')
newItem = newItemArray[0]
newItemName = newItem+"1.jpg"
print newItemName
newPath = pathToTexture+""+newItemName
print newPath
colShader = item[c4d.MATERIAL_COLOR_SHADER]
colShader[c4d.BITMAPSHADER_FILENAME] = newPath
item.Message(c4d.MSG_UPDATE)
item.Update(True, True)'
if __name__=='__main__':
main()Above code managed to get all the texture in my project but unfortunately its giving below error.
Traceback (most recent call last) :
File "'scriptmanager'", line 43, in <module>
File "'scriptmanager'", line 26, in main
IndexError: string index out of rangeHow do I solve this? Please help.