2

System:

  • Ubuntu 18.04
  • Pycharm 2019.2 professional edition
  • Python 3.6.8

I have followed this link to create a pycharm remote debugger but I have encountered an error related to remote_debugger.py

Blender add-on configuration:

enter image description here

Pycharm state:

enter image description here

Blender error:

enter image description here

It's nearly impossible to write a complex program in Blender without debugging options. Can you please help me solve this problem? I also think this should be an important feature request for the next release - an easy way to debug the code with 3rd party program.

Thank you all in advance.

AvivSham
  • 185
  • 2
  • 13
  • 1
    The remote_debugger.py was not written for Blender 2.80 which is why you're seeing the error. So it's either porting the script to 2.80 (and crossing fingers that the rest of the tutorial remained valid for the current version of PyCharms and Blender) or you could use VSCode and Jaques Lucke's extension for it (https://github.com/JacquesLucke/blender_vscode) – Robert Gützkow Sep 17 '19 at 18:52
  • 1
    The original add-on has been updated: https://github.com/sybrenstuvel/random-blender-addons/blob/master/remote_debugger.py – Robert Gützkow Sep 19 '19 at 09:19

1 Answers1

4

Below you find the updated remote_debugger.py which runs in Blender 2.80.

  1. Install the add-on
  2. Set the paths to the pydevd.py and pydevd-pycharm.egg (don't forget to save the preferences)
  3. Configure and start the remote debug server in PyCharm
  4. Search for the Connect to remote PyCharm debugger in the operator search using F3 or Edit > Operator Search

This works only with PyCharm Professional, the Community Edition is missing the remote debug feature.


"""
Remote debugging support.
This addon allows you to use a remote Python debugger with PyCharm, PyDev and
possibly other IDEs. As it is, without modification, it only supports PyCharm,
but it may work by pointing it at a similar egg file shipped with PyDev.
Before using, point the addon to your pydevd-pycharm.egg file in the
addon preferences screen.
For more information on how to use this addon, please read my article at
http://code.blender.org/2015/10/debugging-python-code-with-pycharm/
"""

bl_info = {
    'name': 'Remote debugger',
    'author': 'Sybren A. Stüvel and Robert Gützkow',
    'version': (0, 4),
    'blender': (2, 80, 0),
    'location': 'Press F3, search for "debugger"',
    'category': 'Development',
}

import bpy
import os.path
from bpy.types import AddonPreferences
from bpy.props import StringProperty


class DebuggerAddonPreferences(AddonPreferences):
    # this must match the addon name, use '__package__'
    # when defining this in a submodule of a python package.
    bl_idname = __name__

    eggpath: StringProperty(
        name='Path of the PyCharm egg file',
        description='Make sure you select the py3k egg',
        subtype='FILE_PATH',
        default='pydevd-pycharm.egg'
    )

    pydevpath: StringProperty(
        name='Path of the PyDev pydevd.py file',
        subtype='FILE_PATH',
        default='pydevd.py'
    )

    def draw(self, context):
        layout = self.layout
        layout.prop(self, 'pydevpath')
        layout.prop(self, 'eggpath')
        layout.label(text='Make sure you select the egg for Python 3.x: pycharm-debug-py3k.egg ')


class DEBUG_OT_connect_debugger_pycharm(bpy.types.Operator):
    bl_idname = 'debug.connect_debugger_pycharm'
    bl_label = 'Connect to remote PyCharm debugger'
    bl_description = 'Connects to a PyCharm debugger on localhost:1090'

    def execute(self, context):
        import sys

        user_preferences = context.preferences
        addon_prefs = user_preferences.addons[__name__].preferences

        eggpath = os.path.abspath(addon_prefs.eggpath)

        if not os.path.exists(eggpath):
            self.report({'ERROR'}, 'Unable to find debug egg at %r. Configure the addon properties '
                                   'in the User Preferences menu.' % eggpath)
            return {'CANCELLED'}

        if not any('pycharm-debug' in p for p in sys.path):
            sys.path.append(eggpath)

        import pydevd_pycharm
        pydevd_pycharm.settrace('localhost', port=1090, stdoutToServer=True, stderrToServer=True,
                        suspend=False)

        return {'FINISHED'}


class DEBUG_OT_connect_debugger_pydev(bpy.types.Operator):
    bl_idname = 'debug.connect_debugger_pydev'
    bl_label = 'Connect to remote PyDev debugger'
    bl_description = 'Connects to a PyDev debugger on localhost:5678'

    def execute(self, context):
        import sys

        user_preferences = context.preferences
        addon_prefs = user_preferences.addons[__name__].preferences

        pydevpath = os.path.abspath(addon_prefs.pydevpath)

        if not os.path.exists(pydevpath):
            self.report({'ERROR'}, 'Unable to find pydevd.py at %r. Configure the addon properties '
                                   'in the User Preferences menu.' % pydevpath)
            return {'CANCELLED'}

        dirname = os.path.dirname(pydevpath)
        basename = os.path.basename(dirname)
        if not any(basename in p for p in sys.path):
            sys.path.append(dirname)

        import pydevd
        pydevd.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True,
                        suspend=False)

        return {'FINISHED'}


def register():
    bpy.utils.register_class(DEBUG_OT_connect_debugger_pycharm)
    bpy.utils.register_class(DEBUG_OT_connect_debugger_pydev)
    bpy.utils.register_class(DebuggerAddonPreferences)


