[x3d-public] Question about a Python and x3d.py Error message

Brutzman, Donald (Don) (CIV) brutzman at nps.edu
Wed Jan 15 09:20:19 PST 2025


Thanks for using x3d.py and for asking.

Before diving into Python internals of x3d.py, which is rarely necessary, wondering what you really want to do.

There is already a top-level class for creating an instance of an X3D scene graph.  You then add nodes and statements.

The library is "Pythonic" in that it is intentionally strict about what you are allowed to construct.  Parent-child node relationships must be legal.  Attribute values must be legal.  I don't know if it is possible to build an illegal scene graph with x3d.py.

Hopefully the following verbose examples are illustrative.

Recommend next looking at design patterns for Hello World, and the SmokeTests example, and any of the many X3D Examples Archives scene that get converted into python, then run via command line as unit tests.

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

  *
Python X3D Package x3d.py
  *
X3D Python Scene Access Interface Library (X3DPSAIL)
  *
Download and Installation | Design Features and Data Types | Development | Examples | Jupyter Notebook | PyPI for The x3d.py Python X3D Package supports programmers with Python interfaces and objects for standards-based X3D programming, all as open source.
  *
The presentation Python X3D Package Implementation provides an overview and shows examples.
  *
https://www.web3d.org/x3d/stylesheets/python/python.html
  *
https://www.web3d.org/x3d/stylesheets/python/python.html#Examples

===================================================================
The HelloWorld example shows a nested Construction of a scene graph.

  *
https://www.web3d.org/x3d/stylesheets/python/examples/HelloWorld.py

from x3d import *

#  comment preceding root node
newModel=X3D(profile='Immersive',version='3.3',
  head=head(
    children=[
    meta(content='HelloWorld.x3d',name='title'),
   // ...

    meta(content='HelloWorld.json',name='reference')]),
  Scene=Scene(
    #  Example scene to illustrate X3D nodes and fields (XML elements and attributes)
    children=[
    WorldInfo(title='Hello World!'),
    WorldInfo(title="Hello ' apostrophe 1"),
    WorldInfo(title="Hello ' apostrophe 2"),
    WorldInfo(title='Hello " quotation mark 3'),
    WorldInfo(title='Hello " quotation mark 4'),
    MetadataSet(name="items'",
      value=[
      MetadataInteger(name='one',value=[1]),
      MetadataInteger(name='two',value=[2])]),
    Group(
      children=[
      Viewpoint(DEF='ViewUpClose',centerOfRotation=(0,-1,0),description='Hello world!',position=(0,-1,7)),
      #  insert commas to test removal when converted to ttl
      Transform(DEF='TestWhitespaceCommas',rotation=(0,1,0,3),
        children=[
        Shape(
          geometry=Sphere(),
          appearance=Appearance(
            material=Material(DEF='MaterialLightBlue',diffuseColor=(0.1,0.5,1)),
            texture=ImageTexture(DEF='ImageCloudlessEarth',url=["earth-topo.png","earth-topo.jpg","earth-topo-small.gif","http://www.web3d.org/x3d/content/examples/Basic/earth-topo.png","http://www.web3d.org/x3d/content/examples/Basic/earth-topo.jpg","http://www.web3d.org/x3d/content/examples/Basic/earth-topo-small.gif"])))]),
      Transform(translation=(0,-2,0),
        children=[
        Shape(
          geometry=Text(DEF='TextMessage',string=["Hello","world!"],
            fontStyle=FontStyle(justify=["MIDDLE","MIDDLE"])),
          appearance=Appearance(
            material=Material(USE='MaterialLightBlue')))])])])
) # X3D model complete




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

print('Self-test diagnostics for HelloWorld.py:')
if        metaDiagnostics(newModel): # built-in utility method in X3D class
    print(metaDiagnostics(newModel)) # display meta info, hint, warning, error, TODO values in this model
