ltethe
12-14-2011, 12:12 AM
Ok, I've read the wiki, I've read a half dozen examples involving bicycles, pies, and abstractions, I still don't quite get the advantage of OPP over Proc coding. I'm a newb to be sure, but I've started throwing everything into Class definitions (with little understanding if I'm even doing them right) cause OOP is the way to go. I have no doubt about that, but what makes it better is eluding me.
Below are two simple code snippets of a shader declaration, one in Proc format, one in Class/Obj format. If someone could tell me what advantages I'm getting with the Class/Obj stuff using the example provided, I'd appreciate it, because at my level, it just looks like I added a bunch of extra lines of code with no tangible advantage.
Procedural Definition
#define rig material
def createMaterial(name, color, type):
cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name=name+"SG")
cmds.shadingNode(type, asShader=True, name=name)
cmds.setAttr( name+".color", color[0], color[1], color[2], type='double3')
cmds.connectAttr(name+".outColor", name+"SG.surfaceShader", f=True)
createMaterial ('red_rig_mtl',(1,0,0),'lambert')
Class Definition
class imShader:
def __init__(self, name, color, type):
self.name = name
self.color = color
self.type = type
def create(self):
#check for existing shader
#if The Shader exists: Don't create the shader
cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name=self.name+"SG")
cmds.shadingNode(self.type, asShader=True, name=self.name)
cmds.setAttr( self.name+".color", self.color[0], self.color[1], self.color[2], type='double3')
cmds.connectAttr(self.name+".outColor", self.name+"SG.surfaceShader", f=True)
###############################
#create Shader
rigShader = imShader('red_rig_mtl', (1,0,0), 'lambert')
rigShader.create()
Bonus! If you notice in the class definition, I've got a couple lines of pseudo code for an if statement to check if the shader exists in the scene and not create the shader if it exist already. However, I can't figure out how to query that, I pulled the query logic from windows, but obviously shaderNodes aren't query able, so I'm still hunting down how to implement this.
Below are two simple code snippets of a shader declaration, one in Proc format, one in Class/Obj format. If someone could tell me what advantages I'm getting with the Class/Obj stuff using the example provided, I'd appreciate it, because at my level, it just looks like I added a bunch of extra lines of code with no tangible advantage.
Procedural Definition
#define rig material
def createMaterial(name, color, type):
cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name=name+"SG")
cmds.shadingNode(type, asShader=True, name=name)
cmds.setAttr( name+".color", color[0], color[1], color[2], type='double3')
cmds.connectAttr(name+".outColor", name+"SG.surfaceShader", f=True)
createMaterial ('red_rig_mtl',(1,0,0),'lambert')
Class Definition
class imShader:
def __init__(self, name, color, type):
self.name = name
self.color = color
self.type = type
def create(self):
#check for existing shader
#if The Shader exists: Don't create the shader
cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name=self.name+"SG")
cmds.shadingNode(self.type, asShader=True, name=self.name)
cmds.setAttr( self.name+".color", self.color[0], self.color[1], self.color[2], type='double3')
cmds.connectAttr(self.name+".outColor", self.name+"SG.surfaceShader", f=True)
###############################
#create Shader
rigShader = imShader('red_rig_mtl', (1,0,0), 'lambert')
rigShader.create()
Bonus! If you notice in the class definition, I've got a couple lines of pseudo code for an if statement to check if the shader exists in the scene and not create the shader if it exist already. However, I can't figure out how to query that, I pulled the query logic from windows, but obviously shaderNodes aren't query able, so I'm still hunting down how to implement this.
