4
$\begingroup$

I want to import the action template of action editor, and use template ID to display it. But the 'new' button is disabled, how could I active it?

here is my code currently:

import bpy

def drawRenderEngine(self, context):
        layout = self.layout
        ob = context.object

        window = context.window
        screen = context.screen
        scene = window.scene

        if ob is not None:
            bpy.context.object.animation_data_create()
            layout.template_ID(context.object.animation_data, "action", new='action.new')




bpy.types.TOPBAR_HT_upper_bar.draw_right = drawRenderEngine

enter image description here

$\endgroup$
2
$\begingroup$

The operator does not poll.

The issue arises since the operator bpy.ops.action.new does not poll in the toolbar header.

Similarly in python console

>>> bpy.ops.action.new.poll()
False

Closer inspection of the action editor UI code, will see that the template object is the dope sheet space st = context.space_data not the animation data of the context object. My guess is the operator uses space to poll.

Code wise will confess to never using this operator; to add a new action would instead

action = bpy.data.actions.new(name)

and assign it to the animation data of the object I'm animating.

Enough waffle. Recommend here creating an operator bpy.ops.foo.bar that makes the new action and assigns it where you wish, it appears from Q this is on the active object Call the following from your operators execute method.

def main(context):
    ob = context.object
    name = f"{ob.name}Action"
    action = bpy.data.actions.new(name)
    # choose whether to set as active action or whatever
    ob.animation_data_create()
    ob.animation_data.action = action

Have it poll if there is a context object

Then use it instead for new

layout.template_ID(ob.animation_data, "action", new="foo.bar")

Side Note.

The draw method is only meant for drawing to the UI. If you put an __init__ in a panel class will see they are initialized constantly.

Trying to alter objects in any way during a draw will result in an error. This applies to

bpy.context.object.animation_data_create()

in question code.

$\endgroup$

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.