# print('check newModel.XML() serialization...')
newModelXML= newModel.XML() # test export method XML() for exceptions during export
newModel.XMLvalidate()
# print(newModelXML) # diagnostic
  // ... etc.

===================================================================
Self-test results:

  *
https://www.web3d.org/x3d/stylesheets/python/build.examples.log.txt


print("python HelloWorld.py load and self-test diagnostics complete.")
===========================================
Loading local x3d.py package in python to check for correctness...
  then test HelloWorld.py execution and self-validation:
  python examples/HelloWorld.py
x3d.py package 4.0.64.5 loaded, have fun with X3D Graphics!
Self-test diagnostics for HelloWorld.py:
Python-to-XML well-formed XML document  test of XML output complete
Python-to-XML X3D 3.3 schema validation test of XML output complete
Python-to-VRML export of VRML output successful
Python-to-JSON export of JSON output successful (under development)
python HelloWorld.py load and self-test diagnostics complete.
convert.X3dToPython.xslt conversion of HelloWorld.x3d to HelloWorld.py complete.
===========================================

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

Comprehensive smoke tests example:

  *   PythonX3dSmokeTests.py performs "smoke tests" of package functionality every time x3d.py is built.
  *
https://www.web3d.org/x3d/stylesheets/python/examples/PythonX3dSmokeTests.py
  *
Test output from most recent build:
  *
https://www.web3d.org/x3d/stylesheets/python/build.examples.log.txt

This should let you focus on creation of your X3D scene graph.  Once you have that, there are plenty of methods for use with other python code.

Yes some things are hard, at first... But you only have to get them right once.

Hope this helps Aaron.  Have fun with X3D Python!  🙂


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 https://faculty.nps.edu/brutzman



________________________________
From: x3d-public <x3d-public-bounces at web3d.org> on behalf of Bergstrom, Aaron via x3d-public <x3d-public at web3d.org>
Sent: Wednesday, January 15, 2025 8:01 AM
To: x3d-public at web3d.org <x3d-public at web3d.org>
Cc: Bergstrom, Aaron <aaron.bergstrom at und.edu>
Subject: [x3d-public] Question about a Python and x3d.py Error message


I need help troubleshooting a x3d.py error message.



In developing a Python version of the Rawkee X3D exporter plugin for Maya, I am attempting to create nodes that inherit from both the Maya Python API 2.0 and x3d.py



As such, I have a question about python and multiple class inheritance, and an error I am seeing. Admittedly, I am not the strongest Python programmer.



I am getting the following error when I execute the Python code in Maya, and I’m pretty sure I do not of the __init__() function setup correctly.



# Error: TypeError: file C:\Users\user.name\AppData\Roaming\Python\Python311\site-packages\x3d\x3d.py line 14984: Scene.hasChild() missing 1 required positional argument: 'self' #



Can anyone with some Python experience give me an idea of what I am doing wrong here?



Here is the code:

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

import x3d



import maya.api.OpenMaya as aom

import maya.api.OpenMayaUI as aomui

import maya.api.OpenMayaRender as aomr



# import xmltodict

# import json



class X3DScene (aom.MPxNode, x3d.Scene):

    TYPE_NAME    = "Scene"

    TYPE_ID      = aom.MTypeId(0x00108FFF)

    PROFILE_TYPE = "Full"

    VERSION      = "4.0"



    def __init__(self):

        super(X3DScene, self).__init__()



    @classmethod

    def creator(cls):

        return X3DScene()



    @classmethod

    def initialize(cls):

        cls.x3dDoc = x3d.X3D(profile=cls.PROFILE_TYPE, version=cls.VERSION)

        cls.x3dDoc.Scene = cls

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

        # Test dump to make sure the class works as expected

        #print(json.dumps(xmltodict.parse(cls.x3dDoc.XML()), indent=4))

        print(cls.x3dDoc.XML())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20250115/d3a7834b/attachment-0001.html>


More information about the x3d-public mailing list