Changing polygons' colors in Maya via Python

喜夏-厌秋 提交于 2019-12-11 18:04:04

问题


The only way I know is to use a slider:

import maya.cmds as cmds

cmds.colorSliderGrp( 'polygonColour', label = "Colour", hsv = ( 1, 1, 1 ) )

Then taking the RGB value from that:

rgb = cmds.colorSliderGrp( 'polygonColour', query = True, rgbValue = True )

And then assigning a material to the polygon and giving that material the color:

myShader = cmds.shadingNode( 'lambert', asShader = True, name = "polygonMaterial" )
cmds.setAttr( 'polygon1' + ":blockMaterial.color", rgb[ 0 ], rgb[ 1 ], rgb[ 2 ], type = 'double3' )

Is there an easier approach without using a slider and/or without assigning a material?


回答1:


If you just want to assign a new color to the existing shader(s), it's a simple setAttr(). Every shader has it's own attribute set, so the exact values you need to set depend on the type of shader you want, but for common cases (lambert, phong, and blinn) it's just the .color attribute. Setting the color is as simple as passing in the RGB values you want:

 cmds.setAttr(shader + '.color', 0, 1, 0)  # green

Getting the shader on a particular face is not easy. The easiest thing to do is to get your face and check all the shadingEngine nodes :

 for shadingEngine in cmds.ls(type="shadingEngine"):
     if cmds.sets('pCube1.f[0]', im=shadingEngine):
        shaderball = cmds.listConnections(sshadingEngine, type = 'lambert')[0]
        print "face is assigned to %s" % shaderball

You can assign a shader to a face by i cmds.sets('pCube1.f[99]', fe='initialShadingGroup')

where the argument to fe is the shadingEngine node. The shader ball - the thing you edit by hand - is connected to the shadingEngine's .surfaceMaterial attribute, you can get it the way I did above.

If you want to change the color of a face without shader assignment you want to make vertex colors instead. The menu is Color > Apply Color, the command is polyColorPerVertex (faces, rgb=(r,g,b). So setting the color of one face on a cube looks like

 cmds.polyColorPerVertex('pCube1', rgb=(1,0,0)) # red

The display of the vertex colors in the viewport is controlled by the .displayColors attribute or the Colors > Toggle Display Colors Attribute menu item.



来源:https://stackoverflow.com/questions/29088105/changing-polygons-colors-in-maya-via-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!