Anim: implement "Copy Driver to Selected" operator #120936
No reviewers
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#120936
Loading…
Reference in New Issue
No description provided.
Delete Branch "nathanvegdahl/blender:copy_driver_to_selected_120518"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Resolves #120518
The implementation is based on the "Copy to Selected" operator, just adapted to copy drivers instead.
For reviewers:
Todo (from review comments):
See if we can give a good explanatory message in the tool tip when the operator is disabled.(It seems like this is more trouble than it's worth, given current limitations of the relevant APIs.)WIP: Anim: implement "Copy Driver to Selected" operatorto Anim: implement "Copy Driver to Selected" operator@blender-bot package
Package build started. Download here when ready.
@SimonThommes @Mets I've triggered a package build. When it's finished I would love some testing on this from you guys.
This seems to work well. There's one state that this operator can create that the Blender UI otherwise avoids, and that is having both animation and a driver on the same property. The UI does not handle this properly. Evaluation-wise the driver "wins", but when it comes to the background colour the FCurve "wins". This is not a good state to be in. Would be good to have a bit of a design discussion to figure out what should happen in this case.
@ -1567,0 +1587,4 @@
* otherwise. Returns true in poll mode if a copy could be successfully made,
* and false otherwise.
*/
static bool copy_driver_to_selected_button(bContext *C, bool poll)
const bool
@ -1567,0 +1621,4 @@
* side data. */
std::optional<std::string> path;
bool use_path_from_id;
blender::Vector<PointerRNA> lb;
Don't use a modern container, but retain the old
lb
(for "ListBase") name.@ -1567,0 +1628,4 @@
/* Copy the driver to the list of target properties. */
int copy_count = 0;
for (PointerRNA &link : lb) {
Same here, no
link
(because of old ListBase again), but name after what's in there.@ -1567,0 +1695,4 @@
static bool copy_driver_to_selected_button_poll(bContext *C)
{
return copy_driver_to_selected_button(C, true);
The poll function should use
CTX_wm_operator_poll_msg_set()
(orCTX_wm_operator_poll_msg_set_dynamic()
) to explain why the operator is disabled. I think this can be done relatively easily by makingcopy_driver_to_selected_button()
return astd::optional<std::string>
(or maybestd::optional<StringRef>
) that contains the explanation when nothing could be copied.So instead of
return false;
it would bereturn "You didn't fry an egg this morning";
, andreturn true;
becomesreturn {};
(or have someconst std::optional<StringRef> it_was_all_ok
to return a more explicit 'ok' thing).I gave this a shot, but the explanatory message in the popup always ended up being the string for when a valid property to copy from isn't found at all, which was... weird.
With a little debugging, it turns out that the poll function is being passed different things in the following two cases:
Copy Driver to Selected
menu item is enabled or not. In this case it gets passed the button for the property we were hovering over (e.g. the X location of the active object).Copy Driver to Selected
. As far as I can tell from tracing the execution, in this case it gets passed the button for theCopy Driver to Selected
menu item itself, which of course doesn't have a valid property associated with it.I'm not sure if this is a limitation of Blender's internal APIs (e.g. in case number 2 the user is, indeed, hovering over the
Copy Driver to Selected
menu item at that point), or if my code is doing something wrong (e.g. I'm registering the operator incorrectly, or adding it to the menu wrong, etc.).Then let's not go there (yet). If this is to be addressed, it can wait until another PR.
@ -1567,0 +1700,4 @@
static int copy_driver_to_selected_button_exec(bContext *C, wmOperator * /* op */)
{
if (copy_driver_to_selected_button(C, false)) {
Flip condition, return early.
@ -1567,0 +1703,4 @@
if (copy_driver_to_selected_button(C, false)) {
DEG_relations_tag_update(CTX_data_main(C));
/* TODO: ask someone in-the-know whether this call is needed or not.
Notifiers are necessary here, and given that Paste Driver also uses these, they're fine to use here too.
In the long term, we need to untangle the use of
ND_KEY...
for drivers, but that's for another day.@ -1567,0 +1723,4 @@
static void UI_OT_copy_driver_to_selected_button(wmOperatorType *ot)
{
/* identifiers */
Capitalisation & period.
@ -1567,0 +1727,4 @@
ot->name = "Copy Driver to Selected";
ot->idname = "UI_OT_copy_driver_to_selected_button";
ot->description =
"Copy the property's driver from the active item to the same property of all selected items "
Comma after "selected items"?
lgtm and super useful to have
I agree with optimizing the case of finding a driver FCurve later. Even though it is quadratic the amount of driver curves will likely stay quite low.
Also very nice to have that comment on top of
copy_driver_to_selected_button
.Awesome! Thank you for the implementation, it worked great in my tests!
I do think it would be useful to have a split into single/all for arrays. Especially for colors it can be tedious to open the color picker and do it per component. Right now this operator also doesn't show up in the context menu of a color property, since there is no
all
version. I think it should work the same way how the generalCopy to Selected
functionality works.Ah, that's a good point about color properties. I wanted to avoid cluttering the context menu even further (it's already really full of stuff), but maybe that's unavoidable.
In any case, that should be pretty easy to add.
A slight hiccup in implementing the "all" variant of the menu item: if we want to have just one operator with an "all" parameter (like
Copy to Selected
does), thepoll
function that determines if the menu item is enabled/disabled has to be the same for both menu items. This is because thepoll
function doesn't get access to operator parameters.For the
Copy to Selected
operator this doesn't matter, because it's just copying values, which always exist. But for theCopy Drivers to Selected
operator there may be drivers on only some of the elements of an array property. This leads to the odd situation where if the user right-clicks on one of the elements without a driver, there is a conflict between the two menu items: ideally the 'single' menu item is disabled and the 'all' menu item is enabled... but that's not possible.I discussed with @SimonThommes and we agreed that (probably) the least weird option is for both menu items to be disabled in that case, following whether there's a driver on the specific element you right-clicked on. So I'll implement it that way. If that turns out to be especially weird, it will be easy to swap.
@blender-bot package
Package build started. Download here when ready.
@SimonThommes @Mets I've triggered another package build, now with "Copy All" implemented for array properties. When it's done building and you get the chance, I would appreciate it if you could give it another spin.
Awesome, ready to be merged imo 👍
Buncha notes.
@ -761,3 +776,4 @@
ICON_DRIVER,
"ANIM_OT_driver_button_edit");
}
else {
This goes with the
if (!is_whole_array)
. Usually I try to avoid a negation + anelse
, so it might be better to just swap the two conditional blocks and useif (is_whole_array)
.@ -1567,0 +1572,4 @@
* \{ */
/**
* Gets the driver(s) of the given property.
AFAIK the preferred style is the imperative, so "Get the driver(s)..."
@ -1567,0 +1598,4 @@
const bool is_array_prop = RNA_property_array_check(prop);
if (r_is_array_prop != nullptr) {
*r_is_array_prop = is_array_prop;
This code being here means that
r_is_array_prop
is set even when the precondition checks below fail. I think it's better to push the assignment to*r_is_array_prop
further down, when actual data can be returned. This will reduce the number of "states" that can be returned (i.e. either "nothing is returned" or "useful info is returned"; the latter can still mean an empty vector of course).I would move the call to
RNA_property_array_check()
to just above whereis_array_prop
is used, and the assignment to*r_is_array_prop
to as low as makes sense in this function. This of course depends on how the caller has to deal with the*r_is_array_prop
value.@ -1567,0 +1613,4 @@
blender::Vector<FCurve *> drivers = {};
if (!is_array_prop) {
drivers.append(BKE_fcurve_find(&adt->drivers, path->c_str(), index));
Here I actuall agree with the negation +
else
order, as the first thing you read is the simplest one; this makes the whole easier to digest.@ -1567,0 +1617,4 @@
}
else {
/* For array properties, we always allocate space for all elements of an
* array property, and the unused ones just remain null. */
Good choice.
@ -1567,0 +1626,4 @@
}
}
/* If we didn't get any drivers to copy, early-out. */
I think here the "early out" isn't the most relevant. I think this would be clearer:
"If we didn't get any drivers to copy, instead of returning a vector of all nullptr, return an empty vector for clarity. That way the caller gets either a useful result or an empty one."
@ -1567,0 +1647,4 @@
*
* Note: intended to be used in conjunction with `get_property_drivers()` above.
*
* \param src_drivers The array of drivers to paste. If `is_array_prop`
since this is not an array in the C++ type sense of the word, I think it's clearer to just write "The span of drivers" instead.
@ -1567,0 +1653,4 @@
* \param is_array_prop Whether `src_drivers` are drivers for the elements
* of an array property.
* \param dst_ptr The RNA pointer for the destination property.
* \param dist_prop The destination property RNA.
Just for clarity, you may want to add "If src_drivers has more than one element, it is assumed that this is an array property of equal length of src_drivers" or something along those lines. Just to drive the point home that, if it is an array, it is assumed that these two match.
@ -1567,0 +1669,4 @@
/* Get the RNA path and relevant animdata for the property we're copying to. */
const std::optional<std::string> dst_path = RNA_path_from_ID_to_property(dst_ptr, dst_prop);
if (!dst_path.has_value()) {
return 0;
Do you think it makes sense to distinguish between "no drivers copied" and "could not actually get to the point where we can copy drivers"?
I definitely do think that makes sense, but for the time being I think this fine. Distinguishing those kinds of failure cases is exactly what I want something like we've discussed in the error handling devtalk thread for. Trying to propagate that information up the call chain would be a bit awkward and bespoke right now.
@ -1567,0 +1684,4 @@
}
const int dst_index = is_array_prop ? i : -1;
/* If it's already animated by something other than a driver, skip. */
Add a comment that explains why. Before I looked into this in more detail, I also didn't know that the UI is actually blocking the "both driver and animation" case.
@ -1567,0 +1701,4 @@
* drivers to find one that matches. We should be able to make this more
* efficient with a little cleverness .*/
{
FCurve *old_driver = BKE_fcurve_find(&dst_adt->drivers, dst_path->c_str(), dst_index);
You might be able to skip this when
driven
above isfalse
?Yeah, it looks like we can do that. One caveat, however, is that the way that
BKE_fcurve_find_by_rna()
works internally (which you have to dive a couple of function calls deep to find out) is thatdriven
only gets set to true if the property isn't also animated. In other words, if the property is both animated and driven,driven
remains false despite there being a driver (which takes precedence in evaluation!).But since we skip adding the driver if it's animated, that's fine in this case.
(Another odd thing about
BKE_fcurve_find_by_rna()
, and the only reason I created thedriven
variable initially, is that despite appearances ther_driven
parameter isn't null safe. All assignments to the otherr_*
parameters are guarded behind nullptr checks, but there's one place where it looks like someone forgot a guard forr_driven
. That should also be fixed, but not in this PR. I'll make another PR for that after this one lands.)@ -1567,0 +1709,4 @@
}
/* Create the new driver. */
FCurve *new_driver = BKE_fcurve_copy(src_drivers[i]);
This IMO should not be here, but in a
BKE_driver_copy()
function.These four lines are doing the following things:
I don't think the name
BKE_copy_driver()
appropriately captures that. And I don't think tying all three of those things together in a single function makes sense. Maybe just the first two, but then I don't think we're gaining much clarity or meaningfully reducing code duplication.However, I do think something like a "BKE_set_driver_rna_path()" could help the clarity here, since as-is we're doing raw memory freeing and string handling, which certainly makes the intent a bit unclear.
What do you think?
Sure, I didn't mean to suggest that this name was perfect, just wanted to say that such functionality would be better placed in another function instead of as part of this one.
I agree it would be nice to split up, something like:
And with all of that, I also agree that it's probably not worth the effort to write that first one, as it would only replace two lines that are fairly simple anway. It's indeed the freeing & string handling that's the most desirable to clean up.
@ -1567,0 +1732,4 @@
* bool to switch between exec and poll behavior. This isn't great, but seems
* like the lesser evil under the circumstances.
*
* \param all If true, copies drivers of all elements of an array property.
I think
copy_entire_array
ordo_entire_array
would be better thanall
@ -1567,0 +1806,4 @@
const int paste_count = paste_property_drivers(
src_drivers.as_span(), is_array_prop, &dst_ptr, dst_prop);
if (paste_count > 0) {
I'm wondering what I'd like more -- this approach, or:
Yeah, I like that better. Thanks!
@ -1567,0 +1842,4 @@
ot->description =
"Copy the property's driver from the active item to the same property of all selected "
"items, "
"if the same property exists";
This should be rewrapped.
LGTM!
@blender-bot build