Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #168: avoid creating duplicate link for addon #172

Merged
merged 49 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
e6f2f34
Fix #138: avoid creating duplicate link for addon
Mateusz-Grzelinski Jul 22, 2024
408442f
Rever capitalization of global vars
Mateusz-Grzelinski Jul 25, 2024
7c62eca
Support loading from externsion directory
Mateusz-Grzelinski Jul 25, 2024
6866abb
Add patch based tests
Mateusz-Grzelinski Jul 28, 2024
bf3d5a4
Fix tests and compability with python 3.7
Mateusz-Grzelinski Jul 28, 2024
99c7873
Fix addon reloading
Mateusz-Grzelinski Jul 29, 2024
869f0df
Fix type hint
Mateusz-Grzelinski Jul 29, 2024
74f9585
Fix logic for setting up addon links
Mateusz-Grzelinski Jul 29, 2024
3e9e7ee
Remove comment
Mateusz-Grzelinski Jul 29, 2024
b7d756d
Mock call to sys.path
Mateusz-Grzelinski Jul 30, 2024
f7b4f7e
Deduplicate patch calls
Mateusz-Grzelinski Jul 30, 2024
a0f99b6
Update comment
Mateusz-Grzelinski Jul 30, 2024
445778f
Extensions probably does not need sys.path
Mateusz-Grzelinski Jul 30, 2024
750fc41
Remove no longer needed gitignore
Mateusz-Grzelinski Jul 30, 2024
469435c
Fix missing directory
Mateusz-Grzelinski Jul 31, 2024
b803c97
Fix: prevent type hint from failing older verions of blender
Mateusz-Grzelinski Jul 31, 2024
c2e4330
Merge remote-tracking branch 'refs/remotes/jack/master' into master-n…
Mateusz-Grzelinski Aug 2, 2024
af8167a
Cover corner case with extension that can be an addon
Mateusz-Grzelinski Aug 2, 2024
21c4e78
Fix test and test new edge case
Mateusz-Grzelinski Aug 3, 2024
c6ab0fc
Remove link after blender exits
Mateusz-Grzelinski Aug 3, 2024
bbe0c9e
Fix: sys.path was not handled correctly for fresh blender install
Mateusz-Grzelinski Aug 3, 2024
702e4ea
Revert "Remove link after blender exits"
Mateusz-Grzelinski Aug 3, 2024
25b1454
Fix tests
Mateusz-Grzelinski Aug 3, 2024
ee612fe
Fix corner cases
Mateusz-Grzelinski Aug 8, 2024
3b29b68
Merge remote-tracking branch 'refs/remotes/jack/master' into master-c…
Mateusz-Grzelinski Aug 8, 2024
a95d6ee
Fix tests
Mateusz-Grzelinski Aug 9, 2024
50e1d01
Fix blender 2.8 compatibility
Mateusz-Grzelinski Aug 9, 2024
7377fe1
Update changelog and readme
Mateusz-Grzelinski Aug 11, 2024
e901876
Improve error message
Mateusz-Grzelinski Aug 11, 2024
5e5d0f9
Change ugly text rendering
Mateusz-Grzelinski Aug 11, 2024
1cc45ad
Fix blender 2.8 compability
Mateusz-Grzelinski Aug 11, 2024
e958c83
Update README
Mateusz-Grzelinski Aug 11, 2024
26972be
Update README
Mateusz-Grzelinski Aug 11, 2024
7a6c1e8
Update README
Mateusz-Grzelinski Aug 11, 2024
237de93
Update README
Mateusz-Grzelinski Aug 11, 2024
7c17ca3
Update README
Mateusz-Grzelinski Aug 12, 2024
fee5b4c
Resolve junction using windows cmd as fallback method of getting junc…
Mateusz-Grzelinski Aug 12, 2024
4e8601b
Fix tests and blender 2.8 compatibility
Mateusz-Grzelinski Aug 12, 2024
4f45939
Update readme
Mateusz-Grzelinski Aug 12, 2024
6f0dc8f
Merge branch 'master' into master
Mateusz-Grzelinski Aug 17, 2024
ddfc085
Merge remote-tracking branch 'refs/remotes/jack/master'
Mateusz-Grzelinski Aug 17, 2024
6c62c5e
Silence error on linux
Mateusz-Grzelinski Aug 17, 2024
ee18ca1
Update changelog
Mateusz-Grzelinski Aug 17, 2024
4e6bff7
Silence all errors when resolving link
Mateusz-Grzelinski Aug 17, 2024
38e6451
Update README
Mateusz-Grzelinski Aug 17, 2024
ef81feb
Use vscode URI for settings
Mateusz-Grzelinski Aug 14, 2024
3d8c665
Update README
Mateusz-Grzelinski Aug 17, 2024
426d8de
Update README
Mateusz-Grzelinski Aug 17, 2024
21bb909
Update README
Mateusz-Grzelinski Aug 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

