试用 Houdini Engine Python API

2024-09-06 23:18
文章标签 python api engine 试用 houdini

本文主要是介绍试用 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

使用步骤:

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

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

附录

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

这篇关于试用 Houdini Engine Python API的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1143399

相关文章

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

Python打包成exe常用的四种方法小结

《Python打包成exe常用的四种方法小结》本文主要介绍了Python打包成exe常用的四种方法,包括PyInstaller、cx_Freeze、Py2exe、Nuitka,文中通过示例代码介绍的非... 目录一.PyInstaller11.安装:2. PyInstaller常用参数下面是pyinstal

Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题

《Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题》在爬虫工程里,“HTTPS”是绕不开的话题,HTTPS为传输加密提供保护,同时也给爬虫带来证书校验、... 目录一、核心问题与优先级检查(先问三件事)二、基础示例:requests 与证书处理三、高并发选型:

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数