Skip to content

Commit a2d253c

Browse files
committed
1.1.0 Update
This update requiere VS Code 1.78.0 or more. - Add support for Cinema 4D 2024.0. - Auto detect which Python version to use. - Added an option to disable the C4D console auto-focus. - Fix incorrect posix path usage.
1 parent 30c6454 commit a2d253c

5 files changed

Lines changed: 109 additions & 31 deletions

File tree

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"icon": "image/maxon_logo.png",
66
"displayName": "Cinema 4D Connector",
77
"description": "Provides helper tool to developer Cinema 4D scripts and plugins.",
8-
"version": "1.0.2",
8+
"version": "1.1.0",
99
"repository": {
1010
"type": "git",
1111
"url": "https://github.com/PluginCafe/Cinema4D_Connector-VisualStudioCode_Extension"
@@ -14,7 +14,7 @@
1414
"url": "https://github.com/PluginCafe/Cinema4D_Connector-VisualStudioCode_Extension/issues"
1515
},
1616
"engines": {
17-
"vscode": "^1.62.0"
17+
"vscode": "^1.78.0"
1818
},
1919
"categories": [
2020
"Other"
@@ -122,6 +122,13 @@
122122
"type": "string",
123123
"order": 3,
124124
"description": "Path to the directory containing the python scripts used as template."
125+
},
126+
"c4d.bringconsoleinfront": {
127+
"scope": "application",
128+
"type": "boolean",
129+
"default": true,
130+
"order": 4,
131+
"description": "If checked, bring in front the Cinema 4D console each time a new statement appears."
125132
}
126133
}
127134
}

src/commands.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import * as os from 'os';
33
import * as fs from 'fs';
44
import * as path from 'path';
55
import { client, SET_SCRIPT_CONTENT, fixPath } from './async_client';
6-
import { c4dFs } from './file_system';
7-
import { GetAndStoreTemplateDir } from './settings';
6+
import { c4dFs, getPythonFolder, getPythonExecutablePath } from './file_system';
7+
import { GetAndStoreTemplateDir, C4D_BRING_IN_FRONT_CONFIG_ID } from './settings';
88
import { errShowErrorMessage } from './errors';
99

1010
import assert = require('assert');
@@ -74,7 +74,10 @@ export function addToC4DConsoleCmd(data: string)
7474
{ throw new Error("Failed to retrieve Cinema 4D Output channel"); }
7575

