-
Notifications
You must be signed in to change notification settings - Fork 1
/
save_actor.py
112 lines (78 loc) · 3.29 KB
/
save_actor.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
##############################################################################
# Imports Order: 3rd Party Imports, Python Built-ins, Custom Modules
##############################################################################
import bpy
import os
import string
##############################################################################
# Classes
##############################################################################
classes = [] # Initialize the class array to be registered
class SaveActorOperator(bpy.types.Operator):
"""Saves the current actor to the "saved" subfolder of the "add actor" menu"""
bl_idname = "wm.save_actor" # Unique operator reference name
bl_label = "Save Preset" # String for the UI
bl_options = {"REGISTER"} # Enable undo for the operator
def execute(self, context): # execute() is called when running the operator
# Grab the selected actor
actor = context.active_object
# Fields to fill in the template files
actor_fields = {
"actor_name": actor.name,
"actor_etype": actor["Actor Type"],
"actor_game_task": actor["Game Task"],
"actor_mesh_name": actor["Mesh"],
"actor_icon_name": actor["Icon"],
}
# Path to this script
script_path = os.path.dirname(__file__)
def fill_template(template, fields):
with open(template, "r") as f:
src = string.Template(f.read())
return src.substitute(fields)
def insert_before(file, text, divider):
with open(file, "r") as f:
contents = f.readlines()
with open(file, "w") as f:
for line in contents:
# This is a shitty implementation
if divider in line:
f.write(text)
f.write(line)
# Export the normal properties
content = fill_template(
os.path.join(script_path, "templates\\saved_actor_template.txt"),
actor_fields,
)
insert_before(
os.path.join(script_path, f"saved_actors.json"),
content,
"]",
)
for key, value in actor.items():
if key in [
"Actor Type",
"Game Task",
"Mesh",
"Icon",
"JSON Category",
"JSON Index",
]:
continue
print(f"{key} : {value}")
return {"FINISHED"} # Let Blender know the operator finished successfully
classes.append(SaveActorOperator) # Add the class to the array
##############################################################################
# Functions
##############################################################################
##############################################################################
# Registration
##############################################################################
def register():
for cls in classes: # Register all the classes
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes): # Unregister all the classes
bpy.utils.unregister_class(cls)
# if __name__ == "__main__": # For internal Blender script testing
# register()