0
$\begingroup$

Cycles Render, Node Editor,

I want to add a tooltip for the shader buttons in the node toolshelf. I create a class for that that contains the tooltip and creates the node. And call it by a button then.

It works somehow. I have my tooltip and the class adds the node. But the use_transform setting gets ignored. Means the created node is stuck under the tool shelf or where ever it gets created, which is somehow random. I have to pick it up again.

What do i overlook here? Why is use_transform=True ignored?

Examplecode. First button is the one with my tooltip. The second one works directly. Note that you need to have an object and a material to get the first button to work. I haven't solved that part yet:

import bpy
import os
import bpy.utils.previews


################ Tooltip class ###############################

# Tooltip
class NODEEDITOR_texture_image(bpy.types.Operator):
    """Image Texture\nHere you can load an image as a texture"""
    bl_idname = "node.texture_image"
    bl_label = "texture image"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        bpy.ops.node.add_node(type="ShaderNodeTexImage", use_transform=True)
        return {'FINISHED'}  

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

class NodesIconPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "All Nodes"
    bl_idname = "nodes.common_buttons"
    bl_space_type = 'NODE_EDITOR'
    bl_region_type = 'TOOLS'
    bl_category = "All Nodes"

    @staticmethod
    def draw(self, context):
        layout = self.layout
        default_context = bpy.app.translations.contexts.default

        ##### Textbutton with Tooltip

        layout.operator("node.texture_image", text="Image Texture with Tooltip")

        #### Textbutton usual
        props = layout.operator("node.add_node", text="Image Texture")    
        props.use_transform = True
        props.type = "ShaderNodeTexImage"

 # -----------------------------------------------------------   


def register():

    # Register all classes

    bpy.utils.register_module(__name__)


def unregister():

    # Unregister all classes

    bpy.utils.unregister_module(__name__)

# This allows you to run the script directly from blenders text editor
# to test the addon without having to install it.

if __name__ == "__main__":
    register()

enter image description here

$\endgroup$
  • $\begingroup$ It works for me. $\endgroup$ – cmomoney Dec 15 '16 at 18:22
  • $\begingroup$ Have you tried with both buttons? The problem is not that it does not create the node. It is that the node does not stick to the mouse as it should. The use_transform part fails. $\endgroup$ – Tiles Dec 15 '16 at 18:39
  • $\begingroup$ The nodes does stick to the mouse. It works as expected. I even looked in the INFO and it lists the operator with the use_transform set to True. $\endgroup$ – cmomoney Dec 15 '16 at 18:43
  • $\begingroup$ With both buttons? $\endgroup$ – Tiles Dec 15 '16 at 18:44
  • $\begingroup$ I am still baffled that it works for you while it fails for me at two Windows 7 pc's with blender 2.76 up to 2.78. A Windows issue maybe? But the API should work OS independant. - I guess i have to give up here. Anyways. Many thanks for having a look and testing. $\endgroup$ – Tiles Dec 16 '16 at 8:08
1
$\begingroup$

There are two operators called: bpy.ops.node.add_node() and bpy.ops.node.translate_attach_remove_on_cancel(). To get the same result, you put the second(modal) operator in your return and use the INVOKE_DEFAULT option:

class NODEEDITOR_texture_image(bpy.types.Operator):
"""Image Texture\nHere you can load an image as a texture"""
bl_idname = "node.texture_image"
bl_label = "texture image"
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
    bpy.ops.node.add_node(type="ShaderNodeTexImage", use_transform=True)
    return bpy.ops.node.translate_attach_remove_on_cancel('INVOKE_DEFAULT')
$\endgroup$
  • $\begingroup$ Hm, this solution works. But throws an funny error now in the console: ERROR: Python context internal state bug. this should not happen! $\endgroup$ – Tiles Dec 17 '16 at 9:02
  • $\begingroup$ Ah, error doesn't happen in Blender 2.78. So all is good. Many thanks :) $\endgroup$ – Tiles Dec 17 '16 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.