[cgkit-commits] SF.net SVN: cgkit:[312] cgkit/trunk
Brought to you by:
mbaas
From: <mb...@us...> - 2009-05-09 10:00:36
|
Revision: 312 http://cgkit.svn.sourceforge.net/cgkit/?rev=312&view=rev Author: mbaas Date: 2009-05-09 10:00:26 +0000 (Sat, 09 May 2009) Log Message: ----------- RMShader: Passing array parameters via the constructor didn't work. Modified Paths: -------------- cgkit/trunk/cgkit/rmshader.py cgkit/trunk/changelog.txt Added Paths: ----------- cgkit/trunk/unittests/test_rmshader.py Modified: cgkit/trunk/cgkit/rmshader.py =================================================================== --- cgkit/trunk/cgkit/rmshader.py 2009-05-09 09:58:50 UTC (rev 311) +++ cgkit/trunk/cgkit/rmshader.py 2009-05-09 10:00:26 UTC (rev 312) @@ -280,32 +280,9 @@ # print 'declare("%s", type=%s, cls=%s, arraysize=%s, default=%s)'%(name, type, cls, arraysize, default) - # Create a "dummy shader" which will be passed to slparams to parse - # the declaration in name - shd = "surface spam(%s) {}"%name - try: - # Force a syntax error when name contains no declaration - if " " not in name: - raise slparams.SyntaxError() - slinfo = slparams.slparams(StringIO.StringIO(shd)) - shdtype, shdname, params = slinfo[0] - except slparams.SyntaxError, e: - # Check if name is only a single name or if there was an attempt - # to specify the entire declaration - invalid = " []():;'\"'" - for inv in invalid: - if inv in name: - raise ValueError, 'Invalid declaration: "%s"'%name - # It's probably really just the name, so use the remaining - # arguments to create a parameter tuple... - if cls==None: - cls = "uniform" - if type==None: - raise ValueError, 'No type for parameter "%s" specified'%name - if type not in ["float", "string", "color", "point", "vector", - "normal", "matrix"]: - raise ValueError, 'Invalid type for parameter "%s": %s'%(name, type) - params = [("", cls, type, arraysize, name, "", str(default))] + # Get a slparams-stype params tuple from either name alone or from + # all the args + params = self._declare_getParams(name, type, cls, arraysize, default) typelut = {"float":"double", "string":"str", @@ -323,7 +300,7 @@ pname = p[4] pdefault = slparams.convertdefault(p) slottype = typelut[ptype] - if parraylen==None: + if parraylen is None: decl = "%s %s"%(p[1], ptype) else: decl = "%s %s[%d]"%(p[1], ptype, parraylen) @@ -344,7 +321,18 @@ if pytype=="str": pdefault = self.undeclared[pname] else: - pdefault = eval("%s(%s)"%(pytype, repr(self.undeclared[pname]))) + # Scalar? + if parraylen is None: + pdefault = eval("%s(%s)"%(pytype, repr(self.undeclared[pname]))) + # Array + else: + userDefault = self.undeclared[pname] + try: + if len(userDefault)!=parraylen: + raise ValueError('Invalid default value for shader parameter "%s". Expected %s values, but got %s'%(pname, parraylen, len(userDefault))) + except TypeError: + raise TypeError('Invalid default value for shader parameter "%s". Expected a sequence of %s values.'%(pname, parraylen)) + pdefault = [eval("%s(%s)"%(pytype, repr(v))) for v in self.undeclared[pname]] del self.undeclared[pname] # Create the slot and add the variable to the params dictionary @@ -353,7 +341,45 @@ else: self.createSlot(pname, slottype, parraylen, pdefault) self.shaderparams[pname] = decl + + def _declare_getParams(self, name, type, cls, arraysize, default): + """Helper method for declare(). + Turns the arguments into a params "tuple". See declare() for a description + of the arguments. + The return value is either a params object as returned by slparams() + or an old-style params tuple. + """ + # Create a "dummy shader" which will be passed to slparams to parse + # the declaration in name + shd = "surface spam(%s) {}"%name + try: + # Force a syntax error when name contains no declaration + if " " not in name: + raise slparams.SyntaxError() + slinfo = slparams.slparams(StringIO.StringIO(shd)) + shdtype, shdname, params = slinfo[0] + except slparams.SyntaxError, e: + # Check if name is only a single name or if there was an attempt + # to specify the entire declaration + invalid = " []():;'\"'" + for inv in invalid: + if inv in name: + raise ValueError, 'Invalid declaration: "%s"'%name + # It's probably really just the name, so use the remaining + # arguments to create a parameter tuple... + if cls is None: + cls = "uniform" + if type is None: + raise ValueError, 'No type for parameter "%s" specified'%name + if type not in ["float", "string", "color", "point", "vector", + "normal", "matrix"]: + raise ValueError, 'Invalid type for parameter "%s": %s'%(name, type) + params = [("", cls, type, arraysize, name, "", str(default))] + + return params + + # loadShaderSource def loadShaderSource(self): """Load shader source and replace the shader name. @@ -365,14 +391,14 @@ \return Shader source code or None """ - if self.filename==None: + if self.filename is None: return None f = file(self.filename) src = f.read() # Search for <shader type> + one or more white space + <shadername>.. match = re.search("%s\s+%s"%(self.shadertype, self.shadername), src) - if match!=None: + if match is not None: s, e = match.start(), match.end() src = src[:e-len(self.shadername)] + "$SHADERNAME" + src[e:] else: @@ -393,14 +419,14 @@ \param default Default value or None """ - if arraylen==None: + if arraylen is None: exec "slot = %sSlot()"%type.capitalize() - if default!=None: + if default is not None: slot.setValue(default) else: exec "slot = %sArraySlot()"%type.capitalize() slot.resize(arraylen) - if default!=None: + if default is not None: for i,v in enumerate(default): slot[i] = v Modified: cgkit/trunk/changelog.txt =================================================================== --- cgkit/trunk/changelog.txt 2009-05-09 09:58:50 UTC (rev 311) +++ cgkit/trunk/changelog.txt 2009-05-09 10:00:26 UTC (rev 312) @@ -10,6 +10,8 @@ Bug fixes/enhancements: +- RMShader: Passing parameter values for array parameters in the constructor + didn't work. - ri/cri: Added the following calls: RiArchiveBegin(), RiArchiveEnd(), RiCamera(), RiDisplayChannel(), RiIfBegin(), RiElseIf(), RiElse(), RiIfEnd(), Added: cgkit/trunk/unittests/test_rmshader.py =================================================================== --- cgkit/trunk/unittests/test_rmshader.py (rev 0) +++ cgkit/trunk/unittests/test_rmshader.py 2009-05-09 10:00:26 UTC (rev 312) @@ -0,0 +1,61 @@ +# Test the RMShader class + +import unittest +from cgkit.rmshader import RMShader +from cgkit.cgtypes import * +import numpy + +class TestRMShader(unittest.TestCase): + """Test the RMShader class. + """ + + def testShader(self): + """Test the RMShader class. + """ + shd = RMShader("data/testshader.sl") + # Attribute access + self.assertEqual(1.0, shd.Ka) + self.assertEqual((0,0,0), shd.norm) + self.assertEqual([1.0, 2.0], shd.uv) + self.assertEqual((0,0,0), shd.out) + self.assertEqual([(1.0,2.0,3.0), (4.0,5.0,6.0), (7.0,8.0,9.0)], shd.pnts) + self.assertEqual((1.0,1.0,1.0), shd.col) + self.assertEqual(mat4(1), shd.M) + self.assertEqual(0.5, shd.Kd) + self.assertEqual([0.5], shd.singleArr) + + # Slot access + self.assertEqual(1.0, shd.Ka_slot.getValue()) + self.assertEqual((0,0,0), shd.norm_slot.getValue()) + self.assertEqual(1.0, shd.uv_slot.getValue(0)) + self.assertEqual(2.0, shd.uv_slot.getValue(1)) + self.assertEqual((0,0,0), shd.out_slot.getValue()) + self.assertEqual((1.0,2.0,3.0), shd.pnts_slot.getValue(0)) + self.assertEqual((4.0,5.0,6.0), shd.pnts_slot.getValue(1)) + self.assertEqual((7.0,8.0,9.0), shd.pnts_slot.getValue(2)) + self.assertEqual((1.0,1.0,1.0), shd.col_slot.getValue()) + self.assertEqual(mat4(1), shd.M_slot.getValue()) + self.assertEqual(0.5, shd.Kd_slot.getValue()) + self.assertEqual(0.5, shd.singleArr_slot.getValue(0)) + + # Assign some new values + shd.Ka = 0.7 + self.assertEqual(0.7, shd.Ka_slot.getValue()) + shd.singleArr = [0.6] + self.assertEqual(0.6, shd.singleArr_slot.getValue(0)) + + def testConstructor(self): + """Test passing initial parameter values in the constructor. + """ + shd = RMShader("data/testshader.sl", Ka=0.7, norm=(0,0,1), uv=(0.2,0.3), singleArr=[0.8]) + self.assertEqual(0.7, shd.Ka_slot.getValue()) + self.assertEqual((0,0,1), shd.norm_slot.getValue()) + self.assertEqual(0.2, shd.uv_slot.getValue(0)) + self.assertEqual(0.3, shd.uv_slot.getValue(1)) + self.assertEqual(0.8, shd.singleArr_slot.getValue(0)) + + +###################################################################### + +if __name__=="__main__": + unittest.main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |