Trait props

This commit is contained in:
Lubos Lenco 2017-07-24 02:27:22 +02:00
parent ed99d0490b
commit ea4e1fc008
6 changed files with 118 additions and 2 deletions

View file

@ -2814,6 +2814,11 @@ class ArmoryExporter:
x['parameters'] = []
for pt in t.my_paramstraitlist: # Append parameters
x['parameters'].append(pt.name)
if len(t.my_propstraitlist) > 0:
x['props'] = []
for pt in t.my_propstraitlist: # Append props
x['props'].append(pt.name)
x['props'].append(pt.value)
o['traits'].append(x)
# Rigid body trait

View file

@ -403,6 +403,7 @@ def play_project(in_viewport, is_render=False):
for fn in glob.iglob(os.path.join(script_path, '**', '*.hx'), recursive=True):
mtime = os.path.getmtime(fn)
if scripts_mtime < mtime:
arm.utils.fetch_script_props(fn) # Trait props
fn = fn.split('Sources/')[1]
fn = fn[:-3] #.hx
fn = fn.replace('/', '.')
@ -411,6 +412,8 @@ def play_project(in_viewport, is_render=False):
if new_mtime < mtime:
new_mtime = mtime
scripts_mtime = new_mtime
if len(state.mod_scripts) > 0: # Trait props
arm.utils.fetch_trait_props()
# New compile requred - traits changed
if wrd.arm_recompile:

View file