7676
c4dOutputChannel.append(data);
77-
c4dOutputChannel.show();
77+
78+
let bringInFront = vscode.workspace.getConfiguration().get<boolean>(C4D_BRING_IN_FRONT_CONFIG_ID);
79+
if (bringInFront)
80+
{c4dOutputChannel.show();}
7881
}
7982
catch (err: any)
8083
{
@@ -120,25 +123,12 @@ export async function DebugScriptContentInScriptManager()
120123
if (c4dPath === undefined)
121124
{ throw new Error("Unable to retrieve Cinema 4D path"); }
122125

123-
let pythonPath = os.platform() === 'win32'?
124-
path.join(c4dPath, "resource", "modules", "python", "libs", "python39.win64.framework", "python.exe")
125-
:
126-
path.join(c4dPath, "resource", "modules", "python", "libs", "python39.macos.framework", "python", "Contents", "MacOS", "python")
127-
;
128-
129-
if (!fs.existsSync(pythonPath))
130-
{ pythonPath = os.platform() === 'win32'?
131-
path.join(c4dPath, "resource", "modules", "python", "libs", "python310.win64.framework", "python.exe")
132-
:
133-
path.join(c4dPath, "resource", "modules", "python", "libs", "python310.macos.framework", "python", "Contents", "MacOS", "python")
134-
;}
126+
let pythonPath = getPythonExecutablePath(c4dPath);
135127

136128
if (!fs.existsSync(pythonPath))
137129
{ throw new Error("Incorrect path for the c4d python executable, debugger will not work."); }
138130

139-
let debugAdapterPath = path.join(c4dPath!, "resource", "modules", "python", "libs", "python39", "debugpy", "adapter");
140-
if (!fs.existsSync(debugAdapterPath))
141-
{ debugAdapterPath = path.join(c4dPath!, "resource", "modules", "python", "libs", "python310", "debugpy", "adapter"); }
131+
let debugAdapterPath = path.posix.join(getPythonFolder(c4dPath), "debugpy", "adapter");
142132

143133
if (!fs.existsSync(pythonPath))
144134
{ throw new Error("Incorrect path for the debugpy, debugger will not work."); }
@@ -184,7 +174,7 @@ export async function LoadTemplateScript()
184174

185175
fs.readdirSync(templatePath).forEach(fileName =>
186176
{
187-
let file = path.join(templatePath, fileName);
177+
let file = path.posix.join(templatePath, fileName);
188178
if (fileName.endsWith(".py"))
189179
{
190180
fileNames.push(new FileItem(file));

src/file_system.ts

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as path from 'path';
22
import * as vscode from 'vscode';
3+
import * as os from 'os';
4+
import * as fs from 'fs';
35
import { client } from './async_client';
46
import { errShowErrorMessage } from './errors';
57

@@ -282,4 +284,84 @@ export class C4DFS implements vscode.FileSystemProvider
282284
}
283285
}
284286

285-
export const c4dFs = new C4DFS();
287+
export const c4dFs = new C4DFS();
288+
289+
export function getPythonFolder(c4dPath: string)
290+
{
291+
let pyVersionToUse = 39;
292+
293+
let pyLibs = path.posix.join(c4dPath, "resource", "modules", "python", "libs").replace(/\\/g, "/");
294+
const readDirPyLibs = fs.readdirSync(pyLibs);
295+
296+
readDirPyLibs.every((element: string) => {
297+
const fullpath = path.posix.join(pyLibs, element);
298+
if (!fs.lstatSync(fullpath).isDirectory())
299+
{return true;}
300+
301+
const prefix = "python";
302+
303+
if (!element.startsWith(prefix))
304+
{return true;}
305+
306+
const startIndex = element.indexOf(prefix) + prefix.length;
307+
let endIndex = startIndex;
308+
for (let index = endIndex; index < element.length; index++)
309+
{
310+
if (element.charAt(index) === ".")
311+
{break;}
312+
313+
endIndex++;
314+
}
315+
316+
const numberPart = element.slice(startIndex, endIndex);
317+
pyVersionToUse = parseInt(numberPart, 10);
318+
return false;
319+
});
320+
321+
return path.posix.join(pyLibs, `python${pyVersionToUse}`);
322+
}
323+
324+
export function getPythonExecutablePath(c4dPath: string)
325+
{
326+
let pyVersionToUse = 39;
327+
328+
let pyLibs = path.posix.join(c4dPath, "resource", "modules", "python", "libs").replace(/\\/g, "/");
329+
const readDirPyLibs = fs.readdirSync(pyLibs);
330+
331+
readDirPyLibs.every((element: string) => {
332+
const fullpath = path.posix.join(pyLibs, element);
333+
if (!fs.lstatSync(fullpath).isDirectory())
334+
{return true;}
335+
336+
const prefix = "python";
337+
338+
if (!element.startsWith(prefix))
339+
{return true;}
340+
341+
const startIndex = element.indexOf(prefix) + prefix.length;
342+
let endIndex = startIndex;
343+
for (let index = endIndex; index < element.length; index++)
344+
{
345+
if (element.charAt(index) === ".")
346+
{break;}
347+
348+
endIndex++;
349+
}
350+
351+
const numberPart = element.slice(startIndex, endIndex);
352+
pyVersionToUse = parseInt(numberPart, 10);
353+
return false;
354+
});
355+
356+
357+
if (os.platform() === 'win32')
358+
{
359+
if (pyVersionToUse >= 311)
360+
{return path.posix.join(pyLibs, "win64", "python.exe");}
361+
362+
return path.posix.join(pyLibs, `python${pyVersionToUse}.win64.framework`, "python.exe");
363+
}
364+
365+
// Mac OS
366+
return path.posix.join(pyLibs, `python${pyVersionToUse}.macos.framework`, "python", "Contents", "MacOS", "python");
367+
}

src/settings.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import * as path from "path";
44
import { errorHandler, errShowErrorMessage as showErrorMessage} from './errors';
55
import { client } from "./async_client";
66
import { setupStatusBarClientListener } from "./statusbar";
7-
import { posix } from "path";
7+
import { getPythonFolder } from './file_system';
88
import assert = require('assert');
99

1010
export const C4D_PATH_CONFIG_ID = "c4d.path";
1111
export const C4D_IP_CONFIG_ID = "c4d.ip";
1212
export const C4D_PORT_CONFIG_ID = "c4d.port";
1313
export const C4D_TEMPLATE_CONFIG_ID = "c4d.template";
14+
export const C4D_BRING_IN_FRONT_CONFIG_ID = "c4d.bringconsoleinfront";
1415
export const C4D_CALL_GET_PATH_ON_CONNECT_ID = 'c4d.call_get_path_on_connect';
1516

1617
const PYTHON_AUTOCOMPLETE_EXTRA_PATH_CONFIG_ID = "python.autoComplete.extraPaths";
@@ -57,11 +58,9 @@ function setPythonExtraPath(inputPath?: string)
5758
if (!c4dPath || !fs.existsSync(c4dPath))
5859
{ return; }
5960

60-
let pathToAdd = path.join(c4dPath, "resource", "modules", "python", "libs", "python39");
61+
let pathToAdd = path.join(getPythonFolder(c4dPath));
6162
if (!fs.existsSync(pathToAdd))
62-
{ pathToAdd = path.join(c4dPath, "resource", "modules", "python", "libs", "python310"); }
63-
if (!fs.existsSync(pathToAdd))
64-
{ throw new Error("Incorrect path for c4d.path, autocompletion will not work"); }
63+
{ throw new Error("Unable to Find Python path for c4d.path, autocompletion will not work"); }
6564

6665
let pathToAddC4D = path.join(pathToAdd, "c4d");
6766
if (!fs.existsSync(pathToAddC4D))
@@ -168,7 +167,7 @@ export function GetAndStoreTemplateDir()
168167
let extension = vscode.extensions.getExtension("maxonc4dsdk.cinema4d-connector");
169168
assert(extension !== undefined);
170169

171-
templatePath = path.join(extension.extensionPath, "script_template");
170+
templatePath = path.posix.join(extension.extensionPath, "script_template");
172171
if (!templatePath || !fs.existsSync(templatePath))
173172
{
174173
throw new Error("Can't compute script template.");

0 commit comments

Comments
 (0)