UI/PropertyDrop.py

UI/PropertyDrop.py
1 # Copyright 2009 Autodesk, Inc. All rights reserved.
2 # Use of this software is subject to the terms of the Autodesk license agreement
3 # provided at the time of installation or download, or which otherwise accompanies
4 # this software in either electronic or hard copy form.
5 #
6 # Script description:
7 # Create a tool that shows how drag and drop works.
8 # Allow for a model to be dropped in a container. From that model show its property list and allow edition/selection
9 # of all the properties.
10 #
11 # Topic: FBVisualContainer, FBSceneChangeType, FBDragAndDropState, FBPropertyFlag, FBEditPropertyModern, FBEditProperty
12 #
13 
14 from pyfbsdk import *
15 from pyfbsdk_additions import *
16 
17 def SetupPropertyList(model):
18  tool.container.Items.removeAll()
19  tool.list.Items.removeAll()
20  tool.prop_list = []
21 
22  tool.prop.Property = None
23  tool.prop_modern.Property = None
24 
25  tool.model = model
26 
27  if model:
28  tool.container.Items.append(model.Name)
29  tool.list.Items.append("<Select Property>")
30  tool.prop_list.append(None)
31  for p in model.PropertyList:
32  if p and p.IsInternal() and not p.GetPropertyFlag(FBPropertyFlag.kFBPropertyFlagHideProperty):
33  tool.list.Items.append(p.Name)
34  tool.prop_list.append(p)
35  tool.list.ItemIndex = 0
36  PropertyListChanged(tool.list, None)
37 
38 
39 def EventContainerDblClick(control, event):
40  SetupPropertyList(None)
41 
42 def EventContainerDragAndDrop(control, event):
43  if event.State == FBDragAndDropState.kFBDragAndDropDrag:
44  event.Accept()
45  elif event.State == FBDragAndDropState.kFBDragAndDropDrop:
46  SetupPropertyList( event.Components[0] )
47 
48 def PropertyListChanged(control, event):
49  tool.prop.Property = tool.prop_list[control.ItemIndex]
50  tool.prop_modern.Property = tool.prop_list[control.ItemIndex]
51 
52 def PrevProperty(control, event):
53  if tool.list.ItemIndex - 1 < 0:
54  tool.list.ItemIndex = len(tool.list.Items)-1
55  else:
56  tool.list.ItemIndex = tool.list.ItemIndex - 1
57  PropertyListChanged(tool.list, None)
58 
59 def NextProperty(control, event):
60  if tool.list.ItemIndex + 1 >= len(tool.list.Items):
61  tool.list.ItemIndex = 0
62  else:
63  tool.list.ItemIndex = tool.list.ItemIndex + 1
64  PropertyListChanged(tool.list, None)
65 
66 def SceneChanged(scene, event):
67  if len(tool.container.Items) != 0 and \
68  event.Type == FBSceneChangeType.kFBSceneChangeDetach and \
69  event.ChildComponent == tool.model:
70  SetupPropertyList(None)
71 
72 
73 def PopulateLayout(mainLyt):
74  x = FBAddRegionParam(0,FBAttachType.kFBAttachLeft,"")
75  y = FBAddRegionParam(0,FBAttachType.kFBAttachTop,"")
76  w = FBAddRegionParam(0,FBAttachType.kFBAttachRight,"")
77  h = FBAddRegionParam(25,FBAttachType.kFBAttachBottom,"")
78  mainLyt.AddRegion("main","main", x, y, w, h)
79  vlyt = FBVBoxLayout()
80  mainLyt.SetControl("main",vlyt)
81 
82  l = FBLabel()
83  l.Caption = "Drag and drop a model into the container. Double click to clear."
84  vlyt.Add(l,30)
85 
86  tool.model = None
87  tool.container = FBVisualContainer()
88  tool.container.OnDragAndDrop.Add(EventContainerDragAndDrop)
89  tool.container.OnDblClick.Add(EventContainerDblClick)
90  vlyt.Add(tool.container,30)
91 
92  hlyt = FBHBoxLayout()
93  tool.list = FBList()
94  tool.list.OnChange.Add(PropertyListChanged)
95  hlyt.AddRelative(tool.list)
96 
97  prev = FBButton()
98  prev.OnClick.Add(PrevProperty)
99  prev.Caption = "<"
100  hlyt.Add(prev, 30)
101 
102  next = FBButton()
103  next.OnClick.Add(NextProperty)
104  next.Caption = ">"
105  hlyt.Add(next, 30)
106 
107  vlyt.Add(hlyt, 30)
108 
109  tool.prop = FBEditProperty()
110  vlyt.Add(tool.prop,30)
111 
112  tool.prop_modern = FBEditPropertyModern()
113  vlyt.Add(tool.prop_modern,30)
114 
115  # Register for scene event
116  FBSystem().Scene.OnChange.Add(SceneChanged)
117 
118  # register when this tool is destroyed.
119  tool.OnUnbind.Add(OnToolDestroy)
120 
121 
122 def OnToolDestroy(control,event):
123  # Important: each time we run this script we need to remove
124  # the SceneChanged from the Scene else they will accumulate
125  FBSystem().Scene.OnChange.Remove(SceneChanged)
126 
127 def CreateTool():
128  global tool
129 
130  tool = FBCreateUniqueTool("Property Example")
131  tool.StartSizeX = 400
132  tool.StartSizeY = 200
133  PopulateLayout(tool)
134  ShowTool(tool)
135 
136 
137 CreateTool()