Skip to content

Commit

Permalink
Updated for Blender 2.80
Browse files Browse the repository at this point in the history
  • Loading branch information
AlansCodeLog committed Sep 28, 2019
1 parent 5841368 commit 045675a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 61 deletions.
64 changes: 32 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Blender Debugger for VS Code (and Visual Studio)

Inspired by [Blender-VScode-Debugger](https://github.com/Barbarbarbarian/Blender-VScode-Debugger) which was itself inspired by this [remote_debugger](https://github.com/sybrenstuvel/random-blender-addons/blob/master/remote_debugger.py) for pycharm as explained in this [Blender Developer's Blog post](https://code.blender.org/2015/10/debugging-python-code-with-pycharm/).

Inspired by [Blender-VScode-Debugger](https://github.com/Barbarbarbarian/Blender-VScode-Debugger) which was itself inspired by this [remote_debugger](https://github.com/sybrenstuvel/random-blender-addons/blob/master/remote_debugger.py) for pycharm as explained in this [Blender Developer's Blog post](https://code.blender.org/2015/10/debugging-python-code-with-pycharm/).

Since the VS Code one wasn't really well documented and it looked kind of dead, once I figured it out, I was just going to add the documentation, but then I ended up rewriting the whole thing.

Expand All @@ -17,12 +16,14 @@ Now it can:

I have made a video (click the image below) for those who just started messing with python in Blender or programming in general, but if you're semi-familiar with Python, VS Code, and the command line the following should make sense. If you have any questions or suggestions, don't hesitate to file an issue.

<strong>NOTE: ptvsd doesn't have to be version 3.0.0 anymore, so you can now ignore that and just `pip install ptvsd`</strong>
#### NOTES/EDITS
- ptvsd doesn't have to be version 3.0.0 anymore, so you can now ignore that and just `pip install ptvsd`
- This video was done with blender 2.79, but everything still works 99% the same. I think the only change is that blender changed the default shortcut to the search menu.

<p align="center" style="position:relative;">
<a href="https://www.youtube.com/watch?v=UVDf2VSmRvk" title="Click to go to Video">
<img alt="youtube video" src="https://img.youtube.com/vi/UVDf2VSmRvk/maxresdefault.jpg" height="300" style="margin:0 auto;" />
</a>
<a href="https://www.youtube.com/watch?v=UVDf2VSmRvk" title="Click to go to Video">
<img alt="youtube video" src="https://img.youtube.com/vi/UVDf2VSmRvk/maxresdefault.jpg" height="300" style="margin:0 auto;" />
</a>
</p>

## Note on Downloading
Expand All @@ -34,12 +35,12 @@ If you download the add-on from the releases page, please make sure to rename th
## Installing Python and Getting PTVSD

Install Python 3 with pip and check add to PATH.<sup id="n1">[1](#f1)</sup>
- If you already have python installed and you can run it from the command line (aka PATH is set), the addon should find it. It checks `where python` or `whereis python` or `which python` to try and determine where python is and uses the first path given<sup id="n2">[2](#f2)</sup>.
- If you are using something like Conda and want to use a virtual environment, to have the addon auto-detect the path you can: activate the environment, run Blender from the command line, and it should work.
- If you already have python installed and you can run it from the command line (aka PATH is set), the addon should find it. It checks `where python` or `whereis python` or `which python` to try and determine where python is and uses the first path given<sup id="n2">[2](#f2)</sup>.
- If you are using something like Conda and want to use a virtual environment, to have the addon auto-detect the path you can: activate the environment, run Blender from the command line, and it should work.

`pip install ptvsd`
- The following is no longer the case, the latest ptvsd version should work just fine now. <strike>Newer versions will not work, the add-on will warn you in the console if the version is above 3.0.0. Later versions aren't supported yet in VS Code, and it will throw an error when trying to connect. See [Debugging Python with VS Code](https://code.visualstudio.com/docs/python/debugging#_remote-debugging) and [#514](https://github.com/Microsoft/vscode-python/issues/514).</strike>
- For Visual Studio, later versions should work depending on the Visual Studio version. I have never used Visual Studio, but you can find more info on setting everything up here: [Remotely Debugging Python Code on Linux](https://docs.microsoft.com/en-us/visualstudio/python/debugging-python-code-on-remote-linux-machines#connection-troubleshooting). (it is not Linux specific)
- The following is no longer the case, the latest ptvsd version should work just fine now. <strike>Newer versions will not work, the add-on will warn you in the console if the version is above 3.0.0. Later versions aren't supported yet in VS Code, and it will throw an error when trying to connect. See [Debugging Python with VS Code](https://code.visualstudio.com/docs/python/debugging#_remote-debugging) and [#514](https://github.com/Microsoft/vscode-python/issues/514).</strike>
- For Visual Studio, later versions should work depending on the Visual Studio version. I have never used Visual Studio, but you can find more info on setting everything up here: [Remotely Debugging Python Code on Linux](https://docs.microsoft.com/en-us/visualstudio/python/debugging-python-code-on-remote-linux-machines#connection-troubleshooting). (it is not Linux specific)

## Setting up your Addon

Expand All @@ -50,14 +51,14 @@ In Blender go to: `User Preferences > File` and set the path to `Scripts` to the
```
Blender Stuff
└── addons
├── your-addon-folder
├── __init__.py
├── ...etc
├── another-addon
├── ...
├── your-addon-folder
├── __init__.py
├── ...etc
├── another-addon
├── ...
```

Now remove your addon from Blender if it was installed, save settings, and when you restart your addon should be installed automatically.
Now remove your addon from Blender if you had installed it manually already, save settings, and when you restart your addon should be installed automatically.

## Setting up this Addon

Expand All @@ -79,16 +80,16 @@ Install the Python extension for VS Code if you haven't already. For Visual Stud

In the lower left ([see #3 here](https://code.visualstudio.com/docs/python/python-tutorial#_prerequisites)), VS Code should have auto detected your Python install and set it as the interpreter. For Visual Studio see [Managing Python Environments](https://docs.microsoft.com/en-us/visualstudio/python/managing-python-environments-in-visual-studio).

Go to the Debugging tab and add a configuration. Pick Python. You'll want the configuration that looks like this, no need to change the defaults, you can delete the rest.
Go to the Debugging tab and add a configuration. Pick Python. You'll want the configuration that looks like this, no need to change the defaults, you can delete the rest.

```JSON
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"port": 5678, //careful, this used to be 3000 in older versions of vscode and this addon
"host": "localhost"
},
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"port": 5678, //careful, this used to be 3000 in older versions of vscode and this addon
"host": "localhost"
},
```

Now when you run the debugger with this config in Blender and VS Code the console should print "Debugger is Attached" if it was still waiting (it should still attach even if it wasn't, it just won't tell you).
Expand All @@ -108,25 +109,24 @@ From blender you can right click just about anything and click "Edit Source" to
Open the file in VS Code, connect to the debugging server, make a change and save it.

Now in Blender the text editor will show this little red button in the top left. Click that and reload the file. Then in `Text Editor > Properties` turn on `Live Edit` if you haven't already. Now to actually get Blender to detect any changes you made just type a single character (like add a space anywhere) and *then* it will detect your changes.



### Debugging/Editing Scripts

See [Issue #4](https://github.com/AlansCodeLog/blender-debugger-for-vscode/issues/4) for a workaround.
In the future if I have some time, I might see if there's something I can do to make this easier.
See [Issue #4](https://github.com/AlansCodeLog/blender-debugger-for-vscode/issues/4) for a workaround.
In the future if I have some time, I might see if there's something I can do to make this easier.

# Troubleshooting

- Check you installed the correct ptvsd version. With VS Code this should no longer be an issue, but I believe different versions of Visual Studio need different versions of ptvsd (see [Installing Python Support](https://docs.microsoft.com/en-us/visualstudio/python/installing-python-support-in-visual-studio)).
- To determine whether the problem is on Blender's side or your editor's: Close Blender and download/copy this [test script](https://gist.github.com/AlansCodeLog/ff1b246a8e31938e1c3dbfdcbb90522f) and run it with Python, and then try to connect to the server with your editor. If you're still getting problems then the problem is with VS Code, try:
- Check your detected your Python install, or set it manually.
- For VS Code try reinstalling the VS Code Python extension.
- Check your detected your Python install, or set it manually.
- For VS Code try reinstalling the VS Code Python extension.
- If you've been using this addon for a while and it's suddenly giving you a connection error, it might be because the default port has changed. VS Code's Python extension (vscode-python) has changed their default port from 3000 to 5678, so I have changed the default accordingly. I've made it configurable now though, so just check the port the addon is set to matches the one in your `launch.json` in VS Code.

Otherwise, if none of that helped, don't hesitate to file an issue.

# Notes

<a id="f1" href="#n1">1.</a> Technically, the add-on will work with Python 2 as well since it doesn't use Python itself, just the ptvsd package, so it doesn't really matter whether you installed it with Python 2 or 3 because the package is compatible with both. On the VS Code side though, the Python extension does need to know where Python is (though not ptvsd), but it will still connect if it's using Python 2, just IntelliSense recommendations the will be wrong.
<a id="f1" href="#n1">1.</a> Technically, the add-on will work with Python 2 as well since it doesn't use Python itself, just the ptvsd package, so it doesn't really matter whether you installed it with Python 2 or 3 because the package is compatible with both. On the VS Code side though, the Python extension does need to know where Python is (though not ptvsd), but it will still connect if it's using Python 2, but your IntelliSense recommendations will be wrong in VS Code.

<a id="f2" href="#n2">2.</a> The addon also detects python if PYTHONPATH is set (because Blender will add it to sys.path) or if you used the Python bundled with Blender to install ptvsd (but that's a bit of a pain because it doesn't have pip installed, you would have to install it manually).
<a id="f2" href="#n2">2.</a> The addon also detects python if PYTHONPATH is set (because Blender will add it to sys.path) or if you used the Python bundled with Blender to install ptvsd (but that's a bit of a pain because it doesn't have pip installed, you would have to install it manually).
59 changes: 30 additions & 29 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
bl_info = {
'name': 'Debugger for VS Code',
'author': 'Alan North',
'version': (0, 3, 0),
'blender': (2, 79, 0),
'version': (1, 0, 0),
'blender': (2, 80, 0),
"description": "Starts debugging server for VS Code.",
'location': 'In search (default shortcut:space) type "Debug"',
'location': 'In search (Edit > Operator Search) type "Debug"',
"warning": "",
"wiki_url": "https://github.com/AlansCodeLog/blender-debugger-for-vscode",
"tracker_url": "https://github.com/AlansCodeLog/blender-debugger-for-vscode/issues",
Expand Down Expand Up @@ -54,7 +54,7 @@ def check_for_ptvsd():
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
except Exception:
except Exception:
continue
if location is not None:
location = str(location.communicate()[0], "utf-8")
Expand All @@ -63,37 +63,37 @@ def check_for_ptvsd():
#extract path up to last slash
match = re.search(".*(/)", location)
if match is not None:
match = match.group()
match = match.group()
if os.path.exists(match+"lib/site-packages/ptvsd"):
match = match+"lib/site-packages"
return match

#check in path just in case PYTHONPATH happens to be set
for path in sys.path:
if os.path.exists(path+"\ptvsd"):
if os.path.exists(path+"/ptvsd"):
return path
if os.path.exists(path+"\site-packages\ptvsd"):
return path+"\site-packages"
if os.path.exists(path+"\lib\site-packages\ptvsd"):
return path+"lib\site-packages"
if os.path.exists(path+"/site-packages/ptvsd"):
return path+"/site-packages"
if os.path.exists(path+"/lib/site-packages/ptvsd"):
return path+"lib/site-packages"
return "PTVSD not Found"

# Preferences
class DebuggerPreferences(bpy.types.AddonPreferences):
bl_idname = __name__

path = bpy.props.StringProperty(
path: bpy.props.StringProperty(
name="Location of PTVSD",
subtype="DIR_PATH",
default=check_for_ptvsd()
)

timeout = bpy.props.IntProperty(
timeout: bpy.props.IntProperty(
name="Timeout",
default=20
)

port = bpy.props.IntProperty(
port: bpy.props.IntProperty(
name="Port",
min=0,
max=65535,
Expand Down Expand Up @@ -125,11 +125,11 @@ def check_done(i, modal_limit, prefs):
return {"PASS_THROUGH"}
print('Debugger is Attached')
return {"FINISHED"}

class DebuggerCheck(bpy.types.Operator):
bl_idname = "debug.check_for_debugger"
bl_label = "Debug: Check if VS Code is Attached"
bl_description = "Starts modal timer that checks if debugger attached until attached or until timeout."
bl_description = "Starts modal timer that checks if debugger attached until attached or until timeout"

_timer = None
count = 0
Expand All @@ -139,18 +139,18 @@ class DebuggerCheck(bpy.types.Operator):
def modal(self, context, event):
self.count = self.count + 1
if event.type == "TIMER":
prefs = bpy.context.user_preferences.addons[__name__].preferences
prefs = bpy.context.preferences.addons[__name__].preferences
return check_done(self.count, self.modal_limit, prefs)
return {"PASS_THROUGH"}
return {"PASS_THROUGH"}

def execute(self, context):
# set initial variables
self.count = 0
prefs = bpy.context.user_preferences.addons[__name__].preferences
prefs = bpy.context.preferences.addons[__name__].preferences
self.modal_limit = prefs.timeout*60

wm = context.window_manager
self._timer = wm.event_timer_add(0.1, context.window)
self._timer = wm.event_timer_add(0.1, window=context.window)
wm.modal_handler_add(self)
return {"RUNNING_MODAL"}

Expand All @@ -159,22 +159,22 @@ def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)

class DebugServerStart(bpy.types.Operator):
class DebugServerStart(bpy.types.Operator):
bl_idname = "debug.connect_debugger_vscode"
bl_label = "Debug: Start Debug Server for VS Code"
bl_description = "Starts ptvsd server for debugger to attach to."
bl_description = "Starts ptvsd server for debugger to attach to"

def execute(self, context):
#get ptvsd and import if exists
prefs = bpy.context.user_preferences.addons[__name__].preferences
prefs = bpy.context.preferences.addons[__name__].preferences
ptvsd_path = prefs.path
ptvsd_port = prefs.port

#actually check ptvsd is still available
if ptvsd_path == "PTVSD not Found":
self.report({"ERROR"}, "Couldn't detect ptvsd, please specify the path manually in the addon preferences or reload the addon if you installed ptvsd after enabling it.")
return {"CANCELLED"}

if not os.path.exists(os.path.abspath(ptvsd_path+"/ptvsd")):
self.report({"ERROR"}, "Can't find ptvsd at: %r/ptvsd." % ptvsd_path)
return {"CANCELLED"}
Expand All @@ -194,12 +194,13 @@ def execute(self, context):
# call our confirmation listener
bpy.ops.debug.check_for_debugger()
return {"FINISHED"}

def register():
bpy.utils.register_module(__name__)

def unregister():
bpy.utils.unregister_module(__name__)
classes = (
DebuggerPreferences,
DebuggerCheck,
DebugServerStart,
)
register, unregister = bpy.utils.register_classes_factory(classes)

if __name__ == "__main__":
register()

0 comments on commit 045675a

Please sign in to comment.