Python Nodes: subclassing broken?

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
OEP
Posts: 0
Joined: Sat May 18, 2013 12:47 pm

Python Nodes: subclassing broken?

Post by OEP » Sat May 18, 2013 1:18 pm

I'm playing around with the new Python Nodes API and I noticed some unusual behavior when using inheritance like I might normally with Python.

Here's my toy script:

Code: Select all

import bpy

class MyTree(bpy.types.NodeTree):
  bl_icon = "MESH_TORUS"
  bl_idname = "MYTREE"
  bl_label = "My Tree"

class A(bpy.types.Node):
  bl_idname = "Node_A"
  bl_label = "A Node"

  def init(self, context):
    print("Init A")
    self.custom()

  def custom(self):
    print("Custom A")


#class B(A):
#  bl_idname = "Node_B"
#  bl_label = "B Node"
#
#  def custom(self):
#    print("Custom B")

bpy.utils.register_class(MyTree)
bpy.utils.register_class(A)
#bpy.utils.register_class(B)
First you can convince yourself that this works. I copied and pasted this in the blender text editor, ran it, and I can add my custom node. This is what I get in the console as well:

Code: Select all

Init A
Custom A
So far so good. Now uncomment the parts concerning class B and repeat. Blender will fail to add A and print a traceback:

Code: Select all

Traceback (most recent call last):
  File "/usr/local/share/blender-2.67-linux-glibc211-x86_64/2.67/scripts/startup/bl_operators/node.py", line 197, in execute
    node = self.create_node(context, item[0].bl_rna.identifier)
  File "/usr/local/share/blender-2.67-linux-glibc211-x86_64/2.67/scripts/startup/bl_operators/node.py", line 49, in create_node
    node = tree.nodes.new(type=node_type)
RuntimeError: Error: Cannot add node of type Node_A to node tree 'NodeTree.001'
If I try and add a "Node B" there is no output to the console, whereas normal OOP would tell me that the parent's init() should have been called.

I dug around in the source code and noticed some metaclass magic that could possibly throw my expectations off.

OEP
Posts: 0
Joined: Sat May 18, 2013 12:47 pm

Post by OEP » Mon May 20, 2013 10:04 pm

I figured I'd update my own post with what I found out.

I couldn't find a bug report for it, but I think this was a side effect of the socket template system that was in place. I saw a revision on the SVN trunk (r56590) while googling which mentioned deleting this. So, I downloaded the latest nightly build and the Node.init() method seems to be working like I hoped it would!

Of course, I'm now having other problems, but I suppose it goes with the territory of using a nightly build. :)

Post Reply