Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

import director.vtkAll as vtk 

import director.objectmodel as om 

from director import lcmUtils 

 

 

#------ Individual Collections To Be Rendered-------------------- 

class CollectionInfo(): 

    def __init__(self, collectionId, collectionName, collectionType, collectionShow): 

        self.id =   collectionId 

        self.name = collectionName 

        self.type = collectionType 

        self.show = collectionShow 

 

 

class CollectionInfoObject(om.ObjectModelItem): 

 

    def __init__(self, collectionInfo, actor): 

 

        om.ObjectModelItem.__init__(self, collectionInfo.name, om.Icons.Collections) 

 

        self.actor = actor 

        self.collectionInfo = collectionInfo 

        #self.actor.SetUseBounds(False) 

        self.addProperty('Visible', actor.GetVisibility()) 

        self.views = [] 

 

        self.collectionsObject = self.getDrawObject("COLLECTIONS") 

 

    def _onPropertyChanged(self, propertySet, propertyName): 

        om.ObjectModelItem._onPropertyChanged(self, propertySet, propertyName) 

 

        if propertyName == 'Visible': 

            makeVisible = self.getProperty(propertyName) 

            self.actor.SetVisibility(makeVisible) 

            self.collectionsObject.setCollectionEnable(self.collectionInfo.id, makeVisible) 

            self.collectionsObject.renderAllViews() 

 

 

    def getDrawObject(self, name): 

        parent = om.getOrCreateContainer('Collections') 

        return parent.findChild(name) 

 

    def addToView(self, view): 

        if view in self.views: 

            return 

        self.views.append(view) 

        view.renderer().AddActor(self.actor) 

        view.render() 

 

    def onRemoveFromObjectModel(self): 

        self.collectionsObject.removeIdFromCollections(self.collectionInfo.id) 

        self.collectionsObject.getCollectionsInfo() 

        self.collectionsObject.renderAllViews() 

 

 

