2

I'm creating a tool for a homework assignment to create and apply materials to selected faces of target objects. The idea is that you'll be able to select faces, type in a name, pick a colour, and it will create and apply the material to the selected faces/objects in your scene. I can't quite figure out how to apply the material to the selected faces though...

So far I've created the material, created a shading group for the material, connected the material to the shading group, and changed the colour. I'm pretty new to scripting with python, so I'm having a hard time finding which command of many will actually apply my new material to my selected faces.

#create a shader (material)
shader=cmds.shadingNode('blinn', asShader=True, n="Wood") #placeholder name
#create a shading group
shading_group=cmds.sets(renderable=True, noSurfaceShader=True, empty=True)
#connect the shader to the shading group
cmds.connectAttr('%s.outColor' %shader, '%s.surfaceShader'%shading_group)
#Change the colour
cmds.setAttr(shader+'.color', R,G,B) #replace r,g,b with values between 0 and 1
selected= cmds.ls(sl=True)

All of my current code works so far, but I don't know what command to use to apply the material to my selection.

I've so far looked at cmds.hyperShade, cmds.setAttr, and cmds.sets but I don't know what to make of what my console is spitting out

Lettrebag
  • 21
  • 1
  • 3
  • Could you please include any attempt you have made to solve this problem? – mrtig Sep 05 '19 at 00:42
  • I've tried cmds.hyperShade("Wood", apply=True) cmds.setAttr(selected, e=True, mat="Wood") neither work... I was hoping for some clue as to where to look to solve this as I'm rather new to scripting. Edit:trying the cmds.sets command – Lettrebag Sep 05 '19 at 01:04
  • the command hyperShade is wrongly documented, you should write mds.hyperShade( apply='Wood'). Anyway, hypershade has limitations in my memory and you should use cmds.sets – DrWeeny Sep 05 '19 at 01:59

1 Answers1

1

from my other post : How can I perform shader assignment to a geo efficiently and non-destructively?

You can use cmds.sets to assign shader

selection = cmds.ls(sl=True, o=True)[0]    
faces = cmds.ls(sl=True)
x = 0
# assign shader
sha = cmds.shadingNode('lambert', asShader=True, name="{}_{}_lambert".format(selection, x))
sg = cmds.sets(empty=True, renderable=True, noSurfaceShader=True,  name="{}_{}_sg".format(selection, x))
cmds.connectAttr( sha+".outColor", sg+".surfaceShader", f=True)
cmds.sets(faces, e=True, forceElement=sg)
DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • What's the " x= 0" for in line 3? say I want to be able to apply the material to faces regardless of how many objects said selected faces are on? – Lettrebag Sep 05 '19 at 03:13
  • Thanks for pointing me towards using the cmds.sets command! It worked! – Lettrebag Sep 05 '19 at 09:39
  • Ive just adapted the code from a previous answer where i needed to generate several shaders. Instead of rewriting, i just put x=0 so it wont throw you an error. Feel free to remove it and change names – DrWeeny Sep 05 '19 at 12:10
  • also if it solved your problem don't forget to mark the question as good :) – DrWeeny Sep 05 '19 at 13:23
  • I have :D it's just I'm so new that it doesn't count visibly yet – Lettrebag Sep 05 '19 at 18:35