Set VERTEXCOLOR in PaintTool
-
I'm working on a python script that will assign vertex color tags with random colors to each mesh in my object to prep my models for working in Substance Painter. I have a sample script that is getting me close to what I need, but I can't for the life of me figure out what I'm doing wrong with the c4d.DescID(c4d.DescLevel) setup. I continue to get the following error, which suggests the DescLevel is reading c4d.ID_CA_PAINT_TOOL_VERTEXCOLOR, c4d.DTYPE_VECTOR as an integer, even though I'm defining it as a vector (unless I'm misinterpreting this):
Traceback (most recent call last): File "scriptmanager", line 43, in <module> File "scriptmanager", line 22, in main TypeError: 'c4d.Vector' object cannot be interpreted as an integer
Current code sample (simplified but w/ test code included to show what I've tried already):
import c4d doc: c4d.documents.BaseDocument # The currently active document. op: c4d.BaseObject | None # The primary selected object in `doc`. Can be `None`. def main() -> None: if op is None: raise RuntimeError("No object selected") c4d.CallCommand(431000045) # Create Vertex Color Tag doc.SetAction(1021286) #Makes the paint tool active tool = doc.GetAction() tool = c4d.plugins.FindPlugin(tool, c4d.PLUGINTYPE_TOOL) if not tool: print("Vertex Paint tool not found!") return False # Set the vertex color parameter myVertexColor = c4d.Vector(0.0,1.0,0.0) # Green # myVertexColor = c4d.DescLevel(c4d.Vector(0.0,1.0,0.0)) # Green desc_level = c4d.DescLevel(c4d.ID_CA_PAINT_TOOL_VERTEXCOLOR, c4d.DTYPE_VECTOR, 0) # Debug: Verify the DescID print(f"DescID: {desc_level}") vertexColorSuccess = tool.SetParameter(desc_level, myVertexColor) if not vertexColorSuccess: print("Failed to set vertex color parameter!") return False # Debug: Verify the parameter was set current_value = tool.GetParameter(desc_level, c4d.DESCFLAGS_GET_0) print(f"Set color: {current_value}") # tool[c4d.ID_CA_PAINT_TOOL_VERTEXCOLOR]=myVertexColor # myVertexColorTag = c4d.DescID(c4d.ID_CA_PAINT_TOOL_VERTEXCOLOR, c4d.DTYPE_VECTOR, 0) # tool.SetParameter(myVertexColorTag, myVertexColor, c4d.DESCFLAGS_SET_0) c4d.CallButton(tool, c4d.ID_CA_PAINT_TOOL_APPLY_ALL) # Applies the color to all vertices c4d.EventAdd() if __name__ == '__main__': main()
Am I thinking about this all wrong...should I be accessing/setting the vertex color a different way?
-
Hey @randymills Python provide helper to directly call SetParameter on the correct parameter just with the integer value.
With that's said internally we construct a DescID which is constructed from one or multiples DescLevel. SetParameter usually only deal DescID, since only DescID are able to identify a given parameter.With that's said here is you code working as expected demonstrating you both way.
import c4d doc: c4d.documents.BaseDocument # The currently active document. op: c4d.BaseObject | None # The primary selected object in `doc`. Can be `None`. def main() -> None: if op is None: raise RuntimeError("No object selected") c4d.CallCommand(431000045) # Create Vertex Color Tag doc.SetAction(1021286) #Makes the paint tool active tool = doc.GetAction() tool = c4d.plugins.FindPlugin(tool, c4d.PLUGINTYPE_TOOL) if not tool: print("Vertex Paint tool not found!") return False # Use SetParameter directly if True: myVertexColor = c4d.Vector(0.0,1.0,0.0) # Green vertexColorSuccess = tool.SetParameter(c4d.ID_CA_PAINT_TOOL_VERTEXCOLOR, myVertexColor, c4d.DESCFLAGS_SET_NONE) if not vertexColorSuccess: print("Failed to set vertex color parameter!") return False # Debug: Verify the parameter was set current_value = tool.GetParameter(c4d.ID_CA_PAINT_TOOL_VERTEXCOLOR, c4d.DESCFLAGS_GET_NONE) print(f"Set color: {current_value}") # Use DescID and DescLevel if True: myVertexColor = c4d.Vector(1.0,0.0,0.0) # Red descLevel = c4d.DescID(c4d.DescLevel(c4d.ID_CA_PAINT_TOOL_VERTEXCOLOR, c4d.DTYPE_VECTOR, 0)) vertexColorSuccess = tool.SetParameter(descLevel, myVertexColor, c4d.DESCFLAGS_SET_NONE) if not vertexColorSuccess: print("Failed to set vertex color parameter!") return False # Debug: Verify the parameter was set current_value = tool.GetParameter(descLevel, c4d.DESCFLAGS_GET_NONE) print(f"Set color: {current_value}") desc_level = c4d.DescID(c4d.DescLevel(c4d.ID_CA_PAINT_TOOL_VERTEXCOLOR, c4d.DTYPE_VECTOR, 0)) # tool[c4d.ID_CA_PAINT_TOOL_VERTEXCOLOR]=myVertexColor # myVertexColorTag = c4d.DescID(c4d.ID_CA_PAINT_TOOL_VERTEXCOLOR, c4d.DTYPE_VECTOR, 0) # tool.SetParameter(myVertexColorTag, myVertexColor, c4d.DESCFLAGS_SET_0) c4d.CallButton(tool, c4d.ID_CA_PAINT_TOOL_APPLY_ALL) # Applies the color to all vertices c4d.EventAdd() if __name__ == '__main__': main()
Cheers,
Maxime. -
@m_adam Thank you so much for the help, that works perfectly and thank you for showing me both methods, that's very helpful and appreciated!