Skip to main content
added 1511 characters in body
Source Link
batFINGER
  • 85.2k
  • 10
  • 114
  • 244

Thanks, I can use your answer later, but you missed the exact point of my question. I do not know that the user will delete specifically the X Location channel. Instead, I want to present a list of channels from all selected objects to the user, so that he will select, for example, "I want to delete the Z Location from all selected objects". However, I could not find a way to convert location[2] to "Z Location".

Re-read the question appears to be a pretty good attempt at an answer to part i. Generally the idea is to have one question per question , re part 2

Can get the name of the property via the "RNA" eg if an object has an fcurve with datapath "location" or "rotation_euler"

>>> C.object.bl_rna.properties['location'].name
'Location'

>>> C.object.bl_rna.properties['rotation_euler'].name
'Euler Rotation'

>>> C.object.bl_rna.properties['rotation_euler'].subtype
'EULER'

>>> C.object.bl_rna.properties['location'].subtype
'TRANSLATION'

As for the axis name from index, have asked this question Python RNA getting the names of Vector axes. (eg "g" is color[1])

>>> index = 0
>>> "XYZ"[index]
'X'

and as for your third question, with espanol selected as language.

>>> bpy.app.translations.pgettext("Location")
'Posición'

consult the docs. https://docs.blender.org/api/current/bpy.app.translations.html#module-bpy.app.translations

Thanks, I can use your answer later, but you missed the exact point of my question. I do not know that the user will delete specifically the X Location channel. Instead, I want to present a list of channels from all selected objects to the user, so that he will select, for example, "I want to delete the Z Location from all selected objects". However, I could not find a way to convert location[2] to "Z Location".

Re-read the question appears to be a pretty good attempt at an answer to part i. Generally the idea is to have one question per question , re part 2

Can get the name of the property via the "RNA" eg if an object has an fcurve with datapath "location" or "rotation_euler"

>>> C.object.bl_rna.properties['location'].name
'Location'

>>> C.object.bl_rna.properties['rotation_euler'].name
'Euler Rotation'

>>> C.object.bl_rna.properties['rotation_euler'].subtype
'EULER'

>>> C.object.bl_rna.properties['location'].subtype
'TRANSLATION'

As for the axis name from index, have asked this question Python RNA getting the names of Vector axes. (eg "g" is color[1])

>>> index = 0
>>> "XYZ"[index]
'X'

and as for your third question, with espanol selected as language.

>>> bpy.app.translations.pgettext("Location")
'Posición'

consult the docs. https://docs.blender.org/api/current/bpy.app.translations.html#module-bpy.app.translations

Source Link
batFINGER
  • 85.2k
  • 10
  • 114
  • 244

Find and remove

The fcurves collection of an action has a method to find fcurves from data path and index, and a remove method.

The same action can be linked to multiple objects, so will use a set to find all the actions' fcurves.

There really is no need to look at the group, location x keyframes have data path 'location' and array_index 0.

Test code. Since you mention you are going to convert this to an operator have added the context variable context = bpy.context Once test code works, lose this and use the context argument passed to operator/panel methods, when pasting into operator code.

import bpy

context = bpy.context

data_path = "location"
index = 0 # location.x or location[0]

#objects = bpy.data.objects # all
#objects = context.scene.objects # all in scene
objects = context.selected_objects # selected

all_fcurves = set(
    o.animation_data.action.fcurves for o in objects
    if o.animation_data and o.animation_data.action
    )

for fcurves in all_fcurves:
    fcurve = fcurves.find(data_path, index=index)
    if fcurve:
        fcurves.remove(fcurve)

for bones the data path is akin to

'pose.bones["Bone"].location`

in which case to find x location keyframe it could be simpler to replace the find method with

def find(fcurves, data_path, index):
    x = [fc for fc in fcurves 
         if fc.data_path.endswith(datapath) 
         and fc.array_index == index]
    return x.pop() if x else None