semagnum / light-painter

Blender add-on that creates lights based on where the user paints.
https://semagnum.github.io/light-painter/
GNU General Public License v3.0
310 stars 6 forks source link

Operator properties not persisting within tools #46

Closed chenpaner closed 10 months ago

chenpaner commented 11 months ago

Hi, there is a small suggestion, you can consider it, every time I add a light is the default point light, although I can modify lamp_type default = 'AREA', but it seems not convenient enough, maybe lamp_type: bpy.props. EnumProperty can be changed to a scene property, so that when adding the second light is still the default light type after the first modification

semagnum commented 11 months ago

The reason Light Painter doesn't have this is it's one of those weird Blender "quirks" ie design choices. In short, when a tool calls a modal operator via shortcut (in this case, a mouse click), it directly uses the keymap's parameter defaults, not the values from the previous run. That's why the tools don't remember previous settings, even though operators in other contexts do.

A couple fixes:

  1. Every time the operators run, force-save the current values to the keymap. This will feel like a "normal" operator usage. There may be a select few artists that don't like these settings overwritten constantly. For them, I can have a toggle in the Light Painter preferences to disable this behavior (but have it on by default for the rest of us). And we'd probably want it to reset when Blender restarts.
  2. Do what you suggest: simply exposing these properties on the toolshelf UI, so artists can change it prior to running the operator.
  3. For those that want a parameter to be the same regardless of the previous run, go to their keymap and change the parameter themselves. Just a workaround without any changes on my part.

This is the code for the first fix (I'm just documenting this for myself and posterity), found on Stack Exchange:

wm = context.window_manager
keyconfigs = wm.keyconfigs
kmi = keyconfigs.find_item_from_operator(self.bl_idname)[1]
for attr in self.properties.keys():
    setattr(kmi.properties, attr, getattr(self, attr))
chenpaner commented 11 months ago

Thank you very much for your response. I'm sorry that I don't understand programming languages; I've just started learning a bit of Python. The suggestion to modify the lamp_type property came from seeing a similar property in another plugin that can retain the previous selection instead of resetting to the default option every time. Here is the link to that plugin; you can take a look. I'm not sure if it will be helpful to you, but your plugin is already fantastic!

Link

image

semagnum commented 11 months ago

Thank you for sharing the link! And I appreciate your words on Light Painter, it really means a lot :)

The nice thing with Blender is "retain the previous options" is a given feature for operators. I don't even have to code it, and neither did the add-on you linked. But for some reason, Blender doesn't reuse the last parameters when run by tools (ie those left-hand buttons in the 3D view, including Light Painter). It just uses to the default values in the keymap, no matter what. I'm trying to figure out why Blender coded it that way. Because there may be a bigger (but better) solution that naturally fits with Blender than just doing a hacky workaround.

Other tools' settings are in the separate tools header and panel, not the redo panel. So it may be that Blender devs would rather I put all the tool properties up there and expose the properties like you said.

I'm not asking you to have the answers. I'm just sharing my thoughts for you and others to see. I'll take some time to think about it (I'm on US holiday anyway), but any feedback from you and others is appreciated.

semagnum commented 10 months ago

Quick update here: after giving it more thought, I have some ideas planned for the next version upgrade to help resolve this, including adding the light type in the toolshelf header. But it'll take advantage of keyboard shortcuts and have a (real) live updates.

So I'll mark this issue as "In progress" :)

semagnum commented 10 months ago

I added the lamp type for the Light Paint tool shelf. I added some for other tools as well. The redo panel is still available and working.

If there's any other parameters that you feel are worth exposing, you can open a new issue. Closing this one.