[x3d-public] Next, in Python news...

John Carlson yottzumm at gmail.com
Sun Aug 22 05:33:58 PDT 2021


Don, attached is the python, with slight changes that does not produce 
XML.  Indeed, it errors out in ProtoBody setter in ??? class.

I tried to patch  x3d.py, but ran into other issues.

There might be an issue with Linux linefeeds?

I can convert if you can't.  I recommend WordPad instead of NotePad.

John


On 8/21/21 11:57 PM, Brutzman, Donald (Don) (CIV) wrote:
> Hi John, am trying to follow your message.
>
> We have pylint but recent upgrade to python caused a gap, not resolved yet.
>
> Meanwhile python evaluates everything building the scene graph quite strictly, primarily through setters when creating values.  I'm not sure it is possible to create invalid XML, VRML or JSON from x3d.py package.  If such a case is found, let's fix the bug it indicates.
>
> If you have an example python program/snippet that does _not_ produce valid XML, VRML or JSON, please advise.
>
> v/r Don
>
>
> -----Original Message-----
> From: x3d-public <x3d-public-bounces at web3d.org> On Behalf Of John Carlson
> Sent: Saturday, August 21, 2021 3:10 AM
> To: X3D Graphics public mailing list <x3d-public at web3d.org>
> Subject: [x3d-public] Next, in Python news...
>
> I've been converting JSON schema to pure python validators (no separate JSON schema file required) with a python package (not mine).  If I could convert X3DUOM to Python and get a free XML, VRML, and JSON schema validation in Python, that would WOW me. BTW, I see that you don't use an XML parser in Python yet?  May I recommend the popular ElementTree built-in package.  Here's a simple pattern (in python):
>
> import xml.etree.ElementTree
> soup =
> xml.etree.ElementTree.parse(open("../../specifications/X3dUnifiedObjectModel-4.0.xml")).getroot()
>
> So, Don should look at pylint output in the logs, perhaps? Me too!  I just found out about pylint!  I can find bugs in Python while Don works on Python-creating XSLT?   That would seem a better division of labor. I just ran ```ant pylint```.  I can do that and search for bugs in x3d.py.  I see that my changes are 1.53/10 worse, so I'm backing the changes that I made to X3duomToX3dPythonPackage.xslt. Please revisit this email, or look at code I pasted below.  The problem is around isinstance.  I could be misunderstanding duck typing.  To me, 2 ProtoBody's inside the isinstance parameters spells trouble, but I'm unsure of a fix at this point.  I have a backup of my changes should we need to recover.  Perhaps a step forward is better than a step backwards?
>
>       @ProtoBody.setter
>       def ProtoBody(self, ProtoBody):
>           if  ProtoBody is None:
>               ProtoBody = None # default
>           assertValidSFNode(ProtoBody)
>           if not ProtoBody is None and not
> isinstance(ProtoBody,(ProtoBody,ProtoInstance)):
>               # print(flush=True)
>               raise X3DTypeError(str(ProtoBody) + ' does not match required node type (ProtoBody,ProtoInstance) and is invalid')
>           self.__ProtoBody = ProtoBody
>
>
> _______________________________________________
> x3d-public mailing list
> x3d-public at web3d.org
> http://web3d.org/mailman/listinfo/x3d-public_web3d.org
-------------- next part --------------
###############################################
#
# Now available: developmental python x3d.py package on PyPi for import.
#   This approach greatly simplifies Python X3D deployment and use.
#   https://pypi.org/project/x3d
#
# Installation:
#       pip install x3d
# or
#       python -m pip install x3d
#
# Developer options for loading x3d package:
#
#    from x3d import *  # preferred approach, terser source that avoids x3d.* class prefixes
#
# or
#    import x3d         # traditional way to subclass x3d package, all classes require x3d.* prefix
#                       # but python source is very verbose, for example x3d.Material x3d.Shape etc.
#                       # X3dToPython.xslt stylesheet insertPackagePrefix=true supports this option.

from x3d import *

###############################################

