armory/blender/arm/props_traits.py

724 lines
28 KiB
Python
Raw Normal View History

2015-10-30 13:23:09 +01:00
import shutil
import bpy
2017-03-15 12:30:14 +01:00
import subprocess
2015-10-30 13:23:09 +01:00
import os
2018-03-25 12:00:43 +02:00
import webbrowser
2018-11-13 14:51:02 +01:00
import bpy.utils.previews
from bpy.types import Menu, Panel, UIList, NodeTree
2015-10-30 13:23:09 +01:00
from bpy.props import *
2017-07-24 02:27:22 +02:00
from arm.props_traits_props import *
2017-03-15 12:30:14 +01:00
import arm.utils
import arm.write_data as write_data
2018-08-07 08:56:48 +02:00
import arm.make as make
2019-08-07 17:02:36 +02:00
import json
2015-10-30 13:23:09 +01:00
2017-10-19 10:10:20 +02:00
def trigger_recompile(self, context):
wrd = bpy.data.worlds['Arm']
wrd.arm_recompile = True
2017-12-11 00:55:26 +01:00
def update_trait_group(self, context):
2019-01-28 22:52:29 +01:00
o = context.object if self.is_object else context.scene
2017-12-11 00:55:26 +01:00
if o == None:
return
i = o.arm_traitlist_index
if i >= 0 and i < len(o.arm_traitlist):
t = o.arm_traitlist[i]
2017-12-11 19:00:32 +01:00
if t.type_prop == 'Haxe Script' or t.type_prop == 'Bundled Script':
2017-12-11 00:55:26 +01:00
t.name = t.class_name_prop
elif t.type_prop == 'WebAssembly':
t.name = t.webassembly_prop
elif t.type_prop == 'UI Canvas':
t.name = t.canvas_name_prop
elif t.type_prop == 'Logic Nodes':
if t.node_tree_prop != None:
t.name = t.node_tree_prop.name
2017-12-11 19:00:32 +01:00
# Fetch props
if t.type_prop == 'Bundled Script' and t.name != '':
file_path = arm.utils.get_sdk_path() + '/armory/Sources/armory/trait/' + t.name + '.hx'
if os.path.exists(file_path):
arm.utils.fetch_script_props(file_path)
arm.utils.fetch_prop(o)
2019-02-05 12:59:34 +01:00
# Show trait users as collections
2019-02-05 13:04:27 +01:00
if self.is_object:
for col in bpy.data.collections:
if col.name.startswith('Trait|') and o.name in col.objects:
col.objects.unlink(o)
for t in o.arm_traitlist:
if 'Trait|' + t.name not in bpy.data.collections:
col = bpy.data.collections.new('Trait|' + t.name)
else:
col = bpy.data.collections['Trait|' + t.name]
col.objects.link(o)
2016-07-10 00:51:39 +02:00
2017-12-11 00:55:26 +01:00
class ArmTraitListItem(bpy.types.PropertyGroup):
2018-12-18 23:48:38 +01:00
name: StringProperty(name="Name", description="A name for this item", default="")
enabled_prop: BoolProperty(name="", description="A name for this item", default=True, update=trigger_recompile)
2019-01-28 22:52:29 +01:00
is_object: BoolProperty(name="", default=True)
2020-04-10 20:43:59 +02:00
fake_user: BoolProperty(name="Fake User", description="Export this trait even if it is deactivated", default=False)
2018-12-18 23:48:38 +01:00
type_prop: EnumProperty(
2018-03-15 23:24:48 +01:00
items = [('Haxe Script', 'Haxe', 'Haxe Script'),
2018-04-17 12:23:55 +02:00
('WebAssembly', 'Wasm', 'WebAssembly'),
2018-03-15 23:24:48 +01:00
('UI Canvas', 'UI', 'UI Canvas'),
('Bundled Script', 'Bundled', 'Bundled Script'),
('Logic Nodes', 'Nodes', 'Logic Nodes')
2017-08-21 12:17:55 +02:00
],
name = "Type")
2018-12-18 23:48:38 +01:00
class_name_prop: StringProperty(name="Class", description="A name for this item", default="", update=update_trait_group)
canvas_name_prop: StringProperty(name="Canvas", description="A name for this item", default="", update=update_trait_group)
webassembly_prop: StringProperty(name="Module", description="A name for this item", default="", update=update_trait_group)
node_tree_prop: PointerProperty(type=NodeTree, update=update_trait_group)
2018-12-18 23:48:38 +01:00
arm_traitpropslist: CollectionProperty(type=ArmTraitPropListItem)
arm_traitpropslist_index: IntProperty(name="Index for my_list", default=0)
arm_traitpropswarnings: CollectionProperty(type=ArmTraitPropWarning)
2017-08-21 12:17:55 +02:00
class ARM_UL_TraitList(bpy.types.UIList):
2015-10-30 13:23:09 +01:00
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
2018-11-13 14:51:02 +01:00
custom_icon = "NONE"
custom_icon_value = 0
if item.type_prop == "Haxe Script":
custom_icon_value = icons_dict["haxe"].icon_id
elif item.type_prop == "WebAssembly":
custom_icon_value = icons_dict["wasm"].icon_id
elif item.type_prop == "UI Canvas":
2018-12-19 13:33:17 +01:00
custom_icon = "OBJECT_DATAMODE"
2018-11-13 14:51:02 +01:00
elif item.type_prop == "Bundled Script":
custom_icon = 'OBJECT_DATAMODE'
elif item.type_prop == "Logic Nodes":
custom_icon = 'NODETREE'
2015-10-30 13:23:09 +01:00
# Make sure your code supports all 3 layout types
if self.layout_type in {'DEFAULT', 'COMPACT'}:
layout.prop(item, "enabled_prop")
2020-04-10 20:43:59 +02:00
# Display " " for props without a name to right-align the
# fake_user button
layout.label(text=item.name if item.name != "" else " ", icon=custom_icon, icon_value=custom_icon_value)
2015-10-30 13:23:09 +01:00
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
2018-11-13 14:51:02 +01:00
layout.label(text="", icon=custom_icon, icon_value=custom_icon_value)
2015-10-30 13:23:09 +01:00
2020-04-10 20:43:59 +02:00
layout.prop(item, "fake_user", text="", icon="FAKE_USER_ON" if item.fake_user else "FAKE_USER_OFF")
2017-08-21 12:17:55 +02:00
class ArmTraitListNewItem(bpy.types.Operator):
bl_idname = "arm_traitlist.new_item"
bl_label = "New Trait Item"
bl_description = "Add a new trait item to the list"
2015-10-30 13:23:09 +01:00
is_object: BoolProperty(name="Object Trait", description="Whether this is an object or scene trait", default=False)
2018-12-18 23:48:38 +01:00
type_prop: EnumProperty(
2018-11-13 14:51:02 +01:00
items = [('Haxe Script', 'Haxe', 'Haxe Script'),
('WebAssembly', 'Wasm', 'WebAssembly'),
('UI Canvas', 'UI', 'UI Canvas'),
('Bundled Script', 'Bundled', 'Bundled Script'),
('Logic Nodes', 'Nodes', 'Logic Nodes')
],
name = "Type")
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
def draw(self, context):
2018-11-13 14:51:02 +01:00
layout = self.layout
# Todo: show is_object property when called from operator search menu
# layout.prop(self, "is_object")
2018-11-13 14:51:02 +01:00
layout.prop(self, "type_prop", expand=True)
2017-08-26 18:41:36 +02:00
2015-10-30 13:23:09 +01:00
def execute(self, context):
2017-08-26 18:41:36 +02:00
if self.is_object:
obj = bpy.context.object
2017-08-27 12:50:09 +02:00
else:
obj = bpy.context.scene
2017-08-26 18:41:36 +02:00
trait = obj.arm_traitlist.add()
2019-01-28 22:52:29 +01:00
trait.is_object = self.is_object
2018-11-13 14:51:02 +01:00
trait.type_prop = self.type_prop
2017-08-26 18:41:36 +02:00
obj.arm_traitlist_index = len(obj.arm_traitlist) - 1
2017-10-19 10:10:20 +02:00
trigger_recompile(None, None)
2015-10-30 13:23:09 +01:00
return{'FINISHED'}
2017-08-21 12:17:55 +02:00
class ArmTraitListDeleteItem(bpy.types.Operator):
"""Delete the selected item from the list"""
2017-08-21 12:17:55 +02:00
bl_idname = "arm_traitlist.delete_item"
2015-10-30 13:23:09 +01:00
bl_label = "Deletes an item"
bl_options = {'INTERNAL'}
2015-10-30 13:23:09 +01:00
2018-12-18 23:48:38 +01:00
is_object: BoolProperty(name="", description="A name for this item", default=False)
2017-08-26 18:41:36 +02:00
2015-10-30 13:23:09 +01:00
@classmethod
def poll(self, context):
""" Enable if there's something in the list """
2017-09-06 13:28:59 +02:00
obj = bpy.context.object
2017-08-26 18:41:36 +02:00
if obj == None:
2017-08-22 12:08:44 +02:00
return False
2017-08-26 18:41:36 +02:00
return len(obj.arm_traitlist) > 0
2015-10-30 13:23:09 +01:00
def execute(self, context):
2017-09-06 13:28:59 +02:00
obj = bpy.context.object
lst = obj.arm_traitlist
2017-08-26 18:41:36 +02:00
index = obj.arm_traitlist_index
2015-10-30 13:23:09 +01:00
2017-09-06 13:28:59 +02:00
if len(lst) <= index:
return{'FINISHED'}
lst.remove(index)
2017-12-11 00:55:26 +01:00
update_trait_group(self, context)
2015-10-30 13:23:09 +01:00
if index > 0:
index = index - 1
2017-08-26 18:41:36 +02:00
obj.arm_traitlist_index = index
2015-10-30 13:23:09 +01:00
return{'FINISHED'}
2017-09-06 13:28:59 +02:00
class ArmTraitListDeleteItemScene(bpy.types.Operator):
"""Delete the selected item from the list"""
2017-09-06 13:28:59 +02:00
bl_idname = "arm_traitlist.delete_item_scene"
bl_label = "Deletes an item"
bl_options = {'INTERNAL'}
2017-09-06 13:28:59 +02:00
2018-12-18 23:48:38 +01:00
is_object: BoolProperty(name="", description="A name for this item", default=False)
2017-09-06 13:28:59 +02:00
@classmethod
def poll(self, context):
""" Enable if there's something in the list """
obj = bpy.context.scene
if obj == None:
return False
return len(obj.arm_traitlist) > 0
def execute(self, context):
obj = bpy.context.scene
lst = obj.arm_traitlist
index = obj.arm_traitlist_index
if len(lst) <= index:
return{'FINISHED'}
lst.remove(index)
if index > 0:
index = index - 1
obj.arm_traitlist_index = index
return{'FINISHED'}
2015-10-30 13:23:09 +01:00
2017-08-21 12:17:55 +02:00
class ArmTraitListMoveItem(bpy.types.Operator):
"""Move an item in the list"""
2017-08-21 12:17:55 +02:00
bl_idname = "arm_traitlist.move_item"
2015-10-30 13:23:09 +01:00
bl_label = "Move an item in the list"
bl_options = {'INTERNAL'}
2018-12-18 23:48:38 +01:00
direction: EnumProperty(
2015-10-30 13:23:09 +01:00
items=(
('UP', 'Up', ""),
('DOWN', 'Down', ""),))
2018-12-18 23:48:38 +01:00
is_object: BoolProperty(name="", description="A name for this item", default=False)
2017-08-26 18:41:36 +02:00
2015-10-30 13:23:09 +01:00
def move_index(self):
# Move index of an item render queue while clamping it
2017-08-27 12:50:09 +02:00
if self.is_object:
2017-08-26 18:41:36 +02:00
obj = bpy.context.object
else:
obj = bpy.context.scene
index = obj.arm_traitlist_index
list_length = len(obj.arm_traitlist) - 1
2015-10-30 13:23:09 +01:00
new_index = 0
if self.direction == 'UP':
new_index = index - 1
elif self.direction == 'DOWN':
new_index = index + 1
new_index = max(0, min(new_index, list_length))
obj.arm_traitlist.move(index, new_index)
obj.arm_traitlist_index = new_index
2015-10-30 13:23:09 +01:00
def execute(self, context):
2017-08-27 12:50:09 +02:00
if self.is_object:
2017-08-26 18:41:36 +02:00
obj = bpy.context.object
else:
obj = bpy.context.scene
list = obj.arm_traitlist
index = obj.arm_traitlist_index
2015-10-30 13:23:09 +01:00
if self.direction == 'DOWN':
neighbor = index + 1
self.move_index()
elif self.direction == 'UP':
neighbor = index - 1
self.move_index()
else:
return{'CANCELLED'}
return{'FINISHED'}
2017-08-21 12:17:55 +02:00
class ArmEditScriptButton(bpy.types.Operator):
2016-07-10 00:51:39 +02:00
bl_idname = 'arm.edit_script'
bl_label = 'Edit Script'
bl_description = 'Edit script in the text editor'
bl_options = {'INTERNAL'}
2017-08-26 18:41:36 +02:00
2018-12-18 23:48:38 +01:00
is_object: BoolProperty(name="", description="A name for this item", default=False)
2019-06-23 12:56:32 +02:00
2016-07-10 00:51:39 +02:00
def execute(self, context):
2018-08-07 08:56:48 +02:00
2019-01-17 21:36:42 +01:00
arm.utils.check_default_props()
if not os.path.exists(arm.utils.get_fp() + "/khafile.js"):
print('Generating Krom project for IDE build configuration')
2019-01-17 21:36:42 +01:00
make.build('krom')
2018-08-07 08:56:48 +02:00
2017-08-27 12:50:09 +02:00
if self.is_object:
2017-08-26 18:41:36 +02:00
obj = bpy.context.object
else:
obj = bpy.context.scene
2017-08-27 12:50:09 +02:00
item = obj.arm_traitlist[obj.arm_traitlist_index]
2017-05-13 17:17:43 +02:00
pkg = arm.utils.safestr(bpy.data.worlds['Arm'].arm_project_package)
# Replace the haxe package syntax with the os-dependent path syntax for opening
hx_path = arm.utils.get_fp() + '/Sources/' + pkg + '/' + item.class_name_prop.replace('.', os.sep) + '.hx'
arm.utils.open_editor(hx_path)
2016-07-10 00:51:39 +02:00
return{'FINISHED'}
2017-08-21 12:17:55 +02:00
class ArmEditBundledScriptButton(bpy.types.Operator):
2017-02-24 21:29:49 +01:00
bl_idname = 'arm.edit_bundled_script'
bl_label = 'Edit Script'
bl_description = 'Copy script to project and edit in the text editor'
bl_options = {'INTERNAL'}
2017-08-26 18:41:36 +02:00
2018-12-18 23:48:38 +01:00
is_object: BoolProperty(name="", description="A name for this item", default=False)
2019-06-23 12:56:32 +02:00
2017-02-24 21:29:49 +01:00
def execute(self, context):
2018-08-07 08:56:48 +02:00
if not arm.utils.check_saved(self):
return {'CANCELLED'}
2019-06-23 12:56:32 +02:00
2017-08-27 12:50:09 +02:00
if self.is_object:
obj = bpy.context.object
else:
obj = bpy.context.scene
2017-03-15 12:30:14 +01:00
sdk_path = arm.utils.get_sdk_path()
project_path = arm.utils.get_fp()
2017-05-13 17:17:43 +02:00
pkg = arm.utils.safestr(bpy.data.worlds['Arm'].arm_project_package)
2019-06-23 12:56:32 +02:00
item = obj.arm_traitlist[obj.arm_traitlist_index]
2017-02-24 21:29:49 +01:00
source_hx_path = sdk_path + '/armory/Sources/armory/trait/' + item.class_name_prop + '.hx'
target_hx_path = project_path + '/Sources/' + pkg + '/' + item.class_name_prop + '.hx'
if not os.path.isfile(target_hx_path):
# Rewrite package and copy
sf = open(source_hx_path)
sf.readline()
tf = open(target_hx_path, 'w')
tf.write('package ' + pkg + ';\n')
shutil.copyfileobj(sf, tf)
sf.close()
tf.close()
2017-03-15 12:30:14 +01:00
arm.utils.fetch_script_names()
2017-02-24 21:29:49 +01:00
# From bundled to script
item.type_prop = 'Haxe Script'
# Edit in Kode Studio
2017-09-10 21:47:00 +02:00
bpy.ops.arm.edit_script('EXEC_DEFAULT', is_object=self.is_object)
2019-06-23 12:56:32 +02:00
2017-02-24 21:29:49 +01:00
return{'FINISHED'}
2019-08-07 17:02:36 +02:00
class ArmoryGenerateNavmeshButton(bpy.types.Operator):
"""Generate navmesh from selected meshes"""
2019-08-07 17:02:36 +02:00
bl_idname = 'arm.generate_navmesh'
bl_label = 'Generate Navmesh'
2019-08-07 17:02:36 +02:00
def execute(self, context):
obj = context.active_object
2019-09-11 20:58:53 +02:00
2019-08-07 17:02:36 +02:00
if obj.type != 'MESH':
return{'CANCELLED'}
if not arm.utils.check_saved(self):
return {"CANCELLED"}
if not arm.utils.check_sdkpath(self):
return {"CANCELLED"}
depsgraph = bpy.context.evaluated_depsgraph_get()
armature = obj.find_armature()
apply_modifiers = not armature
obj_eval = obj.evaluated_get(depsgraph) if apply_modifiers else obj
export_mesh = obj_eval.to_mesh()
2019-08-07 17:02:36 +02:00
# TODO: build tilecache here
print("Started visualization generation")
2019-08-07 17:02:36 +02:00
# For visualization
nav_full_path = arm.utils.get_fp_build() + '/compiled/Assets/navigation'
if not os.path.exists(nav_full_path):
os.makedirs(nav_full_path)
nav_mesh_name = 'nav_' + obj_eval.data.name
2019-08-07 17:02:36 +02:00
mesh_path = nav_full_path + '/' + nav_mesh_name + '.obj'
2019-09-11 20:58:53 +02:00
2019-08-07 17:02:36 +02:00
with open(mesh_path, 'w') as f:
for v in export_mesh.vertices:
f.write("v %.4f " % (v.co[0] * obj_eval.scale.x))
f.write("%.4f " % (v.co[2] * obj_eval.scale.z))
f.write("%.4f\n" % (v.co[1] * obj_eval.scale.y)) # Flipped
for p in export_mesh.polygons:
2019-08-07 17:02:36 +02:00
f.write("f")
for i in reversed(p.vertices): # Flipped normals
f.write(" %d" % (i + 1))
f.write("\n")
2019-09-11 20:58:53 +02:00
2019-08-07 17:02:36 +02:00
buildnavjs_path = arm.utils.get_sdk_path() + '/lib/haxerecast/buildnavjs'
# append config values
nav_config = {}
for trait in obj.arm_traitlist:
2019-08-07 17:02:36 +02:00
# check if trait is navmesh here
if trait.arm_traitpropslist and trait.class_name_prop == 'NavMesh':
for prop in trait.arm_traitpropslist: # Append props
name = prop.name
2020-02-22 17:57:29 +01:00
value = prop.get_value()
2019-08-07 17:02:36 +02:00
nav_config[name] = value
nav_config_json = json.dumps(nav_config)
args = [arm.utils.get_node_path(), buildnavjs_path, nav_mesh_name, nav_config_json]
proc = subprocess.Popen(args, cwd=nav_full_path)
2019-08-07 17:02:36 +02:00
proc.wait()
navmesh = bpy.ops.import_scene.obj(filepath=mesh_path)
navmesh = bpy.context.selected_objects[0]
navmesh.name = nav_mesh_name
navmesh.rotation_euler = (0, 0, 0)
navmesh.location = (obj.location.x, obj.location.y, obj.location.z)
2019-08-07 17:02:36 +02:00
navmesh.arm_export = False
2019-09-11 20:58:53 +02:00
2019-08-07 17:02:36 +02:00
bpy.context.view_layer.objects.active = navmesh
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.remove_doubles()
bpy.ops.object.editmode_toggle()
obj_eval.to_mesh_clear()
print("Finished visualization generation")
2019-08-07 17:02:36 +02:00
return{'FINISHED'}
2017-08-21 12:17:55 +02:00
class ArmEditCanvasButton(bpy.types.Operator):
2017-05-26 16:05:14 +02:00
bl_idname = 'arm.edit_canvas'
bl_label = 'Edit Canvas'
bl_description = 'Edit UI Canvas'
bl_options = {'INTERNAL'}
2017-08-26 18:41:36 +02:00
2018-12-18 23:48:38 +01:00
is_object: BoolProperty(name="", description="A name for this item", default=False)
2019-06-23 12:56:32 +02:00
2017-05-26 16:05:14 +02:00
def execute(self, context):
2017-08-27 12:50:09 +02:00
if self.is_object:
obj = bpy.context.object
else:
obj = bpy.context.scene
2017-05-26 16:05:14 +02:00
project_path = arm.utils.get_fp()
2017-08-27 12:50:09 +02:00
item = obj.arm_traitlist[obj.arm_traitlist_index]
2017-05-26 16:05:14 +02:00
canvas_path = project_path + '/Bundled/canvas/' + item.canvas_name_prop + '.json'
sdk_path = arm.utils.get_sdk_path()
2019-07-15 09:21:23 +02:00
ext = 'd3d11' if arm.utils.get_os() == 'win' else 'opengl'
armory2d_path = sdk_path + '/lib/armory_tools/armory2d/' + ext
krom_location, krom_path = arm.utils.krom_paths()
2017-07-25 17:37:46 +02:00
os.chdir(krom_location)
2018-07-02 10:29:16 +02:00
cpath = canvas_path.replace('\\', '/')
uiscale = str(arm.utils.get_ui_scale())
cmd = [krom_path, armory2d_path, armory2d_path, cpath, uiscale]
if arm.utils.get_os() == 'win':
cmd.append('--consolepid')
cmd.append(str(os.getpid()))
subprocess.Popen(cmd)
2017-05-26 16:05:14 +02:00
return{'FINISHED'}
2017-08-21 12:17:55 +02:00
class ArmNewScriptDialog(bpy.types.Operator):
2016-07-10 00:51:39 +02:00
bl_idname = "arm.new_script"
bl_label = "New Script"
bl_description = 'Create a blank script'
bl_options = {'INTERNAL'}
2019-06-23 16:58:06 +02:00
is_object: BoolProperty(name="Object trait", description="Is this an object trait?", default=False)
class_name: StringProperty(name="Name", description="The class name")
2016-07-10 00:51:39 +02:00
def execute(self, context):
2017-08-27 12:50:09 +02:00
if self.is_object:
obj = bpy.context.object
else:
obj = bpy.context.scene
2016-07-10 00:51:39 +02:00
self.class_name = self.class_name.replace(' ', '')
write_data.write_traithx(self.class_name)
2017-03-15 12:30:14 +01:00
arm.utils.fetch_script_names()
2019-06-23 12:56:32 +02:00
item = obj.arm_traitlist[obj.arm_traitlist_index]
item.class_name_prop = self.class_name
2016-07-10 00:51:39 +02:00
return {'FINISHED'}
2019-06-23 12:56:32 +02:00
2016-07-10 00:51:39 +02:00
def invoke(self, context, event):
2017-03-15 12:30:14 +01:00
if not arm.utils.check_saved(self):
2016-12-08 14:38:04 +01:00
return {'CANCELLED'}
2016-07-10 00:51:39 +02:00
self.class_name = 'MyTrait'
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
self.layout.prop(self, "class_name")
2017-08-21 12:17:55 +02:00
class ArmNewCanvasDialog(bpy.types.Operator):
2017-05-26 16:05:14 +02:00
bl_idname = "arm.new_canvas"
bl_label = "New Canvas"
bl_description = 'Create a blank canvas'
bl_options = {'INTERNAL'}
2019-06-23 12:56:32 +02:00
2019-06-23 16:58:06 +02:00
is_object: BoolProperty(name="Object trait", description="Is this an object trait?", default=False)
canvas_name: StringProperty(name="Name", description="The canvas name")
2019-06-23 12:56:32 +02:00
2017-05-26 16:05:14 +02:00
def execute(self, context):
2017-08-27 12:50:09 +02:00
if self.is_object:
obj = bpy.context.object
else:
obj = bpy.context.scene
2017-05-26 16:05:14 +02:00
self.canvas_name = self.canvas_name.replace(' ', '')
write_data.write_canvasjson(self.canvas_name)
arm.utils.fetch_script_names()
2020-08-04 14:30:05 +02:00
# Todo: create new trait item when called from operator search
# menu, then remove 'INTERNAL' from bl_options
2019-06-23 12:56:32 +02:00
item = obj.arm_traitlist[obj.arm_traitlist_index]
item.canvas_name_prop = self.canvas_name
2017-05-26 16:05:14 +02:00
return {'FINISHED'}
2019-06-23 12:56:32 +02:00
2017-05-26 16:05:14 +02:00
def invoke(self, context, event):
if not arm.utils.check_saved(self):
return {'CANCELLED'}
self.canvas_name = 'MyCanvas'
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
self.layout.prop(self, "canvas_name")
2018-04-15 11:55:42 +02:00
class ArmNewWasmButton(bpy.types.Operator):
"""Create new WebAssembly module"""
2018-04-15 11:55:42 +02:00
bl_idname = 'arm.new_wasm'
bl_label = 'New Module'
2019-06-23 12:56:32 +02:00
2018-04-15 11:55:42 +02:00
def execute(self, context):
webbrowser.open('https://webassembly.studio/')
return{'FINISHED'}
2017-08-21 12:17:55 +02:00
class ArmRefreshScriptsButton(bpy.types.Operator):
"""Fetch all script names"""
2017-08-08 19:56:47 +02:00
bl_idname = 'arm.refresh_scripts'
bl_label = 'Refresh Traits'
2019-06-23 12:56:32 +02:00
2016-07-10 00:51:39 +02:00
def execute(self, context):
2017-03-15 12:30:14 +01:00
arm.utils.fetch_bundled_script_names()
2017-12-11 19:00:32 +01:00
arm.utils.fetch_bundled_trait_props()
2017-03-15 12:30:14 +01:00
arm.utils.fetch_script_names()
2017-07-24 02:27:22 +02:00
arm.utils.fetch_trait_props()
2018-04-15 11:55:42 +02:00
arm.utils.fetch_wasm_names()
2016-07-10 00:51:39 +02:00
return{'FINISHED'}
2015-10-30 13:23:09 +01:00
2017-08-21 12:17:55 +02:00
class ArmRefreshCanvasListButton(bpy.types.Operator):
"""Fetch all canvas names"""
2017-05-26 16:05:14 +02:00
bl_idname = 'arm.refresh_canvas_list'
bl_label = 'Refresh Canvas Traits'
2019-06-23 12:56:32 +02:00
2017-05-26 16:05:14 +02:00
def execute(self, context):
arm.utils.fetch_script_names()
return{'FINISHED'}
class ARM_PT_TraitPanel(bpy.types.Panel):
2016-07-10 00:51:39 +02:00
bl_label = "Armory Traits"
2015-10-30 13:23:09 +01:00
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "object"
2019-06-23 12:56:32 +02:00
2015-10-30 13:23:09 +01:00
def draw(self, context):
layout = self.layout
2018-12-19 13:33:17 +01:00
layout.use_property_split = True
2018-12-19 20:10:34 +01:00
layout.use_property_decorate = False
2015-10-30 13:23:09 +01:00
obj = bpy.context.object
2017-08-26 18:41:36 +02:00
draw_traits(layout, obj, is_object=True)
2015-10-30 13:23:09 +01:00
class ARM_PT_SceneTraitPanel(bpy.types.Panel):
bl_label = "Armory Scene Traits"
2017-08-26 18:41:36 +02:00
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "scene"
2019-06-23 12:56:32 +02:00
2017-08-26 18:41:36 +02:00
def draw(self, context):
2018-12-19 13:33:17 +01:00
layout = self.layout
layout.use_property_split = True
2018-12-19 20:10:34 +01:00
layout.use_property_decorate = False
2017-08-26 18:41:36 +02:00
obj = bpy.context.scene
draw_traits(layout, obj, is_object=False)
def draw_traits(layout, obj, is_object):
rows = 2
if len(obj.arm_traitlist) > 1:
rows = 4
2019-06-23 12:56:32 +02:00
2017-08-26 18:41:36 +02:00
row = layout.row()
row.template_list("ARM_UL_TraitList", "The_List", obj, "arm_traitlist", obj, "arm_traitlist_index", rows=rows)
2019-06-23 12:56:32 +02:00
2017-08-26 18:41:36 +02:00
col = row.column(align=True)
2018-12-18 23:48:38 +01:00
op = col.operator("arm_traitlist.new_item", icon='ADD', text="")
2017-08-26 18:41:36 +02:00
op.is_object = is_object
2017-09-06 13:28:59 +02:00
if is_object:
2018-12-18 23:48:38 +01:00
op = col.operator("arm_traitlist.delete_item", icon='REMOVE', text="")#.all = False
2017-09-06 13:28:59 +02:00
else:
2018-12-18 23:48:38 +01:00
op = col.operator("arm_traitlist.delete_item_scene", icon='REMOVE', text="")#.all = False
2017-08-26 18:41:36 +02:00
op.is_object = is_object
if len(obj.arm_traitlist) > 1:
col.separator()
2017-09-01 15:24:46 +02:00
op = col.operator("arm_traitlist.move_item", icon='TRIA_UP', text="")
op.direction = 'UP'
2017-08-26 18:41:36 +02:00
op.is_object = is_object
2017-09-01 15:24:46 +02:00
op = col.operator("arm_traitlist.move_item", icon='TRIA_DOWN', text="")
op.direction = 'DOWN'
2017-08-26 18:41:36 +02:00
op.is_object = is_object
if obj.arm_traitlist_index >= 0 and len(obj.arm_traitlist) > 0:
item = obj.arm_traitlist[obj.arm_traitlist_index]
2020-04-10 20:44:35 +02:00
if item.type_prop == 'Haxe Script' or item.type_prop == 'Bundled Script':
2017-08-26 18:41:36 +02:00
if item.type_prop == 'Haxe Script':
2017-08-09 00:14:30 +02:00
row = layout.row(align=True)
row.alignment = 'EXPAND'
column = row.column(align=True)
column.alignment = 'EXPAND'
2017-08-26 18:41:36 +02:00
if item.class_name_prop == '':
2017-08-09 00:14:30 +02:00
column.enabled = False
2018-03-15 23:24:48 +01:00
op = column.operator("arm.edit_script", icon="FILE_SCRIPT")
2017-08-27 12:50:09 +02:00
op.is_object = is_object
op = row.operator("arm.new_script")
op.is_object = is_object
op = row.operator("arm.refresh_scripts", text="Refresh")
2017-08-26 18:41:36 +02:00
else: # Bundled
2019-08-07 17:02:36 +02:00
if item.class_name_prop == 'NavMesh':
row = layout.row(align=True)
row.alignment = 'EXPAND'
op = layout.operator("arm.generate_navmesh")
2017-12-11 19:00:32 +01:00
row = layout.row(align=True)
row.alignment = 'EXPAND'
column = row.column(align=True)
column.alignment = 'EXPAND'
2019-08-07 17:02:36 +02:00
if not item.class_name_prop == 'NavMesh':
op = column.operator("arm.edit_bundled_script", icon="FILE_SCRIPT")
op.is_object = is_object
op = row.operator("arm.refresh_scripts", text="Refresh")
2019-06-23 12:56:32 +02:00
2020-04-10 20:44:35 +02:00
# Default props
item.name = item.class_name_prop
row = layout.row()
if item.type_prop == 'Haxe Script':
row.prop_search(item, "class_name_prop", bpy.data.worlds['Arm'], "arm_scripts_list", text="Class")
else:
# Bundled scripts not yet fetched
if not bpy.data.worlds['Arm'].arm_bundled_scripts_list:
arm.utils.fetch_bundled_script_names()
row.prop_search(item, "class_name_prop", bpy.data.worlds['Arm'], "arm_bundled_scripts_list", text="Class")
2017-09-20 14:45:09 +02:00
elif item.type_prop == 'WebAssembly':
2018-04-15 11:55:42 +02:00
item.name = item.webassembly_prop
row = layout.row()
2018-09-05 10:20:02 +02:00
row.prop_search(item, "webassembly_prop", bpy.data.worlds['Arm'], "arm_wasm_list", text="Module")
2018-04-15 11:55:42 +02:00
row = layout.row(align=True)
row.alignment = 'EXPAND'
column = row.column(align=True)
column.alignment = 'EXPAND'
if item.class_name_prop == '':
column.enabled = False
# op = column.operator("arm.edit_script", icon="FILE_SCRIPT")
# op.is_object = is_object
op = row.operator("arm.new_wasm")
# op.is_object = is_object
op = row.operator("arm.refresh_scripts", text="Refresh")
2017-05-26 16:05:14 +02:00
2017-08-26 18:41:36 +02:00
elif item.type_prop == 'UI Canvas':
item.name = item.canvas_name_prop
2019-06-23 12:56:32 +02:00
2017-08-26 18:41:36 +02:00
row = layout.row(align=True)
row.alignment = 'EXPAND'
column = row.column(align=True)
column.alignment = 'EXPAND'
if item.canvas_name_prop == '':
column.enabled = False
2018-03-15 23:24:48 +01:00
op = column.operator("arm.edit_canvas", icon="FILE_SCRIPT")
2017-08-27 12:50:09 +02:00
op.is_object = is_object
op = row.operator("arm.new_canvas")
op.is_object = is_object
op = row.operator("arm.refresh_canvas_list", text="Refresh")
2017-08-26 18:41:36 +02:00
2020-04-10 20:44:35 +02:00
row = layout.row()
row.prop_search(item, "canvas_name_prop", bpy.data.worlds['Arm'], "arm_canvas_list", text="Canvas")
2017-08-26 18:41:36 +02:00
elif item.type_prop == 'Logic Nodes':
row = layout.row()
row.prop_search(item, "node_tree_prop", bpy.data, "node_groups", text="Tree")
2015-10-30 13:23:09 +01:00
2020-04-10 20:44:35 +02:00
if item.type_prop == 'Haxe Script' or item.type_prop == 'Bundled Script':
# Props
if item.arm_traitpropslist:
layout.label(text="Trait Properties:")
if item.arm_traitpropswarnings:
box = layout.box()
box.label(text=f"Warnings ({len(item.arm_traitpropswarnings)}):", icon="ERROR")
for warning in item.arm_traitpropswarnings:
box.label(text=warning.warning)
propsrow = layout.row()
propsrows = max(len(item.arm_traitpropslist), 6)
row = layout.row()
row.template_list("ARM_UL_PropList", "The_List", item, "arm_traitpropslist", item, "arm_traitpropslist_index", rows=propsrows)
2018-11-13 14:51:02 +01:00
def register():
global icons_dict
2017-08-21 12:17:55 +02:00
bpy.utils.register_class(ArmTraitListItem)
bpy.utils.register_class(ARM_UL_TraitList)
2017-08-21 12:17:55 +02:00
bpy.utils.register_class(ArmTraitListNewItem)
bpy.utils.register_class(ArmTraitListDeleteItem)
2017-09-06 13:28:59 +02:00
bpy.utils.register_class(ArmTraitListDeleteItemScene)
2017-08-21 12:17:55 +02:00
bpy.utils.register_class(ArmTraitListMoveItem)
bpy.utils.register_class(ArmEditScriptButton)
bpy.utils.register_class(ArmEditBundledScriptButton)
2019-08-07 17:02:36 +02:00
bpy.utils.register_class(ArmoryGenerateNavmeshButton)
2017-08-21 12:17:55 +02:00
bpy.utils.register_class(ArmEditCanvasButton)
bpy.utils.register_class(ArmNewScriptDialog)
bpy.utils.register_class(ArmNewCanvasDialog)
2018-04-15 11:55:42 +02:00
bpy.utils.register_class(ArmNewWasmButton)
2017-08-21 12:17:55 +02:00
bpy.utils.register_class(ArmRefreshScriptsButton)
bpy.utils.register_class(ArmRefreshCanvasListButton)
bpy.utils.register_class(ARM_PT_TraitPanel)
bpy.utils.register_class(ARM_PT_SceneTraitPanel)
2018-12-18 23:48:38 +01:00
bpy.types.Object.arm_traitlist = CollectionProperty(type=ArmTraitListItem)
bpy.types.Object.arm_traitlist_index = IntProperty(name="Index for arm_traitlist", default=0)
bpy.types.Scene.arm_traitlist = CollectionProperty(type=ArmTraitListItem)
bpy.types.Scene.arm_traitlist_index = IntProperty(name="Index for arm_traitlist", default=0)
2018-11-13 14:51:02 +01:00
icons_dict = bpy.utils.previews.new()
icons_dir = os.path.join(os.path.dirname(__file__), "custom_icons")
icons_dict.load("haxe", os.path.join(icons_dir, "haxe.png"), 'IMAGE')
icons_dict.load("wasm", os.path.join(icons_dir, "wasm.png"), 'IMAGE')
2017-03-15 12:30:14 +01:00
2015-12-07 20:04:23 +01:00
def unregister():
2018-11-13 14:51:02 +01:00
global icons_dict
2017-08-21 12:17:55 +02:00
bpy.utils.unregister_class(ArmTraitListItem)
bpy.utils.unregister_class(ARM_UL_TraitList)
2017-08-21 12:17:55 +02:00
bpy.utils.unregister_class(ArmTraitListNewItem)
bpy.utils.unregister_class(ArmTraitListDeleteItem)
2017-09-06 13:28:59 +02:00
bpy.utils.unregister_class(ArmTraitListDeleteItemScene)
2017-08-21 12:17:55 +02:00
bpy.utils.unregister_class(ArmTraitListMoveItem)
bpy.utils.unregister_class(ArmEditScriptButton)
bpy.utils.unregister_class(ArmEditBundledScriptButton)
2019-08-07 17:02:36 +02:00
bpy.utils.unregister_class(ArmoryGenerateNavmeshButton)
2017-08-21 12:17:55 +02:00
bpy.utils.unregister_class(ArmEditCanvasButton)
bpy.utils.unregister_class(ArmNewScriptDialog)
bpy.utils.unregister_class(ArmNewCanvasDialog)
2018-04-15 11:55:42 +02:00
bpy.utils.unregister_class(ArmNewWasmButton)
2017-08-21 12:17:55 +02:00
bpy.utils.unregister_class(ArmRefreshScriptsButton)
bpy.utils.unregister_class(ArmRefreshCanvasListButton)
bpy.utils.unregister_class(ARM_PT_TraitPanel)
bpy.utils.unregister_class(ARM_PT_SceneTraitPanel)
2018-11-13 14:51:02 +01:00
bpy.utils.previews.remove(icons_dict)