[x3d-public] native Python package for X3D

Brutzman, Donald (Don) (CIV) brutzman at nps.edu
Sun Jul 14 02:48:18 PDT 2019


Loren and I had another good session Friday, he showed me multiple pythonic patterns for properties and setters.

Now have Python package implementation including all field types, and including of all simple fields.

Two X3D field names were problematic due to collision with reserved Python keywords.
* class  mapped to cssClass (similarly in X3DJSAIL)
* global mapped to Global
https://docs.python.org/3/reference/lexical_analysis.html#keywords

Excerpted smoke-test source and results:
========================================
# - - - - - - -
test = SFRotation()
test = SFRotation((0, .5, 1, 0.75)) # commas required
test.value = (0, .5, 1, 0.75)    # commas required
print ("SFRotation test.value =", test.value)

test = MFRotation()
test = MFRotation([(0, .5, 1, 0.75),(1, .5, 0, 0.75)]) # commas required
test.value =   [(0, .5, 1, 0.75),(1, .5, 0, 0.75)]  # commas required
# test = MFRotation( 0, .5, 1, 0.75, 1, .5, 0, 0.75 ) # TODO
print ("MFRotation test.value =", test.value)
# - - - - - - -

material = Material()
material = Material(diffuseColor=(0.5,0.5,0.5), transparency=0.2, DEF='Grey')
print('material=' + str(material) + ' isSFNode(material)=' + str(isSFNode(material)))
print('material DEF=' + str(material.DEF) + ' diffuseColor=' + str(material.diffuseColor) + ' emissiveColor=' + str(material.emissiveColor) + ' transparency=' + str(material.transparency))

========================================
X3dPythonPackage smoke tests:
SFBool  test.value = True
MFBool  test.value = [False, True]
SFInt32 test.value = 1
MFInt32 test.value = [0, 1, 2, 3, 4, 5]
SFFloat test.value = 1
MFFloat test.value = [0, 1, 2]
SFDouble test.value = 1
MFDouble test.value = [0, 1, 2]
SFString test.value = test setter
MFString test.value = ['test', 'setter']
SFVec2f test.value = (1, 2)
MFVec2f test.value = [(0, 1), (2, 3)]
SFVec2d test.value = (1, 2)
MFVec2d test.value = [(0, 1), (2, 3)]
SFVec3f test.value = (1, 2, 3)
MFVec3f test.value = [(0, 1, 2), (3, 4, 5)]
SFVec3d test.value = (1, 2, 3)
MFVec3d test.value = [(0, 1, 2), (3, 4, 5)]
SFVec4f test.value = (1, 2, 3, 4)
MFVec4f test.value = [(0, 1, 2, 3), (4, 5, 6, 7)]
SFVec4d test.value = (1, 2, 3, 4)
MFVec4d test.value = [(0, 1, 2, 3), (4, 5, 6, 7)]
SFColor test.value = (0, 0.5, 1)
MFColor test.value = [(0, 0.5, 1), (1, 0.5, 0)]
SFColorRGBA test.value = (0, 0.5, 1, 0.75)
MFColorRGBA test.value = [(0, 0.5, 1, 0.75), (1, 0.5, 0, 0.75)]
SFRotation test.value = (0, 0.5, 1, 0.75)
MFRotation test.value = [(0, 0.5, 1, 0.75), (1, 0.5, 0, 0.75)]
material=<__main__.Material object at 0x00000184C2707940> isSFNode(material)=True
material DEF=Grey diffuseColor=(0.5, 0.5, 0.5) emissiveColor=(0, 0, 0) transparency=0.2
X3dPythonPackage smoke tests complete.
========================================

Example patterns also follow, excerpted from X3dPythonPackage.py autogenerated result.

def isSFFloat (value):
     if not isinstance(value, float) and not isinstance(value, int):
         raise RuntimeError(str(value) + ' is not a valid float value for SFFloat')
     return True

class SFFloat:
     """
     SFFloat is a single-precision floating-point type.
     """
     specificationUrl = 'https://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/fieldsDef.html#SFFloatAndMFFloat'
     tooltipUrl       = 'https://www.web3d.org/x3d/tooltips/X3dTooltips.html#SFFloat'
     defaultValue = 0.0
     regex = '(\s)*([+-]?((0|[1-9][0-9]*)(\.[0-9]*)?|\.[0-9]+)([Ee][+-]?[0-9]+)?)(\s)*'
     def __init__(self, value = None):
         if  value == None:
             value = self.defaultValue
         self.value = value
     @property
     def value (self):
         return self.__value
     @value.setter
     def value (self, value = None):
         if  value == None:
             value = self.defaultValue
         self.__value = value
         isSFFloat(value)

def isSFColor (value):
     if not isinstance(value, tuple):
         raise RuntimeError(str(value) + ' is not a valid Python tuple for SFColor')
     tupleCount = 0
     for each in value:
         tupleCount += 1
         if not isinstance(each, float) and not isinstance(each, int):
             raise RuntimeError('SFColor element ' + str(each) + ' is not a valid float')
     if tupleCount != 3:
         raise RuntimeError('SFColor ' + str(value) + ' has ' + str(tupleCount) + ' elements instead of 3')
     return True
         
def isMFColor (value):
     if not isinstance(value, list):
         raise RuntimeError(str(value) + ' is not a valid Python list for MFColor')
     _index = 0
     for each in value:
         if not isinstance(each, tuple):
             raise RuntimeError('MFColor element #' + str(_index) + ' with value ' + str(each) + ' is not a valid tuple')
         _index += 1
         for element in each:
             if not isinstance(element, float) and not isinstance(element, int):
                 raise RuntimeError('MFColor element #' + str(_index) + ' tuple ' + str(each) + ' has value=' + str(element) + ' that is not a valid float')
     return True

========================================

So, good progress and program syntax is emerging. Will work on SFNode/MFNode children next.  At that point we can look at model conversions.


On 7/6/2019 4:47 PM, Brutzman, Donald (Don) (CIV) wrote:
> Have started work, initial build harness is in place.
> 
> Am generating from X3DUOM via stylesheet, in x3d stylesheets subdirectory 'python' at same level as subdirectory 'java' for X3DJSAIL.
> 
> http://www.web3d.org/x3d/stylesheets/build.xml
> ant target BuildX3dPythonPackageFromX3duom.saxon
> 
> using stylesheet
> http://www.web3d.org/x3d/stylesheets/X3duomToX3dPythonPackage.xslt
> 
> producing
> http://www.web3d.org/x3d/stylesheets/python/X3dPythonPackage.py
> 
> version control:
> 
> https://sourceforge.net/p/x3d/code/HEAD/tree/www.web3d.org/x3d/stylesheets/
> https://sourceforge.net/p/x3d/code/HEAD/tree/www.web3d.org/x3d/stylesheets/build.xml
> https://sourceforge.net/p/x3d/code/HEAD/tree/www.web3d.org/x3d/stylesheets/X3duomToX3dPythonPackage.xslt
> 
> https://sourceforge.net/p/x3d/code/HEAD/tree/www.web3d.org/x3d/stylesheets/python/X3dPythonPackage.py
> [...]

all the best, Don
-- 
Don Brutzman  Naval Postgraduate School, Code USW/Br       brutzman at nps.edu
Watkins 270,  MOVES Institute, Monterey CA 93943-5000 USA   +1.831.656.2149
X3D graphics, virtual worlds, navy robotics http://faculty.nps.edu/brutzman


More information about the x3d-public mailing list