Simple API example?

If you have problems understanding something or getting started, ask here

Moderator: joepal

Simple API example?

Postby improvedwoodsier » Fri Feb 18, 2022 4:21 am

I have written my own randomizer that determines parameters for a model (simple python script).

I basically just want to launch makehuman, apply my random parameters, then export the result. (and eventually import into Blender)

Exporting seems simple enough, ie:

https://github.com/makehumancommunity/c ... exports.md

Same with manipulating model parameters:

https://github.com/makehumancommunity/c ... difiers.md

However, I'm not trying to write a plugin or anything, just a single python script for now.

I've tried doing:

Code: Select all
from core import G


in a file called test.py in the "makehuman" directory (same directory as makehuman.py)

but I think I'm missing something as it can't find "core". I tried moving test.py into the "core" directory but same problem. I'm used to using pip where I can just install a library and import it, I don't know how to interact with the makehuman code with my own code.

I'm sure it's just some simple path issue, how do I get started here?
improvedwoodsier
 
Posts: 7
Joined: Fri Feb 18, 2022 3:41 am

Re: Simple API example?

Postby RobBaer » Fri Feb 18, 2022 10:57 pm

Have you looked at the mass produce tab under the community tab? It may already do what you want. In any case, the source code should be useful as a model for your own code.
User avatar
RobBaer
 
Posts: 1208
Joined: Sat Jul 13, 2013 3:30 pm
Location: Kirksville, MO USA

Re: Simple API example?

Postby Aranuvir » Sun Feb 20, 2022 1:15 pm

If you take a look at makehuman.py you will see it modifies sys.path a bit. So, your import should be something like this:
Code: Select all
from lib.core import G

Though trying to access G from outside MakeHuman simply does not make any sense :(. It is the same as trying to use bpy outside of Blender. G is some sort of global object, that gets filled up during the start up sequence. MakeHuman itself has some built in rudimentary scripting functionality. The built in shell might also be useful, especially when it gets extended with ipython and qtconsole.
Aranuvir
 
Posts: 1314
Joined: Sun Oct 12, 2014 2:12 pm

Re: Simple API example?

Postby improvedwoodsier » Wed Feb 23, 2022 10:24 pm

RobBaer wrote:Have you looked at the mass produce tab under the community tab? It may already do what you want. In any case, the source code should be useful as a model for your own code.


Thank you, I will look at the source code for that.
improvedwoodsier
 
Posts: 7
Joined: Fri Feb 18, 2022 3:41 am

Re: Simple API example?

Postby improvedwoodsier » Wed Feb 23, 2022 10:27 pm

Aranuvir wrote:If you take a look at makehuman.py you will see it modifies sys.path a bit. So, your import should be something like this:
Code: Select all
from lib.core import G

Though trying to access G from outside MakeHuman simply does not make any sense :(. It is the same as trying to use bpy outside of Blender. G is some sort of global object, that gets filled up during the start up sequence. MakeHuman itself has some built in rudimentary scripting functionality. The built in shell might also be useful, especially when it gets extended with ipython and qtconsole.


Ah ok. I've been learning the Blender API a bit, and I've been running my Blender scripts like this:

Code: Select all
blender --python test.py -b


I don't need to see the GUI, i just get a rendered image as the output.

Is it possible to invoke makehuman in a similar manner? Start it, run my script, change some params, export a model, close?

Thank you!
improvedwoodsier
 
Posts: 7
Joined: Fri Feb 18, 2022 3:41 am

Re: Simple API example?

Postby improvedwoodsier » Sun Mar 06, 2022 2:59 am

Any information on this? How would I invoke my script from the command line the same way as a blender script?
improvedwoodsier
 
Posts: 7
Joined: Fri Feb 18, 2022 3:41 am

Re: Simple API example?

Postby joepal » Sun Mar 06, 2022 2:40 pm

MakeHuman isn't built for executing addon scripts in this way. The closest I can think of is starting makehuman and using the socket server to send commands, or using the scripting tab and execute your scripts there.
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: Simple API example?

Postby improvedwoodsier » Mon Mar 07, 2022 11:32 pm

joepal wrote:MakeHuman isn't built for executing addon scripts in this way. The closest I can think of is starting makehuman and using the socket server to send commands, or using the scripting tab and execute your scripts there.


Ah, I see. I think just leaving MH running should work fine.

I looked at the socket server stuff a little bit and was able to use the CLI example.

Do you know the syntax for adjusting a slider and exporting the result?

Thank you!
improvedwoodsier
 
Posts: 7
Joined: Fri Feb 18, 2022 3:41 am

Re: Simple API example?

Postby joepal » Tue Mar 08, 2022 6:59 am

The socket API is defined in

https://github.com/makehumancommunity/c ... /modops.py (for getting and setting modifiers)
https://github.com/makehumancommunity/c ... meshops.py (for mesh operations, such as getting the current vertex coordinates)

This is a limited set of simple wrappers for MHAPI calls. These in turn are defined here:

https://github.com/makehumancommunity/c ... er/1_mhapi

The MHAPI export functionality is available here: https://github.com/makehumancommunity/c ... exports.py
The modifier functionality is available here: https://github.com/makehumancommunity/c ... difiers.py

There is currently no wrapper for exports in the socket code. So if you want to get to, for example, the MHAPI exportAsOBJ( ) call, you'd have to add access to it in one of the socket classes.

Or, if you instead go via the scripting tab, this simple snippet is what you'd need for setting some modifier values and then export the character:

Code: Select all
from core import G

modifier_api  = G.app.mhapi.modifiers
modifier_api.setWeight(1.0)
modifier_api.setMuscle(1.0)
modifier_api.applyModifier("head/head-triangular", 1.0)

export_api = G.app.mhapi.exports
export_api.exportAsOBJ("/tmp/human.obj")

# This prints a list of all available modifier names to the console
import pprint
pprint.pprint(modifier_api.getAvailableModifierNames())
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: Simple API example?

Postby improvedwoodsier » Wed Mar 09, 2022 1:36 am

joepal wrote:Or, if you instead go via the scripting tab, this simple snippet is what you'd need for setting some modifier values and then export the character


Thank you! This is exactly what I needed and it works perfectly :)
improvedwoodsier
 
Posts: 7
Joined: Fri Feb 18, 2022 3:41 am

Next

Return to Newbies

Who is online

Users browsing this forum: No registered users and 1 guest