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

From Reallusion Wiki!
Jump to: navigation, search
m (Loading your Python Plugin)
m (Loading your Python Plugin)
 
(One intermediate revision by the same user not shown)
Line 20: Line 20:
 
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'''.
 
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'''.
  
{{Code|<nowiki>
+
<syntaxhighlight lang="Python">
 
import RLPy
 
import RLPy
 
import PySide2
 
import PySide2
Line 38: Line 38:
  
 
def show_dialog():
 
def show_dialog():
     RLPy.RUi.ShowMessageBox("Your First Python Plugin", "Hello World", RLPy.EMsgButton_Ok)</nowiki>
+
     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:
+
Method 2: Manually perform '''Menu > Script > Load Python''', choose a PY file then make sure the initial define resembles the following:
  
{{code|<nowiki>
+
<syntaxhighlight lang="Python">
 
import RLPy
 
import RLPy
 
def run_script():
 
def run_script():
 
     print ("Hello World")
 
     print ("Hello World")
 
     RLPy.RUi.ShowMessageBox("Your First Python Plugin", "Hello World", RLPy.EMsgButton_Ok)
 
     RLPy.RUi.ShowMessageBox("Your First Python Plugin", "Hello World", RLPy.EMsgButton_Ok)
</nowiki>}}
+
</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)