I am creating a script that creates node in the node editor. I need to accept some properties from the user after creating the node. So I wrote a draw
method.
But looks like operator.draw
is not being called when the context is the node editor. I have put a print statement inside draw(self, context)
to verify this. If I change the space type to 'VIEW_3D', the draw
method is called.
Is the operator draw method only available in 3d view?
Edited with example:
In example below the message "inside op draw" is called on click of panel in 3d view not in node view:
import bpy
bl_info = {
'name': 'test node editor op',
'category': 'Object',
'blender': (2, 80, 0),
}
class my_op(bpy.types.Operator):
bl_idname = 'object.test_op'
bl_label = 'test node op'
bl_options = {'REGISTER', 'UNDO'}
x : bpy.props.FloatProperty(name='X')
y : bpy.props.FloatProperty(name='Y')
def execute(self, context):
if(context.area.type == 'VIEW_3D'):
bpy.ops.mesh.primitive_cube_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.location.x=self.x
bpy.context.object.location.y=self.y
else:
if(bpy.context.active_object!=None):
if(bpy.context.active_object.active_material == None):
m=bpy.data.materials.new('my material')
bpy.context.active_object.data.materials.append(m)
bpy.context.active_object.active_material.use_nodes = True
n = bpy.context.active_object.active_material.node_tree.nodes.new('ShaderNodeValue')
n.location = (self.x, self.y)
return {'FINISHED'}
def draw(self, context):
print('inside op draw')
col = self.layout.column()
col.prop(self, 'x')
col.prop(self, 'y')
class simple_3dview_panel(bpy.types.Panel):
bl_label = 'simple 3d'
bl_idname = 'simple_3d_panel'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'My View 3D Panel'
def draw(self, context):
col = self.layout.column()
col.operator('object.test_op')
class simple_node_panel(bpy.types.Panel):
bl_label = 'simple node'
bl_idname = 'simple_node_panel'
bl_space_type = 'NODE_EDITOR'
bl_region_type = 'UI'
bl_category = 'My Node Panel'
def draw(self, context):
col = self.layout.column()
col.operator('object.test_op')
def register():
bpy.utils.register_class(my_op)
bpy.utils.register_class(simple_node_panel)
bpy.utils.register_class(simple_3dview_panel)
def unregister():
bpy.utils.unregister_class(simple_node_panel)
bpy.utils.unregister_class(simple_3dview_panel)
bpy.utils.unregister_class(my_op)
if(__name__ == '__main__'):
register()
```
col
is not defined so the call and thus the script won't work anyway... Further reading: What do operator methods do? (poll, invoke, execute, draw & modal) $\endgroup$context
reference passed by each method (def draw(self, context):
) so I replacedbpy.context.*
bycontext.*
in your entire script. Also, as mentioned there are conventions for class names eg._PT_
, otherwise you'll get a warning in the console. $\endgroup$