试用 Houdini Engine Python API

13 篇文章 1 订阅

介绍

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.Mesh

hapi.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

使用步骤:

  1. 打开Cmd
  2. 使用cd命令将目录变到hython.exe所在的文件夹
  3. 使用命令 hython D:/Temp/hapi_createmesh.py 来执行脚本。

在这里插入图片描述
此脚本将会生成一个几何体文件在C:/tmp/hapi_createmesh.bgeo.sc
在这里插入图片描述
可以在Houdini中用文件节点打开:
在这里插入图片描述

附录

相比较于原版的python代码,我的改动:
在这里插入图片描述
若不改动我这边运行会报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值