0
$\begingroup$

I was looking into this: https://github.com/CenekStrichel/CendaTools/wiki/Render-Tools And there´s 1 key assignment to do the render region.

Basically you have: step 1: If user presses "(key)", invoke view3d.render_border command step 2: view3d.toggle_renderer step 3: Render area gets represented. step 4: IF a render area exist, and user presses "(key)" THEN view3d.clear_render_border

How could an .addon be created bindig all those commands and IF statement to a single keystroke?

Please help. Thanks.

Could this code get us started?

import bpy

class QuickRenderRegion(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "view3d.toggle_render"
    bl_label = "QuickRenderRegion"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def execute(self, context):
        bpy.ops.view3d.render_border()
        bpy.ops.view3d.toggle_render    
        #return {'FINISHED'}


def register():
    bpy.utils.register_class(QuickRenderRegion)


def unregister():
    bpy.utils.unregister_class(QuickRenderRegion)


if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ Modal operator. Have a look into Templates > Python > Operator Modal. However that's cumbersome in this case. Out of curiosity: why you can't just use Ctrl+B? $\endgroup$ – p2or Jun 18 '18 at 9:48
  • 1
    $\begingroup$ ctrl+b, shift+z, wait for render, shift+z, ctrl+alt+b <---- Outrageous combo! Ultra finish. (I think you get me). Keybinding it to 1 key: genius. $\endgroup$ – Pierre Schiller Jun 18 '18 at 17:17
  • $\begingroup$ Simple example on how to bind an operaor to any shortcut. I think you get the idea, copy the operator and just replace the name of the class when calling keymap_items.new() and you are literally done. But notice that the operator you've linked to, actually needs a cleanup before using it in a production: there are bad references, bpy is imported multiple times etc... $\endgroup$ – p2or Jun 19 '18 at 13:48
  • $\begingroup$ Actually it would be necessary to anchor 3 commands to the same keybind: 1-that starts the drawing rectangle area, 2 shifts to render, 3 if the same key is pressed again and there is a render preview area, then clear the area first and go into rectangle draw area again unless "ESC" is pressed and session will cancel (toggle back to regular selection mode). $\endgroup$ – Pierre Schiller Jun 21 '18 at 16:00
  • $\begingroup$ @PierreSchiller you should make your solution into an answer to close this question. I don't think there is any better solution. $\endgroup$ – HenrikD Mar 5 at 16:35
5
+50
$\begingroup$

I took p2or link and modified a little. After running the script, shortcut key is Shift + Q. def register() part has type = "Q" and shift = True alt = False and ctrl = False.

bl_info = {
    "name": "Toggle Simplify",
    "description": "Toggle Simplify",
    "author": "poor",
    "version": (0, 0, 2),
    "blender": (2, 79, 6),
    "location": "3D View",
    "category": "3D View"
}

import bpy


# operator
class ToggleSimplify(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "view3d.toggle_simplify"
    bl_label = "Toggle Simplify"
    bl_options = {'REGISTER'}

    border = bpy.props.BoolProperty(default=False)

    def execute(self, context):

        if bpy.context.area.spaces.active.use_render_border:
            bpy.context.area.spaces.active.use_render_border = False
            self.border = False
            bpy.ops.view3d.toggle_render()
        else:
            bpy.ops.view3d.render_border("INVOKE_DEFAULT")

        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}

    def modal(self, context, event):

        if not self.border and context.area.spaces.active.use_render_border:
            bpy.ops.view3d.toggle_render()
            self.border = True
            return {'FINISHED'}
        return {'PASS_THROUGH'}


addon_keymaps = []


# register
def register():
    bpy.utils.register_class(ToggleSimplify)

    # handle the keymap
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
        kmi = km.keymap_items.new(ToggleSimplify.bl_idname, type='Q', value='PRESS', shift=True, alt=False, ctrl=False)
        addon_keymaps.append((km, kmi))


# unregister
def unregister():
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()

    bpy.utils.unregister_class(ToggleSimplify)


if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ Hi. I´ll test this code. Question: What if I do SHIFT+Q for the shortcut instead of "Q" on edit mode, or object mode? Is the interaction mode going to affect the script? Say I am in wireframe mode, will it toggle to full render and back to wireframe? Also: How do I clear the region using the same Shotcut key but just "tap" to clear render region? Could this line change to: "ToggleSimplify.bl_idname, type='SHIFT Q', value='PRESS', shift=True" ? $\endgroup$ – Pierre Schiller Mar 5 at 19:44
  • $\begingroup$ I installed your .py addon, but when I press "Q", nothing happens. $\endgroup$ – Pierre Schiller Mar 5 at 19:58
  • $\begingroup$ Shift+Q is already the shortcut key combination for that script. $\endgroup$ – JuhaW Mar 5 at 20:50
  • $\begingroup$ Right now it´s triggering the render toggle first, then it launches area select mode. It should be area select mode first, end selecting area and when defined, then it should launch the toggle render function. Otherwise it beats the purpose of having a render preview. Could that be adjusted? $\endgroup$ – Pierre Schiller Mar 5 at 21:08
  • $\begingroup$ I updated the script. $\endgroup$ – JuhaW Mar 6 at 9:53

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.