[x3d-public] x3d.py ProtoBody setter?
John Carlson
yottzumm at gmail.com
Thu Aug 19 01:46:21 PDT 2021
Fixing these bugs will give me a lot more confidence in x3d.py. I
hope there's a 4.0.42 version soon!
Existing pattern, finding errors:
@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
===========================
Exception (file attached will probably be rejected). There are many
files with this error. Could it be a python 3.8 issue? I'm guessing
that ProtoBody is not a type as in the arg 2 tuple, rather it's a
parameter? (see above)
$ python3 rubik.py
x3d.py package loaded, have fun with X3D Graphics!
Traceback (most recent call last):
File "rubik.py", line 37, in <module>
ProtoDeclare(name='sphereproto',
File
"/home/coderextreme/X3DJSONLD/src/main/python/net/x3djsonld/data/x3d.py",
line 10239, in __init__
self.ProtoBody = ProtoBody
File
"/home/coderextreme/X3DJSONLD/src/main/python/net/x3djsonld/data/x3d.py",
line 10282, in ProtoBody
if not ProtoBody is None and not
isinstance(ProtoBody,(ProtoBody,ProtoInstance)):
TypeError: isinstance() arg 2 must be a type or tuple of types
=================================
Perhaps this would be a better pattern? Please look over this
carefully! (the two str() functions are different).
def ProtoBody(self, xxProtoBody):
if xxProtoBody is None:
xxProtoBody = None # default
assertValidSFNode(xxProtoBody)
if not xxProtoBody is None and not
isinstance(xxProtoBody,(ProtoBody,ProtoInstance)):
# print(flush=True)
raise X3DTypeError(str(xxProtoBody) + ' does not match
required node type (ProtoBody,ProtoInstance) and is invalid')
self.__ProtoBody = xxProtoBody
also:
@ProtoInterface.setter
def ProtoInterface(self, xxProtoInterface):
if xxProtoInterface is None:
xxProtoInterface = None # default
assertValidSFNode(xxProtoInterface)
if not xxProtoInterface is None and not
isinstance(xxProtoInterface,(ProtoInterface,ProtoInstance)):
# print(flush=True)
raise X3DTypeError(str(ProtoInterface) + ' does not match
required node type (ProtoInterface,ProtoInstance) and is invalid')
self.__ProtoInterface = xxProtoInterface
========================================================
Possible patch like above, but there's errors in ```ant examples``` and
```ant examples.SmokeTests```, so please look over this carefully, or
provide your own implementation of setters. This just got past the two
"problems" above. There's more to do here.
$ svn diff X3duomToX3dPythonPackage.xslt
Index: X3duomToX3dPythonPackage.xslt
===================================================================
--- X3duomToX3dPythonPackage.xslt (revision 32361)
+++ X3duomToX3dPythonPackage.xslt (working copy)
@@ -2727,17 +2727,9 @@
<xsl:text>
def </xsl:text><!-- setter -->
<xsl:value-of select="$fieldName"/>
- <xsl:text>(self, </xsl:text>
- <xsl:value-of select="$fieldName"/>
- <!-- =None is omitted; note that setter NEVER needs a
default argument, it is only EVER called if attribute is being assigned
with RHS passed as single argument -->
- <xsl:text>):
- if </xsl:text>
- <xsl:value-of select="$fieldName"/>
- <xsl:text> is None:
- </xsl:text>
- <xsl:value-of select="$fieldName"/>
- <xsl:text> = </xsl:text>
- <xsl:choose>
+ <xsl:text>(self, xx</xsl:text><xsl:value-of
select="$fieldName"/><!-- =None is omitted; note that setter NEVER needs
a default argument, it is only EVER called if attribute is being
assigned with RHS passed as single argument --><xsl:text>):
+ if </xsl:text>xx<xsl:value-of select="$fieldName"/><xsl:text>
is None:</xsl:text>
+ xx<xsl:value-of select="$fieldName"/><xsl:text> =
</xsl:text><xsl:choose>
<xsl:when test="(string-length(@default) > 0)">
<xsl:value-of select="$defaultValue"/>
<xsl:text> # default</xsl:text>
@@ -2798,9 +2790,7 @@
<xsl:text>
assertValid</xsl:text>
<xsl:value-of select="@type"/>
- <xsl:text>(</xsl:text>
- <xsl:value-of select="$fieldName"/>
- <xsl:text>)</xsl:text>
+ <xsl:text>(</xsl:text>xx<xsl:value-of
select="$fieldName"/><xsl:text>)</xsl:text>
<xsl:call-template name="valueRestrictionCheck">
<xsl:with-param name="fieldName" select="$fieldName"/>
<xsl:with-param name="indent"><xsl:text>
</xsl:text></xsl:with-param>
@@ -2814,13 +2804,7 @@
<xsl:choose>
<xsl:when test="(@type = 'SFNode') and
(string-length($acceptableNodeTypes) > 0)">
<xsl:text>
- if not </xsl:text>
- <xsl:value-of select="$fieldName"/>
- <xsl:text> is None and not isinstance(</xsl:text>
- <xsl:value-of select="$fieldName"/>
- <xsl:text>,</xsl:text>
- <xsl:value-of select="$acceptableNodeTypes"/>
- <xsl:text>):
+ if not </xsl:text>xx<xsl:value-of
select="$fieldName"/><xsl:text> is None and not
isinstance(</xsl:text>xx<xsl:value-of
select="$fieldName"/><xsl:text>,</xsl:text><xsl:value-of
select="$acceptableNodeTypes"/><xsl:text>):
# print(flush=True)
raise X3DTypeError(str(</xsl:text>
<xsl:value-of select="$fieldName"/>
@@ -2838,8 +2822,7 @@
<xsl:text>
self.__</xsl:text>
<xsl:value-of select="$fieldName"/>
- <xsl:text> = </xsl:text>
- <xsl:value-of select="$fieldName"/>
+ <xsl:text> = </xsl:text>xx<xsl:value-of
select="$fieldName"/>
</xsl:if>
</xsl:for-each>
-------------- 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