mercredi 14 août 2019

using metaclasses as hybrid approach between handler and dynamic class creation

I am working on a library for maya and I am finding myself in the situation where I was writing custom classes to handle maya nodes and creating properties for each attribute I am interested in.

Then I realized I could use metaclasses to create something similar to how the models work in django, where for each node I would specify the name of the attribute and the type and let the metaclass create the rest. Something like:

class MultiplyDivideNode(mayacustom.Node):
    node_type = "multiplyDivide"

    input1X = mayacustom.Double()
    input1Y = mayacustom.Double()
    input1Z = mayacustom.Double()

    input1 = mayacustom.Double3(input1X, input1Y, input1Z)

    ...

Where mayacustom.Node is metaclass. In this way if someone wants to create a class to wrap its custom node can be easily achieved.

I was further thinking that if I have to deal with a node that already exists I could have kind of an handler class that, since it knows the type each class represent can create the right instance.

I know that metaclasses are used for both the described cases, I was just wondering if using them for both usages at the same time is still a good practice.

Thanks!

Aucun commentaire:

Enregistrer un commentaire