class CollectionsObject(om.ObjectModelItem): 

 

    def __init__(self, name, actor): 

 

        om.ObjectModelItem.__init__(self, name, om.Icons.Collections) 

 

 

        self.actor = actor 

        self.actor.SetUseBounds(False) 

        self.addProperty('Visible', actor.GetVisibility()) 

        self.addProperty('Start', 0.0, attributes=om.PropertyAttributes(decimals=2, minimum=0, maximum=1.0, singleStep=0.01)) 

        self.addProperty('End', 1.0, attributes=om.PropertyAttributes(decimals=2, minimum=0, maximum=1.0, singleStep=0.01)) 

 

        self.addProperty('Points Alpha', 0.8, attributes=om.PropertyAttributes(decimals=2, minimum=0, maximum=1.0, singleStep=0.1)) 

        self.addProperty('Point Width', 5.0, attributes=om.PropertyAttributes(decimals=0, minimum=1.0, maximum=30.0, singleStep=1)) 

        self.addProperty('Pose Width', 1.0, attributes=om.PropertyAttributes(decimals=1, minimum=0.1, maximum=30.0, singleStep=0.1)) 

        self.addProperty('Color Poses', False) 

 

        self.addProperty('Fill Scans', False) 

        self.addProperty('Color by Time', False) 

        self.addProperty('Elevation by Time', False) 

        self.addProperty('Elevation by Collection', False) 

        self.addProperty('Max Elevation', 0.0, attributes=om.PropertyAttributes(decimals=1, minimum=0, maximum=100.0, singleStep=0.1)) 

 

        self.views = [] 

        self.collectionInfos = None 

 

    def _onPropertyChanged(self, propertySet, propertyName): 

        om.ObjectModelItem._onPropertyChanged(self, propertySet, propertyName) 

 

        if propertyName == 'Visible': 

            self.actor.SetVisibility(self.getProperty(propertyName)) 

            makeVisible = self.getProperty(propertyName) 

            parent = om.getOrCreateContainer('COLLECTIONS') 

            for coll in self.children(): 

                coll.setProperty('Visible',makeVisible) 

 

        elif propertyName == 'Start': 

            self.actor.setRangeStart(self.getProperty(propertyName)) 

        elif propertyName == 'End': 

            self.actor.setRangeEnd(self.getProperty(propertyName)) 

        elif propertyName == 'Points Alpha': 

            self.actor.setAlphaPoints(self.getProperty(propertyName)) 

        elif propertyName == 'Fill Scans': 

            self.actor.setFillScans(self.getProperty(propertyName)) 

        elif propertyName == 'Point Width': 

            self.actor.setPointWidth(self.getProperty(propertyName)) 

        elif propertyName == 'Pose Width': 

            self.actor.setPoseWidth(self.getProperty(propertyName)) 

        elif propertyName == 'Color Poses': 

            self.actor.setColorPoses(self.getProperty(propertyName)) 

        elif propertyName == 'Color by Time': 

            self.actor.setColorByTime(self.getProperty(propertyName)) 

        elif propertyName == 'Elevation by Time': 

            self.actor.setElevationByTime(self.getProperty(propertyName)) 

        elif propertyName == 'Elevation by Collection': 

            self.actor.setElevationByCollection(self.getProperty(propertyName)) 

        elif propertyName == 'Max Elevation': 

            self.actor.setMaxElevation(self.getProperty(propertyName)) 

 

        self.renderAllViews() 

 

    def addToView(self, view): 

        if view in self.views: 

            return 

        self.views.append(view) 

        view.renderer().AddActor(self.actor) 

        view.render() 

 

    def renderAllViews(self): 

        for view in self.views: 

            view.render() 

 

    def onRemoveFromObjectModel(self): 

        self.removeFromAllViews() 

 

    def removeFromAllViews(self): 

        for view in list(self.views): 

            self.removeFromView(view) 

        assert len(self.views) == 0 

 

    def removeFromView(self, view): 

        assert view in self.views 

        self.views.remove(view) 

        view.renderer().RemoveActor(self.actor) 

        view.render() 

 

    def getCollectionsInfo(self): 

        numberOfCollections = self.actor.getCollectionsSize() 

 

        self.collectionInfos = [] 

        for i in range(0,numberOfCollections): 

            cId = self.actor.getCollectionsId(i) 

            cName = self.actor.getCollectionsName(i) 

            cType = self.actor.getCollectionsType(i) 

            cShow = self.actor.getCollectionsShow(i) 

            cInfo = CollectionInfo(cId, cName, cType, cShow ) 

            self.collectionInfos.append(cInfo) 

 

    def disableOne(self): 

        self.setCollectionEnable(1,False) 

 

    def removeIdFromCollections(self, cId): 

        self.actor.removeIdFromCollections(cId) 

 

    def setCollectionEnable(self, cId, enable): 

        self.actor.setEnabled(cId, enable) 

 

    def on_obj_collection_data(self, msgBytes): 

        self.actor.on_obj_collection_data(msgBytes.data()) 

        self.getCollectionsInfo() 

        self.renderAllViews() 

 

    def on_link_collection_data(self, msgBytes): 

        self.actor.on_link_collection_data(msgBytes.data()) 

        self.getCollectionsInfo() 

        self.renderAllViews() 

 

    def on_points_collection_data(self, msgBytes): 

        self.actor.on_points_collection_data(msgBytes.data()) 

        self.getCollectionsInfo() 

        self.renderAllViews() 

 

    def on_reset_collections_data(self, msgBytes): 

        self.actor.on_reset_collections_data(msgBytes.data()) 

        self.getCollectionsInfo() 

        self.renderAllViews() 

 

#------ Overall Set of Collections To Be Rendered-------------------- 

managerInstance = None 

 

