本文主要是介绍试用 Houdini Engine Python API,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
介绍
hapi 这个python包 是HoudiniEngine的一层python封装。
使用python的优势是灵活性,因为C代码总需要编译,使得迭代较慢。
Tips:
hapi中的内容总可以查阅HoudiniEngine的文档,因为概念和函数是相同的。
使用环境
1. Houdini编辑器内:Python Shell
在其中可以 import hapi
:
2. Houdini命令行工具:hython.exe
启动它相当于启动python,当然Houdini同时也应该设置了必要的环境。
在其中可以导入hapi
:
试用
代码来源于HAPI Cookbook : Create an Input Mesh(但做了些许改动):
import hapi
import numpy# Create an in process HAPI session. It's also possible to
# connect to a remote session, such as a Houdini SessionSync
# session, if one is available. For example:
# session = hapi.createThriftSocketSession("localhost", 9090)
session = hapi.createInProcessSession()# Init the session with default cook options.
options = hapi.CookOptions()
hapi.initialize(session, options)# Define vertex positions for our test geometry
vertexPositions = numpy.array([-0.5, -0.5, -0.5,0.5, -0.5, -0.5,0.5, -0.5, 0.5,-0.5, -0.5, 0.5,-0.5, 0.5, -0.5,0.5, 0.5, -0.5,0.5, 0.5, 0.5,-0.5, 0.5, 0.5])# Face and vert info for our test geometry
vertexIndices = numpy.array([1,5,4,0,2,6,5,1,3,7,6,2,0,4,7,3,2,1,0,3,5,6,7,4])
faceCounts = numpy.array([4, 4, 4, 4, 4, 4])
nVerts = len(vertexPositions)//3
nFaces = len(vertexIndices)//4# Construct an input node to stash our geo into
input_id = hapi.createInputNode(session, "input_node")
geo_info = hapi.getDisplayGeoInfo(session, input_id)# Build the the part info and store it to our geo info. This is an example
# of what access a HAPI struct in Python looks like. Python provides a
# constructor for the struct that default-initializes all of the fields. Theh
# fields can then be accessed as properties on the object.
part_info = hapi.PartInfo()
part_info.vertexCount = len(vertexIndices)
part_info.faceCount = nFaces
part_info.pointCount = nVerts
part_info.type = hapi.partType.Meshhapi.setPartInfo(session, geo_info.nodeId, 0, part_info)# Define the attribute info for the P attribute
p_info = hapi.AttributeInfo()
p_info.exists = True
p_info.owner = hapi.attributeOwner.AttrownerPoint
p_info.count = nVerts
p_info.tupleSize = 3
p_info.storage = hapi.storageType.Float
p_info.originalOwner = hapi.attributeOwner.AttrownerInvalid# P attribute can then be added to the geo
hapi.addAttribute(session, geo_info.nodeId, 0, "P", p_info)# We still need to tell HAPI how many values to extract from the input array.
# HAPI is able to use less than the total number of values.
hapi.setAttributeFloatData_np(session, geo_info.nodeId, 0, "P", p_info,vertexPositions, 0, nVerts)# Set the face and vert info on the geo itself.
hapi.setFaceCounts_np(session, geo_info.nodeId, 0, faceCounts, 0,len(faceCounts))
hapi.setVertexList_np(session, geo_info.nodeId, 0, vertexIndices, 0,len(vertexIndices))# Submit all of the geometry changes we've made so they're added to Houdini.
hapi.commitGeo(session, geo_info.nodeId)# We can then ask the node to save it's geometry to disk.
hapi.saveGeoToFile(session, geo_info.nodeId, '/tmp/hapi_createmesh.bgeo.sc')
将python脚本存储到了D:/Temp/hapi_createmesh.py
。
使用步骤:
- 打开Cmd
- 使用
cd
命令将目录变到hython.exe
所在的文件夹 - 使用命令
hython D:/Temp/hapi_createmesh.py
来执行脚本。
此脚本将会生成一个几何体文件在C:/tmp/hapi_createmesh.bgeo.sc
可以在Houdini中用文件节点打开:
附录
相比较于原版的python代码,我的改动:
若不改动我这边运行会报错
这篇关于试用 Houdini Engine Python API的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!