-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlatex2blender.py
545 lines (458 loc) · 21.4 KB
/
latex2blender.py
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
bl_info = {
"name": "latex2blender",
"description": "Enables user to write LaTeX in Blender.",
"author": "Peter K. Johnson and George H. Seelinger",
"version": (1, 0, 7),
"blender": (2, 80, 0),
"location": "View3D > Sidebar",
"warning": "",
"wiki_url": "https://github.com/ghseeli/latex2blender/wiki",
"support": "COMMUNITY",
"category": "Add Mesh"
}
import bpy
from bpy.props import (StringProperty,
BoolProperty,
FloatProperty,
PointerProperty,
EnumProperty
)
from bpy.types import (Panel,
Material,
Menu,
Operator,
PropertyGroup,
)
from bl_operators.presets import AddPresetBase
from bl_ui.utils import PresetPanel
import os
import glob
import subprocess
import tempfile
import shutil
import math
def rel_to_abs(sp_name):
if bpy.context.scene.my_tool[sp_name].startswith('//'):
abs_path = os.path.abspath(bpy.path.abspath(bpy.context.scene.my_tool[sp_name]))
bpy.context.scene.my_tool[sp_name] = abs_path
# Various settings.
class Settings(PropertyGroup):
latex_code: StringProperty(
name="LaTeX Code",
description="Enter LaTeX Code",
default="",
)
custom_latex_path: StringProperty(
name="latex",
description="""
Enter the path of the folder containing the latex command
on your computer. If you are not sure where the latex command is
located, open your terminal/command prompt and type: \"where latex\" """,
default = "",
update = lambda s,c: rel_to_abs('custom_latex_path'),
subtype = 'DIR_PATH',
)
custom_pdflatex_path: StringProperty(
name="pdflatex",
description="""
Enter the path of the folder containing the pdflatex command
on your computer. If you are not sure where the pdflatex command is
located, open your terminal/command prompt and type: \"where pdflatex\" """,
default = "",
update = lambda s,c: rel_to_abs('custom_pdflatex_path'),
subtype = 'DIR_PATH',
)
custom_xelatex_path: StringProperty(
name="xelatex",
description="""
Enter the path of the folder containing the xelatex command
on your computer. If you are not sure where the xelatex command is
located, open your terminal/command prompt and type: \"where xelatex\" """,
default = "",
update = lambda s,c: rel_to_abs('custom_xelatex_path'),
subtype = 'DIR_PATH',
)
custom_lualatex_path: StringProperty(
name="lualatex",
description="""
Enter the path of the folder containing the lualatex command
on your computer. If you are not sure where the lualatex command is
located, open your terminal/command prompt and type: \"where lualatex\" """,
default = "",
update = lambda s,c: rel_to_abs('custom_lualatex_path'),
subtype = 'DIR_PATH',
)
custom_dvisvgm_path: StringProperty(
name="dvisvgm",
description="""
Enter the path of the folder containing the dvisvgm command
on your computer. If you are not sure where the dvisvgm command is
located, open your terminal/command prompt and type: \"where dvisvgm\" """,
default = "",
update = lambda s,c: rel_to_abs('custom_dvisvgm_path'),
subtype = 'DIR_PATH',
)
command_selection: EnumProperty(
name="Command",
description="Select the command used to compile LaTeX code",
items=[
('latex', 'latex', 'Use latex command to compile code'),
('pdflatex', 'pdflatex', 'Use pdflatex command to compile code'),
('xelatex', 'xelatex', 'Use xelatex command to compile code'),
('lualatex', 'lualatex', 'Use lualatex command to compile code')
]
)
text_scale: FloatProperty(
name="Scale",
description="Set size of text",
default=1.0,
)
x_loc: FloatProperty(
name="X",
description="Set x position",
default=0.0,
)
y_loc: FloatProperty(
name="Y",
description="Set y position",
default=0.0,
)
z_loc: FloatProperty(
name="Z",
description="Set z position",
default=0.0,
)
x_rot: FloatProperty(
name="X",
description="Set x rotation",
default=0.0,
)
y_rot: FloatProperty(
name="Y",
description="Set y rotation",
default=0.0,
)
z_rot: FloatProperty(
name="Z",
description="Set z rotation",
default=0.0,
)
custom_material_bool: BoolProperty(
name="Use Custom Material",
description="Use a custom material",
default=False
)
custom_material_value: PointerProperty(
type=Material,
name="Material",
description="Choose a material"
)
custom_preamble_bool: BoolProperty(
name="Use Custom Preamble",
description="Use a custom preamble",
default=False
)
preamble_path: StringProperty(
name="Preamble",
description="Choose a .tex file for the preamble",
default="",
update = lambda s,c: rel_to_abs('preamble_path'),
subtype='FILE_PATH'
)
def ErrorMessageBox(message, title):
def draw(self, context):
self.layout.label(text=message)
bpy.context.window_manager.popup_menu(draw, title=title, icon='ERROR')
# Imports compiled latex code into blender given chosen settings.
def import_latex(self, context, latex_code, custom_latex_path,
custom_pdflatex_path, custom_xelatex_path, custom_lualatex_path,
custom_dvisvgm_path, command_selection, text_scale, x_loc,
y_loc, z_loc, x_rot,y_rot, z_rot, custom_preamble_bool,
temp_dir, custom_material_bool, custom_material_value,
compile_mode, preamble_path=None):
# Set current directory to temp_directory
current_dir = os.getcwd()
os.chdir(temp_dir)
temp_dir = os.path.realpath(temp_dir)
# Create temp latex file with specified preamble.
temp_file_name = temp_dir + os.sep + 'temp'
if custom_preamble_bool:
shutil.copy(preamble_path, temp_file_name + '.tex')
temp = open(temp_file_name + '.tex', "a")
else:
temp = open(temp_file_name + '.tex', "a")
default_preamble = '\\documentclass{amsart}\n\\usepackage{amssymb,amsfonts}\n\\usepackage{tikz}' \
'\n\\usepackage{tikz-cd}\n\\pagestyle{empty}\n\\thispagestyle{empty}'
temp.write(default_preamble)
# Add latex code to temp.tex and close the file.
temp.write('\n\\begin{document}\n' + latex_code + '\n\\end{document}')
temp.close()
# Try to compile temp.tex and create an svg file
try:
# Updates 'PATH' to include reference to folder containing latex and dvisvgm executable files.
latex_exec_path = '/Library/TeX/texbin'
local_env = os.environ.copy()
local_env['PATH'] = (latex_exec_path + os.pathsep + local_env['PATH'])
if custom_latex_path != "" and custom_latex_path != '/Library/TeX/texbin':
local_env['PATH'] = (custom_latex_path + os.pathsep + local_env['PATH'])
if (custom_pdflatex_path != "" and custom_pdflatex_path != '/Library/TeX/texbin'
and custom_pdflatex_path != custom_latex_path):
local_env['PATH'] = (custom_pdflatex_path + os.pathsep + local_env['PATH'])
if (custom_xelatex_path != "" and custom_xelatex_path != '/Library/TeX/texbin'
and custom_xelatex_path != custom_latex_path
and custom_xelatex_path != custom_pdflatex_path):
local_env['PATH'] = (custom_xelatex_path + os.pathsep + local_env['PATH'])
if (custom_lualatex_path != "" and custom_lualatex_path != '/Library/TeX/texbin'
and custom_lualatex_path != custom_latex_path
and custom_lualatex_path != custom_pdflatex_path
and custom_lualatex_path != custom_xelatex_path):
local_env['PATH'] = (custom_lualatex_path + os.pathsep + local_env['PATH'])
if (custom_dvisvgm_path != "" and custom_dvisvgm_path != '/Library/TeX/texbin'
and custom_dvisvgm_path != custom_latex_path
and custom_dvisvgm_path != custom_pdflatex_path
and custom_dvisvgm_path != custom_xelatex_path
and custom_dvisvgm_path != custom_lualatex_path):
local_env['PATH'] = (custom_dvisvgm_path + os.pathsep + local_env['PATH'])
if command_selection == "latex":
tex_process = subprocess.run(["latex", "-interaction=nonstopmode", temp_file_name + ".tex"], env=local_env, text=True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
elif command_selection == "pdflatex":
tex_process = subprocess.run(["pdflatex", "-interaction=nonstopmode", "-output-format=dvi",temp_file_name + ".tex"], env=local_env, text=True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
elif command_selection == "xelatex":
tex_process = subprocess.run(["xelatex", "-interaction=nonstopmode", "-no-pdf", temp_file_name + ".tex"], env=local_env, text=True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
else:
tex_process = subprocess.run(["lualatex", "-interaction=nonstopmode", "-output-format=dvi", temp_file_name + ".tex"], env=local_env, text=True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
dvisvgm_process = subprocess.run(["dvisvgm", "--no-fonts", temp_file_name + ".dvi"], env=local_env, text=True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
svg_file_list = glob.glob("*.svg")
bpy.ops.object.select_all(action='DESELECT')
if len(svg_file_list) == 0:
self.report({"ERROR"},
"Please check your LaTeX code for errors and that LaTeX and dvisvgm are properly "
"installed and their paths are specified correctly. Also, if using a custom preamble, check that it is formatted correctly. \n"
"Tex return code " + str(tex_process.returncode) + "\n"
"dvi2svgm return code " + str(dvisvgm_process.returncode) + "\n"
"Tex error message: " + str(tex_process.stdout) + "\n"
"dvi2svgm error message: " + str(dvisvgm_process.stdout)
)
else:
svg_file_path = temp_dir + os.sep + svg_file_list[0]
objects_before_import = bpy.data.objects[:]
# Import svg into blender as curve
bpy.ops.import_curve.svg(filepath=svg_file_path)
# Select imported objects
imported_curve = [x for x in bpy.data.objects if x not in objects_before_import]
active_obj = imported_curve[0]
context.view_layer.objects.active = active_obj
for x in imported_curve:
x.select_set(True)
# Convert to mesh
bpy.ops.object.convert(target='MESH')
# Join components to merge into single object
bpy.ops.object.join()
# Adjust scale, location, and rotation.
bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS', center='MEDIAN')
active_obj.scale = (600*text_scale, 600*text_scale, 600*text_scale)
bpy.ops.object.transform_apply(location = False, scale = True, rotation = False)
active_obj.location = (x_loc, y_loc, z_loc)
active_obj.rotation_euler = (math.radians(x_rot), math.radians(y_rot), math.radians(z_rot))
# Move mesh to scene collection and delete the temp.svg collection. Then rename mesh.
temp_svg_collection = active_obj.users_collection[0]
bpy.ops.object.move_to_collection(collection_index=0)
bpy.data.collections.remove(temp_svg_collection)
active_obj.name = 'LaTeX Figure'
if custom_material_bool:
active_obj.material_slots[0].material = custom_material_value
if compile_mode == "grease pencil":
# Convert to grease pencil
bpy.ops.object.convert(target='GPENCIL', angle=0, thickness=1, seams=True, faces=True, offset=0)
# Moves to scene collection, fixes name.
bpy.ops.object.move_to_collection(collection_index=0)
bpy.context.selected_objects[0].name = "LaTeX Figure"
if custom_material_bool:
bpy.context.selected_objects[0].material_slots[0].material = custom_material_value
# Create custom property that stores typed LaTeX code
bpy.context.selected_objects[0]["Original LaTeX Code"] = latex_code
except FileNotFoundError as e:
ErrorMessageBox("Please check that LaTeX is installed on your system and that its path is specified correctly.", "Compilation Error")
except subprocess.CalledProcessError as e:
ErrorMessageBox("Please check your LaTeX code for errors and that LaTeX and dvisvgm are properly "
"installed and their paths are specified correctly. Also, if using a custom preamble, check that it is formatted correctly. "
"Return code: " + str(e.returncode) + " " + str(e.output),
"Compilation Error")
finally:
os.chdir(current_dir)
print("Finished trying to compile LaTeX and create an svg file.")
class LATEX2BLENDER_MT_Presets(Menu):
bl_idname = 'LATEX2BLENDER_MT_Presets'
bl_label = 'Presets'
preset_subdir = 'latex2blender_presets'
preset_operator = 'script.execute_preset'
draw = Menu.draw_preset
class OBJECT_OT_add_latex_preset(AddPresetBase, Operator):
bl_idname = 'object.add_latex_preset'
bl_label = 'Create a new preset with below settings'
preset_menu = 'LATEX2BLENDER_MT_Presets'
preset_defines = ['t = bpy.context.scene.my_tool']
preset_values = [
't.custom_latex_path',
't.custom_pdflatex_path',
't.custom_xelatex_path',
't.custom_lualatex_path',
't.custom_dvisvgm_path',
't.command_selection',
't.text_scale',
't.x_loc',
't.y_loc',
't.z_loc',
't.x_rot',
't.y_rot',
't.z_rot',
't.custom_preamble_bool',
't.preamble_path'
]
preset_subdir = 'latex2blender_presets'
# Display into an existing panel
def panel_func(self, context):
layout = self.layout
row = layout.row(align=True)
row.menu(LATEX2BLENDER_MT_Presets.__name__, text=LATEX2BLENDER_MT_Presets.bl_label)
row.operator(OBJECT_OT_add_latex_preset.bl_idname, text="", icon='ADD')
row.operator(OBJECT_OT_add_latex_preset.bl_idname, text="", icon='REMOVE').remove_active = True
# Compile latex as mesh.
class WM_OT_compile_as_mesh(Operator):
bl_idname = "wm.compile_as_mesh"
bl_label = "Compile as Mesh"
def execute(self, context):
scene = context.scene
t = scene.my_tool
if t.latex_code == '' and t.custom_preamble_bool \
and t.preamble_path == '':
ErrorMessageBox("No LaTeX code has been entered and no preamble file has been chosen. Please enter some "
"LaTeX code and choose a .tex file for the preamble", "Multiple Errors")
elif t.custom_material_bool and t.custom_material_value is None:
ErrorMessageBox("No material has been chosen. Please choose a material.", "Custom Material Error")
elif t.latex_code == '':
ErrorMessageBox("No LaTeX code has been entered. Please enter some LaTeX code.", "LaTeX Code Error")
elif t.custom_preamble_bool and t.preamble_path == '':
ErrorMessageBox("No preamble file has been chosen. Please choose a file.", "Custom Preamble Error")
else:
with tempfile.TemporaryDirectory() as temp_dir:
import_latex(self, context, t.latex_code, t.custom_latex_path,
t.custom_pdflatex_path, t.custom_xelatex_path,
t.custom_lualatex_path, t.custom_dvisvgm_path,
t.command_selection, t.text_scale, t.x_loc,
t.y_loc, t.z_loc, t.x_rot, t.y_rot, t.z_rot,
t.custom_preamble_bool, temp_dir,
t.custom_material_bool, t.custom_material_value,
'mesh', t.preamble_path)
return {'FINISHED'}
# Compile latex as grease pencil.
class WM_OT_compile_as_grease_pencil(Operator):
bl_idname = "wm.compile_as_grease_pencil"
bl_label = "Compile as Grease Pencil"
def execute(self, context):
scene = context.scene
t = scene.my_tool
if t.latex_code == '' and t.custom_preamble_bool \
and t.preamble_path == '':
ErrorMessageBox("No LaTeX code has been entered and no preamble file has been chosen. Please enter some "
"LaTeX code and choose a .tex file for the preamble", "Multiple Errors")
elif t.custom_material_bool and t.custom_material_value is None:
ErrorMessageBox("No material has been chosen. Please choose a material.", "Custom Material Error")
elif t.latex_code == '':
ErrorMessageBox("No LaTeX code has been entered. Please enter some LaTeX code.", "LaTeX Code Error")
elif t.custom_preamble_bool and t.preamble_path == '':
ErrorMessageBox("No preamble file has been chosen. Please choose a file.", "Custom Preamble Error")
else:
with tempfile.TemporaryDirectory() as temp_dir:
import_latex(self, context, t.latex_code, t.custom_latex_path,
t.custom_pdflatex_path, t.custom_xelatex_path,
t.custom_lualatex_path, t.custom_dvisvgm_path,
t.command_selection, t.text_scale, t.x_loc,
t.y_loc, t.z_loc, t.x_rot, t.y_rot, t.z_rot,
t.custom_preamble_bool, temp_dir,
t.custom_material_bool, t.custom_material_value,
'grease pencil', t.preamble_path)
return {'FINISHED'}
class OBJECT_PT_latex2blender_panel(Panel):
bl_idname = "OBJECT_PT_latex2blender_panel"
bl_label = "latex2blender"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "latex2blender"
bl_context = "objectmode"
def draw(self, context):
layout = self.layout
scene = context.scene
latex2blender_tool = scene.my_tool
layout.prop(latex2blender_tool, "latex_code")
layout.separator()
layout.prop(latex2blender_tool, "command_selection")
layout.separator
box = layout.box()
box.label(text="Paths to directories containing commands.")
row= box.row()
row.prop(latex2blender_tool, "custom_latex_path")
row = box.row()
row.prop(latex2blender_tool, "custom_pdflatex_path")
row = box.row()
row.prop(latex2blender_tool, "custom_xelatex_path")
row = box.row()
row.prop(latex2blender_tool, "custom_lualatex_path")
row = box.row()
row.prop(latex2blender_tool, "custom_dvisvgm_path")
box = layout.box()
box.label(text="Transform Settings")
row = box.row()
row.prop(latex2blender_tool, "text_scale")
split = box.split()
col = split.column(align=True)
col.label(text="Location:")
col.prop(latex2blender_tool, "x_loc")
col.prop(latex2blender_tool, "y_loc")
col.prop(latex2blender_tool, "z_loc")
col = split.column(align=True)
col.label(text="Rotation:")
col.prop(latex2blender_tool, "x_rot")
col.prop(latex2blender_tool, "y_rot")
col.prop(latex2blender_tool, "z_rot")
layout.prop(latex2blender_tool, "custom_preamble_bool")
if latex2blender_tool.custom_preamble_bool:
layout.prop(latex2blender_tool, "preamble_path")
layout.prop(latex2blender_tool, "custom_material_bool")
if latex2blender_tool.custom_material_bool:
layout.prop(latex2blender_tool, "custom_material_value")
layout.separator()
box = layout.box()
row = box.row()
row.operator("wm.compile_as_mesh")
row = box.row()
row.operator("wm.compile_as_grease_pencil")
classes = (
Settings,
LATEX2BLENDER_MT_Presets,
OBJECT_OT_add_latex_preset,
WM_OT_compile_as_mesh,
WM_OT_compile_as_grease_pencil,
OBJECT_PT_latex2blender_panel
)
# Get path of blender scripts directory.
scripts_dir = bpy.utils.user_resource('SCRIPTS')
# Get path of latex2blender_preset directory
l2b_presets = os.path.join(scripts_dir, 'presets', 'latex2blender_presets')
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.Scene.my_tool = PointerProperty(type=Settings)
OBJECT_PT_latex2blender_panel.prepend(panel_func)
# Create latex2blender_presets folder if not already created.
if not os.path.isdir(l2b_presets):
os.makedirs(l2b_presets)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
del bpy.types.Scene.my_tool
OBJECT_PT_latex2blender_panel.remove(panel_func)
if __name__ == "__main__":
register()