Samples/Referencing/MBFileRefDemo.py

Samples/Referencing/MBFileRefDemo.py
1 # Copyright 2012 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 demo how to embed native Qt widgets created by PySide into MoBu framework.
8 #
9 # Topic: FBWidgetHolder, FBTool, FBFileReference
10 #
11 
12 from pyfbsdk import *
13 from pyfbsdk_additions import *
14 from PySide2 import QtWidgets, shiboken2
15 import sys, inspect, os
16 
17 # Add temp sys.path
18 lCurFilePath = inspect.currentframe().f_code.co_filename
19 sys.path.append( os.path.dirname(lCurFilePath) )
20 sys.path.append( os.path.join( os.path.dirname(lCurFilePath), 'MBFileRefDemo' ) )
21 import ReferencingSample
22 
23 #
24 # Subclass FBWidgetHolder and override its WidgetCreate function
25 #
26 class NativeWidgetHolder(FBWidgetHolder):
27  #
28  # Override WidgetCreate function to create native widget via PySide2.
29  # \param parentWidget Memory address of Parent QWidget.
30  # \return Memory address of the child native widget.
31  #
32  def WidgetCreate(self, pWidgetParent):
33 
34  #
35  # IN parameter pWidgetparent is the memory address of the parent Qt widget.
36  # here we should PySide2.shiboken2.wrapInstance() function to convert it to PySide2.QtWidget object.
37  # and use it the as the parent for native Qt widgets created via Python.
38  # Similiar approach is available in the sip python module for PyQt
39  #
40  # Only a single widget is allowed to be the *direct* child of the IN parent widget.
41  #
42  self.mNativeQtWidget = ReferencingSample.MainForm(shiboken2.wrapInstance(pWidgetParent, QtWidgets.QWidget))
43 
44  #
45  # return the memory address of the *single direct* child QWidget.
46  #
47  return shiboken2.getCppPointer(self.mNativeQtWidget)[0]
48 
49 class FileReferenceTool(FBTool):
50  def BuildLayout(self):
51  x = FBAddRegionParam(0,FBAttachType.kFBAttachLeft,"")
52  y = FBAddRegionParam(0,FBAttachType.kFBAttachTop,"")
53  w = FBAddRegionParam(0,FBAttachType.kFBAttachRight,"")
54  h = FBAddRegionParam(0,FBAttachType.kFBAttachBottom,"")
55  self.AddRegion("main","main", x, y, w, h)
56  self.SetControl("main", self.mNativeWidgetHolder)
57 
58  def __init__(self, name):
59  FBTool.__init__(self, name)
60  self.mNativeWidgetHolder = NativeWidgetHolder();
61  self.BuildLayout()
62  self.StartSizeX = 650
63  self.StartSizeY = 425
64  self.MinSizeX = 650
65  self.MinSizeY = 425
66 
67 gToolName = "File Reference Tool"
68 
69 #Development? - need to recreate each time!!
70 gDEVELOPMENT = True
71 
72 if gDEVELOPMENT:
73  FBDestroyToolByName(gToolName)
74 
75 if gToolName in FBToolList:
76  tool = FBToolList[gToolName]
77  ShowTool(tool)
78 else:
79  tool=FileReferenceTool(gToolName)
80  FBAddTool(tool)
81  if gDEVELOPMENT:
82  ShowTool(tool)
83 
84 # Remove temp sys.path
85 sys.path.remove( os.path.join( os.path.dirname(lCurFilePath), 'MBFileRefDemo' ) )
86 sys.path.remove( os.path.dirname(lCurFilePath) )