## Unreleased

### Changed
### Added
- Support bl_order in auto_load.py (#118)
- Allow user to develop addon even it is placed in directories like (#172)
- `\4.2\scripts\addons` -> default dir for addons
- `\4.2\extensions\blender_org` -> directory indicated by `bpy.context.preferences.extensions.repos` (list of directories)
- Remove duplicate links to development (VSCode) directory (#172)
- Remove broken links in addon and extension dir (#172)

### Changed
- Addon_update operator: Check more precisely which module to delete (#175)
- Formatted all python code with `black -l 120` (#167)
- Fix most of the user reported permission denied errors by changing python packages directory ([#177](https://github.com/JacquesLucke/blender_vscode/pull/177)):
- Instead of installing to system python interpreter (`.\blender-4.2.0-windows-x64\4.2\python\Lib\site-packages` )
- Install to local blender modules `%appdata%\Blender Foundation\Blender\4.2\scripts\modules` (path indicated by `bpy.utils.user_resource("SCRIPTS", path="modules")`).
Expand All @@ -13,6 +23,7 @@

### Fixed
- Pinned requests to version 2.29 to maintain compatibility with blender 2.80 ([#177](https://github.com/JacquesLucke/blender_vscode/pull/177))
- Find correct python path for blender 2.92 and before (#174). This partly fixes compatibility with blender 2.80.
- "Blender: Run Script" will no longer open read-only file when hitting debug point (#142)

## [0.0.21] - 2024-07-16
Expand Down
39 changes: 39 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Contributing

Please make sure that your PR works with Blender 2.80:
- That requires you to use python 3.7 and
- Compatible Blender API (do a version check)
- Remember to update [CHANGELOG](./CHANGELOG.md): https://keepachangelog.com/en/1.0.0/
- Upcoming releases (roadmap) are planned in [milestones](https://github.com/JacquesLucke/blender_vscode/milestones)
- Generally don't commit commented out code unless there is a really good reason
- Prefer comments to be full sentences

# Structure

- Blender entrypoint is in `pythonFiles/launch.py`
- VS Code entrypoint is `src/extension.ts`. Refer to VS code docs, there is nothing non standard here.

# Python guideline

Use Black formatter with `black --line-length 120`

## Python tests

There is no clear guideline or commitment to testing.

Some tests are prototyped in `pythonFiles/tests/blender_vscode/test_load_addons.py`.
They should be run outside of Blender what makes them easy to execute, but prone to breaking: there is a lot of patching
for small number of test.

Run tests:

```powershell
pip install pytest
cd pythonFile
$env:PYTHONPATH="./include" # powershell
pytest -s .\tests
```

# Typescript guideline

Nothing more than `tslint.json`.
36 changes: 36 additions & 0 deletions EXTENSION-SUPPORT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Extension support

> With the introduction of Extensions in Blender 4.2, the old way of creating add-ons is considered deprecated.

[Extensions](https://docs.blender.org/manual/en/4.2/advanced/extensions/getting_started.html) are supported.
For general comparison visit [Legacy vs Extension Add-ons](https://docs.blender.org/manual/en/4.2/advanced/extensions/addons.html#legacy-vs-extension-add-ons).
VS code uses the automatic logic to determine if you are using addon or extension:

| Blender version | Addon has `blender_manifest.toml` | Development location: | Your addon is interpreted as: | Link is created? |
| --------------- | --------------------------------- | ---------------------- | ----------------------------- | -------------------------------------------------- |
| Pre 4.2 | Does not matter | ADDON_DEFAULT_LOCATION | Legacy addon | No link is created |
| Pre 4.2 | Does not matter | Anywhere on disk. | Legacy addon | You addon is linked to ADDON_DEFAULT_LOCATION |
| 4.2 and above | True | Anywhere on disk. | Extension | You addon is linked to EXTENSIONS_DEFAULT_LOCATION |
| 4.2 and above | True | ADDON_DEFAULT_LOCATION | Legacy addon | No link is created |
| 4.2 and above | True | ANY_REPO_LOCATION | Extension | No link is created |
| 4.2 and above | False | Does not apply. | Legacy addon | You addon is linked to ADDON_DEFAULT_LOCATION |

There is no setting to override this behaviour. Find out your default paths:
```python
>>> ADDON_DEFAULT_LOCATION = bpy.utils.user_resource("SCRIPTS", path="addons")
'/home/user/.config/blender/4.2/scripts/addons'
>>> ANY_REPO_LOCATION = [repo.custom_directory if repo.use_custom_directory else repo.directory for repo in bpy.context.preferences.extensions.repos if repo.enabled]
['/home/user/.config/blender/4.2/extensions/blender_org', '/home/user/.config/blender/4.2/extensions/user_default', '/snap/blender/5088/4.2/extensions/system']
>>> EXTENSIONS_DEFAULT_LOCATION = bpy.utils.user_resource("EXTENSIONS", path="user_default")
'/home/user/.config/blender/4.2/extensions/user_default'
```

## Examples

I am using Blender 3.0 to develop my addon in `/home/user/blender-projects/test_extension/`.
My addon supports addons and extensions (has both `bl_info` defined in `__init__.py` and `blender_manifest.toml` file.)
- Result: my addon is interpreted to be addon (because Blender 3 does not support extension). My addon is linked to ADDON_DEFAULT_LOCATION

I am using Blender 4.2 to develop my addon in `/home/user/blender-projects/test_extension/`.
My addon supports addons and extensions (has both `bl_info` defined in `__init__.py` and `blender_manifest.toml` file.)
- Result: my addon is interpreted to be extension. My addon is linked to EXTENSIONS_DEFAULT_LOCATION
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ The extension is [installed](https://code.visualstudio.com/docs/editor/extension

## Addon Tools

You can develop your addon anywhere, VS Code will create a **permanent soft link** (in windows: junction) to link you workspace:
- for addons to `bpy.utils.user_resource("SCRIPTS", path="addons")`
- for extensions to `bpy.utils.user_resource("EXTENSIONS", path="user_default")`

Warning:
- on windows uninstalling linked addon (junction) will result in data loss. If you are lucky you will get `PermissionError` when Blender tries to delete git history.
- on linux/mac from blender [2.80](https://projects.blender.org/blender/blender/commit/e6ba760ce8fda5cf2e18bf26dddeeabdb4021066) blender handles uninstalling links correctly. Addon will be unlinked and no data loss will occur.

### How do I create a new addon?

Execute the **Blender: New Addon** operator and use the setup wizard.
Expand All @@ -27,7 +35,8 @@ To do this, move the file into a new empty folder and rename it to `__init__.py`
To use the extension with your addon, just load the addon folder into Visual Studio Code.
Then execute the `Blender: Start` command.
This will ask you for a path to a Blender executable.
Only Blender 2.8 is supported.

Only Blender 2.8.34 onwards is supported.

After you choose a path, Blender will open.
The terminal output can be seen inside of VS Code.
Expand All @@ -37,14 +46,62 @@ For that it is important that you have an internet connection.
Once Blender is started, you can use the Addon in Blender.
Debugging should work now.

### Extension support

> With the introduction of Extensions in Blender 4.2, the old way of creating add-ons is considered deprecated.

[Extensions](https://docs.blender.org/manual/en/4.2/advanced/extensions/getting_started.html) are supported.
For migration guide visit [Legacy vs Extension Add-ons](https://docs.blender.org/manual/en/4.2/advanced/extensions/addons.html#legacy-vs-extension-add-ons).
VS code uses the [automatic logic to determine if you are using addon or extension](./EXTENSION-SUPPORT.md)

### How can I reload my addon in Blender?

Execute the `Blender: Reload Addons` command.
For that to work, Blender has to be started using the extension.
Your addon does not need to support reloading itself.
It only has to have correct `register` and `unregister` methods.

To reload the addon every time a file is saved, activate the `blender.addon.reloadOnSave` setting in VS Code.
To reload the addon every time a file is saved, activate the [`blender.addon.reloadOnSave`](vscode://settings/blender.addon.reloadOnSave) setting in VS Code.

### How can I open blender file automatically when running `Blender: Start`?

Add the path to .blend file to [`blender.additionalArguments`](vscode://settings/blender.additionalArguments):

```javascript
"blender.additionalArguments": [
"--factory-startup", // any arguments you want
// "--open-last", // Open the most recently opened blend file, or:
"./path/to/my-file.blend" // prefered to be last argument, watch out for trailing spaces (which are invisible in VS code UI)
],
```

### How can I separate development environment from my daily work?

By default, Blender started from VS Code uses your global Blender settings (in windows: `%appdata%\Blender Foundation\Blender\<version>`).

To prevent any accidental changes to your daily setup, change environment var in VS Code setting [`blender.environmentVariables`](vscode://settings/blender.environmentVariables):

```javascript
"blender.environmentVariables": {
"BLENDER_USER_RESOURCES": "./blender_vscode_development" // changes folder for addons, extensions, modules, config
},
```

See `blender --help` for more environment vars with finer controls:

```shell
Environment Variables:
$BLENDER_USER_RESOURCES Replace default directory of all user files.
Other 'BLENDER_USER_*' variables override when set.
$BLENDER_USER_CONFIG Directory for user configuration files.
$BLENDER_USER_SCRIPTS Directory for user scripts.
$BLENDER_USER_EXTENSIONS Directory for user extensions.
$BLENDER_USER_DATAFILES Directory for user data files (icons, translations, ..).
```

### How to use with multiple addons?

Use VS Code feature [Multi-root Workspaces](https://code.visualstudio.com/docs/editor/multi-root-workspaces). Each folder in workspace is treated as addon root.

## Script Tools

Expand Down Expand Up @@ -79,6 +136,20 @@ Currently the support for this is very basic, but still useful.
To run the script in a specific area type in Blender insert a comment like `#context.area: VIEW_3D`.
The preferred way to insert this comment is to execute the `Blender: Set Script Context` command.

### How can I pass command line argument to my script?

Specify your arguments in [`blender.additionalArguments`](vscode://settings/blender.additionalArguments) after `--`, which
indicates [End option processing, following arguments passed unchanged](https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html). Access via Python’s `sys.argv`

Be aware about:

- [sys.exit gotcha](https://docs.blender.org/api/current/info_gotcha.html#sys-exit)
- and [register_cli_command](https://docs.blender.org/api/current/bpy.utils.html#bpy.utils.register_cli_command)

## Core Blender development

This addon has some ability to help with [Blender source code development](https://developer.blender.org/docs/handbook/building_blender/) but it is undocumented.

## Troubleshooting

- Make sure you use the newest version of VS Code.
Expand All @@ -90,3 +161,7 @@ The preferred way to insert this comment is to execute the `Blender: Set Script
## Status

This extension is not actively developed anymore. However, if you are interested in working on this extension, please contact me.

## Contributing

See [DEVELOPMENT.md](./DEVELOPMENT.md)
1 change: 1 addition & 0 deletions pythonFiles/include/blender_vscode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from dataclasses import dataclass
from typing import List
from pathlib import Path
from typing import List

Expand Down
7 changes: 4 additions & 3 deletions pythonFiles/include/blender_vscode/communication.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from typing import Dict
import flask
import debugpy
import random
Expand Down Expand Up @@ -91,12 +92,12 @@ def handle_get():
return "OK"


def register_post_handler(type, handler):
def register_post_handler(type: str, handler):
assert type not in post_handlers
post_handlers[type] = handler


def register_post_action(type, handler):
def register_post_action(type: str, handler):
def request_handler_wrapper(data):
run_in_main_thread(partial(handler, data))
return "OK"
Expand All @@ -108,7 +109,7 @@ def request_handler_wrapper(data):
###############################


def send_connection_information(path_mappings):
def send_connection_information(path_mappings: Dict):
send_dict_as_json(
{
"type": "setup",
Expand Down
Loading