WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Properly handling Inheritance of VizNode and using copy (https://forum.worldviz.com/showthread.php?t=4341)

shivanangel 09-05-2012 09:58 AM

Properly handling Inheritance of VizNode and using copy
 
Hello Vizard support.

I had a question on how to properly implement the .copy method when using inheritance.

I have created several class that inherit from VizNode, however they have their own __init__ functions that take in additional parameters beyond what VizNode might utilize.

When I create a copy of these objects using the original copy method provided by Vizard, I get an error that indicates I am not getting the correct number of parameters into the class __init__ method I created.

When I looked at how copy was implemented, it looks like you return an instance of self.__class__(Vizard Stuff In Here) when I call copy.

Below is my __init__ method, as you can see I take in an additional template parameter.

Code:

class GreenObject(Model3D, Properties):
        def __init__(self, modelString, GOTemplate):
                Model3D.__init__(self, modelString)
               
                self.publicVariables = []
       
                self.variableValues = [0.0] * len(GreenObject.properties2Indices)
               
                self.greenObjectTemplate = GOTemplate

If possible, I would like to call the original copy functionality along with doing some copying of my own when <*>.copy is called.

Normally, I would just call the base classes version then add my own code. But I don't know much about what VizNode is doing behind the scenes and I figured you'd have a better idea on how to handle this.

Thanks,
George

farshizzo 09-05-2012 11:55 AM

Your example code does not show how the node object is created. Is it passed to your class, or is it created within the __init__ of your class?

shivanangel 09-05-2012 12:17 PM

Sorry, forgot to include that class as well.
Here is a super class of GreenObject called Model3D which derives
from viz.VizNode

Code:


class Model3D(viz.VizNode):
       
        GORM = None
       
        def __init__(self, string):
               
                #Typical string entry
                if type(string) == str:
                        base = viz.add(string)
                        viz.VizNode.__init__(self, base.id)
                               
                #Use for on-the-fly creation in Vizard
                elif isinstance(string, viz.VizNode):
                        base = string
                        viz.VizNode.__init__(self, base.id)
                               
                elif isinstance(string, int): #We are dealing with a copy that has passed in an id number
                        viz.VizNode.__init__(self, string)
                       
               
                else:
                        print 'Copy type', string, type(string)


farshizzo 09-05-2012 02:12 PM

The node.copy command will pass all extra keyword arguments to the class __init__ method. You can override the copy method in your class and have it pass all the required arguments as keyword arguments to the underlying node.copy method. Example:
Code:

import viz
viz.go()

class MyNode(viz.VizNode):

        def __init__(self, model, parameter):

                if isinstance(model,basestring):
                        base = viz.add(model)
                        viz.VizNode.__init__(self, base.id)

                elif isinstance(model, viz.VizNode):
                        viz.VizNode.__init__(self, model.id)

                elif isinstance(model, int):
                        viz.VizNode.__init__(self, model)

                self._parameter = parameter

        def copy(self, parameter):
                return viz.VizNode.copy(self,parameter=parameter)

node = MyNode('gallery.osgb',0)
nodeCopy = node.copy(1)

Does this work for you scenario?

shivanangel 09-06-2012 06:56 AM

I believe it will work.
I'll create a new instance of the class in the copy constructor and pass the copy into the initializer.

This way the copy command will now not just copy viz.VizNode but I can code it to also copy the rest of the parameters I've attached to my new class as well.

Thank you,
George


All times are GMT -7. The time now is 04:46 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC