-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlenderSetupUtils.java
More file actions
186 lines (163 loc) · 6.59 KB
/
BlenderSetupUtils.java
File metadata and controls
186 lines (163 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*-
* #%L
* A Mastodon plugin data allows to show the embryo in Blender.
* %%
* Copyright (C) 2022 - 2025 Matthias Arzt
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.mastodon.blender.setup;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
public class BlenderSetupUtils
{
public static boolean verifyBlenderBinary( Path blenderPath )
{
try
{
String expectOutput = "Blender started from within Mastodon.";
String python = "print('" + expectOutput + "')";
return runPythonWithinBlender( blenderPath, python, expectOutput );
}
catch ( Throwable e ) {
return false;
}
}
public static void installAddon( Path blenderPath ) throws IOException
{
Path tmpDir = Files.createTempDirectory( "mastodon_blender_setup" );
Path addonZip = tmpDir.resolve( "install_mastodon_blender_addon.zip" );
Path pythonScript = tmpDir.resolve( "install_addon.py" );
FileUtils.copyURLToFile( BlenderSetupUtils.class.getResource( "/mastodon_blender_view.zip" ), addonZip.toFile() );
FileUtils.copyURLToFile( BlenderSetupUtils.class.getResource( "/blender-scripts/install_addon.py" ), pythonScript.toFile() );
Result result = runCommand( blenderPath.toString(), //
"--background", //
"--python", pythonScript.toAbsolutePath().toString(), //
"--", addonZip.toAbsolutePath().toString() ); //
if ( result.exitCode != 0 )
throw new RuntimeException( "Installation failed.\n" + result );
if ( !result.stdout.contains( "dependencies installed" ) )
throw new RuntimeException( "Installation of the dependencies for the mastodon_blender_view addon failed:\n" + result );
if ( !result.stdout.contains( "mastodon blender view addon installed" ) )
throw new RuntimeException( "Installation of the mastodon_blender_view addon failed:\n" + result );
if ( !result.stdout.contains( "google RPC code compiled" ) )
throw new RuntimeException( "Installation of the mastodon_blender_view addon failed:\n" + result );
Files.delete( pythonScript );
Files.delete( addonZip );
Files.delete( tmpDir );
}
public static void uninstallAddon( Path blenderPath )
{
String python = "import bpy; " + //
"bpy.ops.preferences.addon_remove(module='mastodon_blender_view'); " + //
"print('uninstall completed');";
runPythonWithinBlender( blenderPath, python, "uninstall completed" );
}
public static boolean isMastodonAddonInstalled( Path blenderPath )
{
String python = "import addon_utils; " + //
"print([module.__name__ for module in addon_utils.modules()])";
String expectedOutput = "'mastodon_blender_view'";
boolean success = runPythonWithinBlender( blenderPath, python, expectedOutput );
return success;
}
public static void runAddonTest( Path blenderPath )
throws IOException, InterruptedException
{
String script = "import mastodon_blender_view.mb_server as mb_server;" //
+ "import time;"
+ "import bidict;" // import packages that are not necessary for the test but required for correct plugin execution
+ "import grpc;"
+ "import google.protobuf;"
+ "import pandas;"
+ "mb_server.delayed_start_server();"
+ "time.sleep(8)";
int port = StartBlender.getFreePort();
Process process = StartBlender.startBlender( blenderPath, //
null, //
port,
"--background", //
"--python-expr", script );
process.waitFor();
}
private static boolean runPythonWithinBlender( Path blenderPath, String python, String expectedOutput )
{
Result result = runCommand( blenderPath.toString(), "--background", "--python-expr", python );
return result.exitCode == 0 && result.stdout.contains( expectedOutput );
}
private static Result runCommand( String... command )
{
try
{
Process process = new ProcessBuilder( command ).start();
process.waitFor();
String output = IOUtils.toString( process.getInputStream(), StandardCharsets.UTF_8 );
String error = IOUtils.toString( process.getErrorStream(), StandardCharsets.UTF_8 );
int exitCode = process.exitValue();
return new Result( Arrays.asList( command ), output, error, exitCode );
}
catch ( IOException | InterruptedException e )
{
throw new RuntimeException( e );
}
}
public static class Result
{
private final List< String > command;
private final String stdout;
private final String stderr;
private final int exitCode;
public Result( List< String > command, String stdout, String stderr,
int exitCode )
{
this.command = command;
this.stdout = stdout;
this.stderr = stderr;
this.exitCode = exitCode;
}
@Override
public String toString()
{
final StringBuilder s = new StringBuilder();
s.append( "Command:\n " ).append( command.get( 0 ) );
for ( int i = 1; i < command.size(); i++ )
s.append( " " ).append( addQuotes( command.get( i ) ) );
s.append( "\n\n" );
s.append( "Exit Code:\n " ).append( exitCode ).append("\n\n");
s.append( "Command Output:\n\n" ).append( stdout ).append("\n\n");
s.append( "Commend Error:\n\n" ).append( stderr ).append("\n\n");
return s.toString();
}
static String addQuotes(String value)
{
boolean simple = value.matches("[a-zA-Z0-9_-]+");
return simple ? value : "'" + value + "'";
}
}
}