User Defined functions in makehuman

MakeHuman python API, python plugins, etc

Moderator: joepal

User Defined functions in makehuman

Postby Shreyansh_Dubey » Fri Sep 28, 2018 6:14 am

How can i use Python 2.7 functions which are user defined in Makehuman shell as well as in scripts?

I want to create some functions involving loops and they would include some Python 2.7 predefined functions as well as some Makehuman mhapi functions, that I can call directly in makehuman scripting. Where should i save any such function so that it is accesible in script as well as in Makehuman shell.
Please guide me.

ThankYou
Shreyansh_Dubey
 
Posts: 9
Joined: Sun Sep 16, 2018 10:09 am

Re: User Defined functions in makehuman

Postby joepal » Fri Sep 28, 2018 9:05 am

I'd say the most efficient way would be writing a new plugin, which extends MHAPI on load.

In a file called for example "9_myuserfunctions.py" placed under "plugins":

Code: Select all
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

from core import G

# The reason for the folloring arcane snippet is that python
# refuses to import from modules with a name that starts
# with a number.

mhapimodule = __import__("1_mhapi")

try:
    attrlist = mhapimodule.__all__
except AttributeError:
    attrlist = dir (mhapimodule)
for attr in attrlist:
    globals()[attr] = getattr (mhapimodule, attr)

# Extending Namespace is stricly speaking not necessary, but it's the structure
# that the rest of MHAPI follows. In __init__ we'll also make a convenience assignment
# so you can access the whole of MHAPI via "self.mhapi" in all the methods.

class MyUserFunctions(namespace.NameSpace):

    def __init__(self,api):
        self.mhapi = api
        namespace.NameSpace.__init__(self)

    def oneOfMyFunctions(self):
        print("I'm alive!")

def load(app):
    G.app.mhapi.myuserfunctions = MyUserFunctions(G.app.mhapi)

def unload(app):
    pass


Now, after startup, you can call this like any other MHAPI module. For example in the shell tab:

Code: Select all
from core import G
G.app.mhapi.myuserfunctions.oneOfMyFunctions()


... will respond by printing "I'm alive"
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: User Defined functions in makehuman

Postby Shreyansh_Dubey » Mon Oct 01, 2018 7:18 am

Thankyou :P :geek:
Shreyansh_Dubey
 
Posts: 9
Joined: Sun Sep 16, 2018 10:09 am


Return to Python scripts

Who is online

Users browsing this forum: Google [Bot] and 1 guest