def unregister():
    bpy.utils.unregister_class(DEBUG_OT_connect_debugger_pycharm)
    bpy.utils.unregister_class(DEBUG_OT_connect_debugger_pydev)
    bpy.utils.unregister_class(DebuggerAddonPreferences)


if __name__ == '__main__':
    register()
Robert Gützkow
  • 25,622
  • 3
  • 47
  • 78
  • If you have the time, please open up a pull request: https://github.com/sybrenstuvel/random-blender-addons/pulls Otherwise others will do the same again or this nice snipped (your work) is lost in question land. – brockmann Sep 17 '19 at 20:04
  • @brockmann Sure I will. Not today though, likely tomorrow. – Robert Gützkow Sep 17 '19 at 20:04
  • @brockmann Seems like others were a bit faster. There are already two pull requests. – Robert Gützkow Sep 17 '19 at 20:09
  • Huh, your code is way nicer for sure, go for it @rjg :D – brockmann Sep 17 '19 at 20:14
  • @rjg thank you for the code! I managed to establish the connection between pycharm and blender but still got a problem. In the guide link from my first message, it said that the debugger should stop on pydevd.settrace(…) call but it doesn't. Can you please guide me on how can I debug my project and see the outcome on blender via pycharm? (The connection is established) – AvivSham Sep 18 '19 at 10:33
  • @AvivSham yes the tutorial is a bit incomplete there. You have to add a debug / break point in PyCharm otherwise it won't stop on that line. You should be able to debug add-ons, by opening the files from Blender's add-on directory and setting debug point for them as well. I haven't tested this extensively though, just confirmed that PyCharm actually halts on the break points. – Robert Gützkow Sep 18 '19 at 10:47
  • @AvivSham If this answer solved your problem, please mark it as accepted using the checkmark next to it. – Robert Gützkow Sep 18 '19 at 10:50
  • It's not solved yet. Even when I place a breakpoint inside the DEBUG_OT_connect_debugger_pycharm the debugger doesn't stop there even if I put the breakpoint on the preferences class. – AvivSham Sep 18 '19 at 12:09
  • @AvivSham I've tested it yesterday. Let me check this evening and I'll update my answer if something is missing. – Robert Gützkow Sep 18 '19 at 12:36
  • @rjg I feel like the problem is with my setup but i followed the steps defined the paths: /home/aviv/Pycharm/pycharm-2019.2.2/helpers/pydevd.py for the pydevd.py file and /home/aviv/Pycharm/pycharm-2019.2.2/debug-eggs/pydevd-pycharm.egg for Pycharms egg. Set the server to localhost 1090. where the problem can be? I tried all sorts of things. – AvivSham Sep 18 '19 at 12:46
  • @AvivSham I've looked at it right away. It works as intended. 1.) Open PyCharm, and create the debug server configuration (Run > Edit Configuration). Make sure that you're using port 1090. 2.) Start the debugging by clicking on the bug icon. 3.) In Blender connect to the debugger by searching for Connect to remote PyCharm debugger using F3 4.) Chose any script that Blender uses, open it in PyCharm and add a breakpoint. For example the .obj exporter in 2.80\scripts\addons\io_scene_obj\export_obj.py. Then export an .obj object. The breakpoint should be triggered. – Robert Gützkow Sep 18 '19 at 13:03
  • Still after following your instructions, it didn't stop. After I started the debugger I saw the following line in the debugger's console: use the following code to connect to the debugger: import pydevd_pycharm and in line below it pydevd.settrace(...) so I have changed the code over remote_debugger.py but still it didn't help. Which pycharm version do you use? – AvivSham Sep 18 '19 at 14:05
  • @AvivSham It's PyCharm 2019.2.2. The output on the console should be Starting debug server at port 1090 Use the following code to connect to the debugger: Waiting for process connection... import pydevd_pycharm pydevd_pycharm.settrace('localhost', port=1090, stdoutToServer=True, stderrToServer=True) and once you've connected from Blender it should also print Connected to pydev debugger (build 192.6603.34) – Robert Gützkow Sep 18 '19 at 14:10
  • @rjg this is exactly what I see! but even if I locate a breakpoint inside the remote_debugger file pycharm debugger won't stop there. The problem is that afterward nothing happen blender isn't freeze but pycharm kind of freeze since the debugger is hanging. can we please contact somehow? or its forbidden to post emails here? – AvivSham Sep 18 '19 at 14:22
  • @AvivSham don't add the breakpoint in the remote debugger file or do it after the pydevd_pycharm.settrace, e.g. on the return {'FINISHED'}. Before that line the debugger isn't connected and therefore it won't stop on the breakpoint. – Robert Gützkow Sep 18 '19 at 14:25
  • I have done that. still it didn't stop. What else can I do? – AvivSham Sep 18 '19 at 14:30
  • @AvivSham hard to say with the current information. Looks like you have to figure this out on you own. It would be nice if you can give us an update in case you've found the cause of the issue. – Robert Gützkow Sep 18 '19 at 21:11
  • I'm really frustrated! when I run the code from blender I can see the errors in my python console (in Pycharm) but it looks like it didn't recognize the breakpoints. Any ideas in that case? – AvivSham Sep 19 '19 at 06:56