newModel=X3D(profile='Immersive',version='3.3',
  head=head(
    children=[
    meta(name='title',content='rubik.x3d'),
    meta(name='creator',content='John Carlson'),
    meta(name='generator',content='manual'),
    meta(name='identifier',content='https://coderextreme.net/X3DJSONLD/rubik.x3d'),
    meta(name='description',content='a kind of rubik cube with spheres')]),
  Scene=Scene(
    children=[
    NavigationInfo(type=["EXAMINE"]),
    Viewpoint(description='Rubiks Cube',position=(0,0,12)),
    ProtoDeclare(name='sphereproto',
      ProtoInterface=ProtoInterface(
        field=[
        field(name='xtranslation',accessType='inputOutput',type='SFVec3f',value=(0.0,0.0,0.0))]),
      ProtoBody=ProtoBody(
        children=[
        Transform(
          IS=IS(
            connect=[
            connect(nodeField='translation',protoField='xtranslation')]),
          children=[
          Shape(
            geometry=Sphere(),
            appearance=Appearance(
              material=Material(diffuseColor=(1,1,1))))])])),
    ProtoDeclare(name='three',
      ProtoInterface=ProtoInterface(
        field=[
        field(name='ytranslation',accessType='inputOutput',type='SFVec3f',value=(0.0,0.0,0.0))]),
      ProtoBody=ProtoBody(
        children=[
        Transform(
          IS=IS(
            connect=[
            connect(nodeField='translation',protoField='ytranslation')]),
          children=[
          ProtoInstance(name='sphereproto',
            fieldValue=[
            fieldValue(name='xtranslation',value=(0,0,0))]),
          ProtoInstance(name='sphereproto',
            fieldValue=[
            fieldValue(name='xtranslation',value=(2,0,0))]),
          ProtoInstance(name='sphereproto',
            fieldValue=[
            fieldValue(name='xtranslation',value=(-2,0,0))])])])),
    ProtoDeclare(name='nine',
      ProtoInterface=ProtoInterface(
        field=[
        field(name='ztranslation',accessType='inputOutput',type='SFVec3f',value=(0.0,0.0,0.0))]),
      ProtoBody=ProtoBody(
        children=[
        Transform(
          IS=IS(
            connect=[
            connect(nodeField='translation',protoField='ztranslation')]),
          children=[
          ProtoInstance(name='three',
            fieldValue=[
            fieldValue(name='ytranslation',value=(0,0,0))]),
          ProtoInstance(name='three',
            fieldValue=[
            fieldValue(name='ytranslation',value=(0,2,0))]),
          ProtoInstance(name='three',
            fieldValue=[
            fieldValue(name='ytranslation',value=(0,-2,0))])])])),
    ProtoDeclare(name='twentyseven',
      ProtoInterface=ProtoInterface(
        field=[
        field(name='ttranslation',accessType='inputOutput',type='SFVec3f',value=(0.0,0.0,0.0))]),
      ProtoBody=ProtoBody(
        children=[
        Transform(
          IS=IS(
            connect=[
            connect(nodeField='translation',protoField='ttranslation')]),
          children=[
          ProtoInstance(name='nine',
            fieldValue=[
            fieldValue(name='ztranslation',value=(0,0,0))]),
          ProtoInstance(name='nine',
            fieldValue=[
            fieldValue(name='ztranslation',value=(0,0,2))]),
          ProtoInstance(name='nine',
            fieldValue=[
            fieldValue(name='ztranslation',value=(0,0,-2))])])])),
    ProtoInstance(name='twentyseven',
      fieldValue=[
      fieldValue(name='ttranslation',value=(0,0,0))])])
) # X3D model complete

###############################################
# Self-test diagnostics
###############################################

if        metaDiagnostics(newModel): # built-in utility method in X3D class
    print(metaDiagnostics(newModel))
print('check  newModel.XML() serialization...')
newModelXML = newModel.XML() # test export method XML() for exceptions
print(newModelXML) # debug

print ("python x3d.py load successful for rubik.py")


More information about the x3d-public mailing list