(newbee) Python versus COFFEE

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 16/12/2011 at 02:00, xxxxxxxx wrote:

Hi there,
I am new to scripting in C4D but I am fluent in Python. So I thought Python is probably the way to go.
However, it appears to me that the SDK API is underexposed in Python, compared to COFFEE, at least looking at the documentation API : Is there anybody to confirm or to invalidate that ?

Exemple.
I have to create a bunch of plane objects, and of each of them, a new material (with color and alpha channel).

At first this simple script fails (error is raised) :

import c4d
from c4d import *
  
def main() :
    mat =  BaseMaterial(Mmaterial)
    mat.setChannelState(CHANNEL_ALPHA, True)
  
if __name__=='__main__':
    main()

AttributeError: 'c4d.BaseMaterial' object has no attribute 'setChannelState'. Which is quite expected since the  setChannelState method is declared only for subclass Material derived from BaseMaterial, but guess what, when I use Material it says that the Material symbol is not defined (despite the "from c4d import *").

What's wrong ? I simply need to crearte a new material and apply it to a freshly created plane object.

Secondly, is there any Python code snippets or samples web site where one can learn by examples ?
I find the COFFEE API better documented that the Python one.

Thank you for your help,
Elie

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 16/12/2011 at 03:28, xxxxxxxx wrote:

Originally posted by xxxxxxxx

I am new to scripting in C4D but I am fluent in Python. So I thought Python is probably the way to go.
However, it appears to me that the SDK API is underexposed in Python, compared to COFFEE, at least looking at the documentation API : Is there anybody to confirm or to invalidate that ?
Exemple.
I have to create a bunch of plane objects, and of each of them, a new material (with color and alpha channel).
At first this simple script fails (error is raised) :

 
import c4d  
from c4d import *  
def main() :  
  mat =  BaseMaterial(Mmaterial)  
  mat.setChannelState(CHANNEL_ALPHA, True)  
    
if __name__=='__main__':  
  main()  

AttributeError: 'c4d.BaseMaterial' object has no attribute 'setChannelState'. Which is quite expected since the  setChannelState method is declared only for subclass Material derived from BaseMaterial, but guess what, when I use Material it says that the Material symbol is not defined (despite the "from c4d import *").
What's wrong ? I simply need to crearte a new material and apply it to a freshly created plane object.

Material class is deprecated as stated in this post. I have to put this note in the docs.
So the code could be:

  
import c4d  
from c4d import *  
def main() :  
  mat =  BaseMaterial(Mmaterial)  
  print mat[MATERIAL_USE_ALPHA]  
  mat[MATERIAL_USE_ALPHA] = True  
  print mat[MATERIAL_USE_ALPHA]  
  
if __name__=='__main__':  
  main()  

Note: Python is case-sensitive and you were calling 'setChannelState' (declared 'SetChannelState'), so anyway it won't even find the method.

Originally posted by xxxxxxxx

Secondly, is there any Python code snippets or samples web site where one can learn by examples ?
I find the COFFEE API better documented that the Python one.

Have you looked at the examples included in the docs ?
Searching this forum is a good way to find answers.

Python documentation is pretty young compared to the COFFEE one. I plan to greatly improve it in the future.

Regards,

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 16/12/2011 at 09:58, xxxxxxxx wrote:

Hello Yannick,
Thank you for your prompt answer that helped a lot. The python dictionnary approach to feed the parameters is really convenient.

Yes the doc is a bit weak at least for beginners. But fortunately making massive use of google and using this forum helps a lot.
I must say that this folder is a knowledge mine that would gained to be documented with example of use in context (constant names) : 
/Applications/MAXON/CINEMA 4D R13/resource/res/description

For others that may have the same need, here is the testing code I used to create a material, a plane and assign the material as a texture to the  plain.

Cheers,
Elie

import c4d
import math
from c4d import *
  
def creatMat(matname) :
    #create a standard material
    mat = c4d.BaseMaterial(c4d.Mmaterial) 
    #create a bitmap shader for color
    xch = c4d.BaseList2D(c4d.Xbitmap)
    #set the path of the image file to the bitmap shader
    xch[c4d.BITMAPSHADER_FILENAME] = "/Users/semiosys/Desktop/fleurbleue.tif"
    #set the shader to the color channel
    mat[c4d.MATERIAL_COLOR_SHADER] = xch
    mat.InsertShader(xch) 
    
    #create a bitmap shader for color
    axch = c4d.BaseList2D(c4d.Xbitmap)
    #set the path of the image file to the bitmap shader
    axch[c4d.BITMAPSHADER_FILENAME] = "/Users/semiosys/Desktop/fleurbleue_mask2.tif"
    #set the shader to the color channel
    mat[c4d.MATERIAL_ALPHA_SHADER] = axch
    mat.InsertShader(axch) 
    
    mat[c4d.MATERIAL_USE_ALPHA] = 1
    mat[c4d.MATERIAL_USE_SPECULAR] = 0
    
    mat[c4d.ID_BASELIST_NAME] = matname
    # UVW projection
    
    mat.Message(c4d.MSG_UPDATE)
    mat.Update(True, True)
    
    doc.InsertMaterial(mat)
    return mat
  
  
def main() :
    
    
    
   neutre = BaseObject(Onull)
   
   mat = creatMat("PETAL");
   
   plane = BaseObject(Oplane)   
   plane.InsertUnder(neutre) 
   plane[c4d.PRIM_PLANE_WIDTH] = 1000;
   plane[c4d.PRIM_PLANE_HEIGHT] =1000;
   plane[c4d.PRIM_PLANE_SUBW] = 10;
   plane[c4d.PRIM_PLANE_SUBH] = 10;
   plane[c4d.PRIM_AXIS] = 0;
   plane[c4d.ID_BASELIST_NAME] = "planperso2";
  
   plane[c4d.ID_BASEOBJECT_REL_POSITION] = Vector(10,10,10)
   plane[c4d.ID_BASEOBJECT_REL_SCALE] = Vector(1,1,1)
  
   doc.InsertObject(plane)
    
   # Flip horizontal
   plane[c4d.ID_BASEOBJECT_REL_ROTATION]  = Vector(math.pi,0,0)
 
   tag = c4d.TextureTag()
   tag[c4d.TEXTURETAG_MATERIAL] = mat
   tag[c4d.TEXTURETAG_PROJECTION]=6
   #print tag[c4d.TEXTURETAG_MATERIAL].GetName()
   tag[c4d.ID_BASELIST_NAME] = tag[c4d.TEXTURETAG_MATERIAL].GetName()
   plane.InsertTag(tag)
  
   c4d.EventAdd()
   
if __name__=='__main__':
    main()