@ -6,6 +6,7 @@ import json
from bpy.types import Menu, Panel, UIList
from bpy.props import *
from arm.props_traits_params import *
from arm.props_traits_props import *
import arm.utils
import arm.write_data as write_data
@ -53,6 +54,9 @@ class ListTraitItem(bpy.types.PropertyGroup):
my_paramstraitlist = bpy.props.CollectionProperty(type=ListParamsTraitItem)
paramstraitlist_index = bpy.props.IntProperty(name="Index for my_list", default=0)
my_propstraitlist = bpy.props.CollectionProperty(type=ListPropsTraitItem)
propstraitlist_index = bpy.props.IntProperty(name="Index for my_list", default=0)
class MY_UL_TraitList(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
# We could write some code to decide which icon to use here...
@ -78,7 +82,7 @@ class LIST_OT_TraitNewItem(bpy.types.Operator):
bl_label = "Add a new item"
def execute(self, context):
bpy.context.object.my_traitlist.add()
trait = bpy.context.object.my_traitlist.add()
bpy.context.object.traitlist_index = len(bpy.context.object.my_traitlist) - 1
return{'FINISHED'}
@ -284,6 +288,7 @@ class ArmoryRefreshScriptsListButton(bpy.types.Operator):
def execute(self, context):
arm.utils.fetch_bundled_script_names()
arm.utils.fetch_script_names()
arm.utils.fetch_trait_props()
return{'FINISHED'}
class ArmoryRefreshCanvasListButton(bpy.types.Operator):
@ -338,6 +343,16 @@ class ToolsTraitsPanel(bpy.types.Panel):
row.prop_search(item, "class_name_prop", bpy.data.worlds['Arm'], "scripts_list", "Class")
else:
row.prop_search(item, "class_name_prop", bpy.data.worlds['Arm'], "bundled_scripts_list", "Class")
# Props
if len(item.my_propstraitlist) > 0:
propsrow = layout.row()
propsrows = 2
if len(item.my_propstraitlist) > 2:
propsrows = 4
row = layout.row()
row.template_list("MY_UL_PropsTraitList", "The_List", item, "my_propstraitlist", item, "propstraitlist_index", rows=propsrows)
# Params
layout.label("Parameters")
paramsrow = layout.row()

View file

@ -0,0 +1,43 @@
import shutil
import bpy
import os
import json
from bpy.types import Menu, Panel, UIList
from bpy.props import *
from arm.utils import to_hex
class ListPropsTraitItem(bpy.types.PropertyGroup):
# Group of properties representing an item in the list
name = bpy.props.StringProperty(
name="Name",
description="A name for this item",
default="Untitled")
value = bpy.props.StringProperty(
name="Value",
description="A name for this item",
default="")
class MY_UL_PropsTraitList(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
# We could write some code to decide which icon to use here...
custom_icon = 'OBJECT_DATAMODE'
# Make sure your code supports all 3 layout types
if self.layout_type in {'DEFAULT', 'COMPACT'}:
#layout.prop(item, "enabled_prop")
#layout.label(item.name, icon = custom_icon)
# layout.label(item.name)
layout.prop(item, "value", text=item.name, emboss=False, icon=custom_icon)
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label("", icon = custom_icon)
def register():
bpy.utils.register_class(ListPropsTraitItem)
bpy.utils.register_class(MY_UL_PropsTraitList)
def unregister():
bpy.utils.unregister_class(ListPropsTraitItem)
bpy.utils.unregister_class(MY_UL_PropsTraitList)

View file

@ -146,6 +146,28 @@ def fetch_bundled_script_names():
for file in glob.glob('*.hx'):
wrd.bundled_scripts_list.add().name = file.rsplit('.')[0]
script_props = {}
def fetch_script_props(file):
with open(file) as f:
if '/' in file:
file = file.split('/')[-1]
if '\\' in file:
file = file.split('\\')[-1]
name = file.rsplit('.')[0]
script_props[name] = []
lines = f.read().splitlines()
readprop = False
for l in lines:
if readprop:
p = l.split('var ')[1]
if ':' in p:
p = p.split(':')[0].strip()
script_props[name].append(p)
elif '=' in p:
p = p.split('=')[0].strip()
script_props[name].append(p)
readprop = l.strip().startswith('@prop')
def fetch_script_names():
if bpy.data.filepath == "":
return
@ -156,7 +178,10 @@ def fetch_script_names():
if os.path.isdir(sources_path):
os.chdir(sources_path)
for file in glob.glob('*.hx'):
wrd.scripts_list.add().name = file.rsplit('.')[0]
name = file.rsplit('.')[0]
wrd.scripts_list.add().name = name
fetch_script_props(file)
# Canvas
wrd.canvas_list.clear()
canvas_path = get_fp() + '/Bundled/canvas'
@ -166,6 +191,28 @@ def fetch_script_names():
wrd.canvas_list.add().name = file.rsplit('.')[0]
os.chdir(get_fp())
def fetch_trait_props():
for o in bpy.data.objects:
for item in o.my_traitlist:
if item.name not in script_props:
continue
props = script_props[item.name]
# Remove old props
for i in range(len(item.my_propstraitlist) - 1, -1, -1):
ip = item.my_propstraitlist[i]
if ip.name not in props:
item.my_propstraitlist.remove(i)
# Add new props
for p in props:
found = False
for ip in item.my_propstraitlist:
if ip.name == p:
found = True
break
if not found:
prop = item.my_propstraitlist.add()
prop.name = p
def to_hex(val):
return '#%02x%02x%02x%02x' % (int(val[3] * 255), int(val[0] * 255), int(val[1] * 255), int(val[2] * 255))

View file

@ -5,6 +5,7 @@ import arm.props_traits_action
import arm.props_traits_clip
import arm.props_traits_library
import arm.props_traits_params
import arm.props_traits_props
import arm.props_traits
import arm.props_lod
import arm.props_navigation
@ -34,6 +35,7 @@ def register():
arm.nodes_renderpath.register()
arm.make_renderer.register()
arm.props_traits_params.register()
arm.props_traits_props.register()
arm.props_traits.register()
arm.props_lod.register()
arm.props_navigation.register()
@ -52,6 +54,7 @@ def unregister():
arm.make_renderer.unregister()
arm.nodes_renderpath.unregister()
arm.props_traits_params.unregister()
arm.props_traits_props.unregister()
arm.props_traits.unregister()
arm.props_lod.unregister()
arm.props_navigation.unregister()