Difference between revisions of "IC Python API:Your First iClone Python Plugin"

From Reallusion Wiki!
Jump to: navigation, search
(Created page with "This topic will guide you through the creation and execution of your first iClone Python plugin. == Preparing your Python IDE == iClone doesn't support an internal IDE, so y...")
 
m (Loading your Python Plugin)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{TOC}}
 +
{{Parent|IC_Python_API|iClone Python API}}
 +
 
This topic will guide you through the creation and execution of your first iClone Python plugin.
 
This topic will guide you through the creation and execution of your first iClone Python plugin.
  
Line 5: Line 8:
 
iClone doesn't support an internal IDE, so you'll have to resort to an external script editor like [https://notepad-plus-plus.org/ Notepad++], [https://www.jetbrains.com/pycharm/ PyCharm], [https://www.spyder-ide.org/ Spyder], among others.
 
iClone doesn't support an internal IDE, so you'll have to resort to an external script editor like [https://notepad-plus-plus.org/ Notepad++], [https://www.jetbrains.com/pycharm/ PyCharm], [https://www.spyder-ide.org/ Spyder], among others.
  
We recommend Notepad++, it's a free and tiny source code editor with Python support. Before using Notepad++, you should enable Replace by space option under '''Settings > Preferences > Language > Tab Settings'''.
+
We recommend Notepad++, it's a free and tiny source code editor with Python support. Before using Notepad++, you should enable '''Replace by space''' option under '''Settings > Preferences > Language > Tab Settings'''.
  
 
{{Single_Illustration|Python_api_notepad.png}}
 
{{Single_Illustration|Python_api_notepad.png}}
  
 
We recommend setting tab sizes to 4 spaces.
 
We recommend setting tab sizes to 4 spaces.
 +
 +
== Loading your Python Plugin ==
 +
 +
There are two different methods for loading a plugin into iClone:
 +
 +
Method 1: Create a main.py file and put it inside the '''iClone Install Directory \ Bin64 \ OpenPlugin folder\ [Your Plugin Name]''' The PY files requires one specific functions: '''initialize_plugin()''', which is respectively called when iClone attempts to load the plugin. If this functions do not exist in the file, the plugin will fail to load. The name of PY file must be '''main.py'''.
 +
 +
<syntaxhighlight lang="Python">
 +
import RLPy
 +
import PySide2
 +
from PySide2 import *
 +
from PySide2.shiboken2 import wrapInstance
 +
 +
def initialize_plugin():
 +
    # Add menu
 +
    ic_dlg = wrapInstance(int(RLPy.RUi.GetMainWindow()), QtWidgets.QMainWindow)
 +
    plugin_menu = ic_dlg.menuBar().findChild(QtWidgets.QMenu, "pysample_menu")
 +
    if (plugin_menu == None):
 +
        plugin_menu = wrapInstance(int(RLPy.RUi.AddMenu("Python Samples", RLPy.EMenu_Plugins)), QtWidgets.QMenu)
 +
        plugin_menu.setObjectName("pysample_menu")
 +
 +
    hello_world_action = plugin_menu.addAction("Hello World")
 +
    hello_world_action.triggered.connect(show_dialog)
 +
 +
def show_dialog():
 +
    RLPy.RUi.ShowMessageBox("Your First Python Plugin", "Hello World", RLPy.EMsgButton_Ok)
 +
</syntaxhighlight>
 +
 +
Method 2: Manually perform '''Menu > Script > Load Python''', choose a PY file then make sure the initial define resembles the following:
 +
 +
<syntaxhighlight lang="Python">
 +
import RLPy
 +
def run_script():
 +
    print ("Hello World")
 +
    RLPy.RUi.ShowMessageBox("Your First Python Plugin", "Hello World", RLPy.EMsgButton_Ok)
 +
</syntaxhighlight>

Latest revision as of 01:19, 9 April 2019

Main article: iClone Python API.

This topic will guide you through the creation and execution of your first iClone Python plugin.

Preparing your Python IDE

iClone doesn't support an internal IDE, so you'll have to resort to an external script editor like Notepad++, PyCharm, Spyder, among others.

We recommend Notepad++, it's a free and tiny source code editor with Python support. Before using Notepad++, you should enable Replace by space option under Settings > Preferences > Language > Tab Settings.

Python api notepad.png

We recommend setting tab sizes to 4 spaces.

Loading your Python Plugin

There are two different methods for loading a plugin into iClone:

Method 1: Create a main.py file and put it inside the iClone Install Directory \ Bin64 \ OpenPlugin folder\ [Your Plugin Name] The PY files requires one specific functions: initialize_plugin(), which is respectively called when iClone attempts to load the plugin. If this functions do not exist in the file, the plugin will fail to load. The name of PY file must be main.py.

import RLPy
import PySide2
from PySide2 import *
from PySide2.shiboken2 import wrapInstance

def initialize_plugin():
    # Add menu
    ic_dlg = wrapInstance(int(RLPy.RUi.GetMainWindow()), QtWidgets.QMainWindow)
    plugin_menu = ic_dlg.menuBar().findChild(QtWidgets.QMenu, "pysample_menu")
    if (plugin_menu == None):
        plugin_menu = wrapInstance(int(RLPy.RUi.AddMenu("Python Samples", RLPy.EMenu_Plugins)), QtWidgets.QMenu)
        plugin_menu.setObjectName("pysample_menu")

    hello_world_action = plugin_menu.addAction("Hello World")
    hello_world_action.triggered.connect(show_dialog)

def show_dialog():
    RLPy.RUi.ShowMessageBox("Your First Python Plugin", "Hello World", RLPy.EMsgButton_Ok)

Method 2: Manually perform Menu > Script > Load Python, choose a PY file then make sure the initial define resembles the following:

import RLPy
def run_script():
    print ("Hello World")
    RLPy.RUi.ShowMessageBox("Your First Python Plugin", "Hello World", RLPy.EMsgButton_Ok)