Change Material via Python script

HI everybody,

I would like to create Vray Materials via a Python script and then change the parameters. My problem is, that I am not sure, how I can access the different parameters and then change them.

So far I got this together, but it is not working yet. I can create the material, but I cannot add a VrayBitmap in the Diffuse channel and change the color space to linear in the VrayBitmap.

Has anyone an idea?

Thanks a lot for your help and have a nice day.

import c4d

from c4d import gui

# Welcome to the world of Python

# Script state in the menu or the command palette

# Return True or c4d.CMD_ENABLED to enable, False or 0 to disable

# Alternatively return c4d.CMD_ENABLED|c4d.CMD_VALUE to enable and check/mark

#def state():

# return True

# Main function

def main():

c4d.CallCommand(1054644) # V-Ray Material

c4d.CallCommand(1055619) # add VrayBitmap

VRayMaterial[c4d.BRDFVRAYMTL_DIFFUSE_TEXTURE] # add VrayBitmap

VRayBitmap[c4d.BITMAPBUFFER_TRANSFER_FUNCTION] # change color space in VrayBitmap to linear

# Execute main()

if __name__==‘__main__’:

main()

Hi @Anna1 , the reason you can not change the parameters is that you simply call command for creating an instance of a material. This command only creates the material and inserts it into the document. To properly add a bitmap shader, or change any of its parameters you need to access the material that was inserted. Since this is not easy, a more robust approach is to create the material directly with its own ID and operate on the instance after you have inserted it in the document. Here is an example code:


import c4d
from c4d import gui

# Main function
def main():
  activeDocument = c4d.documents.GetActiveDocument()
  vrayMaterial = c4d.BaseMaterial(1053286) # Create V-Ray Material
  vrayMaterial.SetName('PythonMaterial') # optional
  # Insert the new material in the document
  activeDocument.InsertMaterial(vrayMaterial)
  vrayBitmapShader = c4d.BaseShader(1055619) # Create V-Ray BitmapShader
  # Insert the new shader in the material
  vrayMaterial.InsertShader(vrayBitmapShader)
  # Assign shader to diffuse
  vrayMaterial[c4d.BRDFVRAYMTL_DIFFUSE_TEXTURE] = vrayBitmapShader
  # Set the transfer function to linear
  vrayBitmapShader[c4d.BITMAPBUFFER_TRANSFER_FUNCTION] = c4d.BITMAPBUFFER_TRANSFER_FUNCTION_LINEAR
  # Update the document
  c4d.EventAdd()

# Execute main()
if __name__=='__main__':
  main()

I hope this code helps you achieve what you want.

Hey Deyan,

wow, thanks a lot. That works like a charm. I am not very familiar with coding yet, so I hope you can enlighten me a little bit. Is there a way, so that I can see, how all the things are called? I mean how do you know, that the V-Ray Material is called c4d.BaseMaterial(1053286)? Is there some list or some other way to quickly find out? I know that there is a Python SDK for cinema but I didn’t come across one for V-Ray.

Have a lovely evening and thanks for your quick help
Anna

Hi @Anna1 , there is not (yet) a dedicated V-Ray specific Python SDK for Cinema 4D. Although with the standard Cinema Python SDK you should be able to achieve most of the setup for V-Ray rendering, just without any control over the V-Ray VFB (which is planned for the future).

Is there a way, so that I can see, how all the things are called? I mean how do you know, that the V-Ray Material is called c4d.BaseMaterial(1053286)
All V-Ray specific nodes use Cinema 4D plugin IDs, which in our case are listed here <V-RayInstallLocation>/res/c4d_symbols.h, and usually the V-Ray install location is in the “plugins” subdirectory of the Cinema 4D installation directory.
You can use those ids with the Cinema 4D python SDK to create shaders, tags, materials, objects and anything else V-Ray related. V-Ray render elements are a bit trickier, but we will provide guidance should you need to create those from a script.

And also in case you didnt know (but I assume you do know) you can simple drag and drop the settings-parameters into the console or your script to see how they are defined in C4D. Also the scriptlogger helps a lot and of course the Python SDK as you already mentioned.

Hi you two,

thanks a lot for your help and explanations.

Have a nice evening
Anna