class CollectionsManager(object): 

 

    def __init__(self, view): 

        self.view = view 

        self.subscriber0 = None 

        self.subscriber1 = None 

        self.subscriber2 = None 

        self.subscriber3 = None 

 

        self.enable() 

 

    def isEnabled(self): 

        return self.subscriber0 is not None 

 

    def setEnabled(self, enabled): 

        if enabled and not self.subscriber0: 

            self.subscriber0 = lcmUtils.addSubscriber('OBJECT_COLLECTION', callback=self.on_obj_collection_data) 

            self.subscriber1 = lcmUtils.addSubscriber('LINK_COLLECTION', callback=self.on_link_collection_data) 

            self.subscriber2 = lcmUtils.addSubscriber('POINTS_COLLECTION', callback=self.on_points_collection_data) 

            self.subscriber3 = lcmUtils.addSubscriber('RESET_COLLECTIONS', callback=self.on_reset_collections_data) 

            self.subscriber0.setNotifyAllMessagesEnabled(True) 

            self.subscriber1.setNotifyAllMessagesEnabled(True) 

            self.subscriber2.setNotifyAllMessagesEnabled(True) 

            self.subscriber3.setNotifyAllMessagesEnabled(True) 

        elif not enabled and self.subscriber0: 

            lcmUtils.removeSubscriber(self.subscriber0) 

            lcmUtils.removeSubscriber(self.subscriber1) 

            lcmUtils.removeSubscriber(self.subscriber2) 

            lcmUtils.removeSubscriber(self.subscriber3) 

            self.subscriber0 = None 

            self.subscriber1 = None 

            self.subscriber2 = None 

            self.subscriber3 = None 

 

    def enable(self): 

        self.setEnabled(True) 

 

    def disable(self): 

        self.setEnabled(False) 

 

    def on_obj_collection_data(self, msgBytes, channel): 

        drawObject = self.getDrawObject("COLLECTIONS") 

        if not drawObject: 

            drawObject = self.addDrawObject("COLLECTIONS", msgBytes) 

        drawObject.on_obj_collection_data(msgBytes) 

        self.addAllObjects() 

 

    def on_link_collection_data(self, msgBytes, channel): 

        drawObject = self.getDrawObject("COLLECTIONS") 

        if not drawObject: 

            drawObject = self.addDrawObject("COLLECTIONS", msgBytes) 

        drawObject.on_link_collection_data(msgBytes) 

        self.addAllObjects() 

 

    def on_points_collection_data(self, msgBytes, channel): 

        drawObject = self.getDrawObject("COLLECTIONS") 

        if not drawObject: 

            drawObject = self.addDrawObject("COLLECTIONS", msgBytes) 

        drawObject.on_points_collection_data(msgBytes) 

        self.addAllObjects() 

 

    def on_reset_collections_data(self, msgBytes, channel): 

        drawObject = self.getDrawObject("COLLECTIONS") 

        if not drawObject: 

            drawObject = self.addDrawObject("COLLECTIONS", msgBytes) 

        drawObject.on_reset_collections_data(msgBytes) 

        self.addAllObjects() 

 

    def getDrawObject(self, name): 

        parent = om.getOrCreateContainer('Collections') 

        return parent.findChild(name) 

 

    def addDrawObject(self, name, msgBytes): 

        actor = vtk.vtkCollections() 

        obj = CollectionsObject(name, actor) 

        om.addToObjectModel(obj, om.getOrCreateContainer('Collections')) 

        obj.addToView(self.view) 

        return obj 

 

    def addAllObjects(self): 

        drawObject = self.getDrawObject("COLLECTIONS") 

        if not drawObject: 

            return 

 

        drawObject.getCollectionsInfo() 

        for coll in drawObject.collectionInfos: 

 

            # If the icon exists, don't re-add it 

            existing = False 

            for existingCollection in drawObject.children(): 

                if coll.id == existingCollection.collectionInfo.id: 

                    existing = True 

                    continue 

            if existing: 

                continue 

 

            actor = vtk.vtkCollections() 

            obj = CollectionInfoObject(coll, actor) 

            om.addToObjectModel(obj, drawObject) 

            obj.addToView(self.view) 

 

 

def init(view): 

    if not hasattr(vtk, 'vtkCollections'): 

        return None 

 

    global managerInstance 

    managerInstance = CollectionsManager(view) 

 

    return managerInstance