科普文章在那个网站做,营销策划与运营培训,无锡营销型网站建设,公司网站空间申请使用Python USD API 来实现 Physics 。
以下内容中#xff0c;大部分 Python 代码可以在 Physics Python 演示脚本文件中找到#xff0c;本文仅作为个人学习笔记。 一.设置 USD Stage 和物理场景 Setting up a USD Stage and a Physics Scene USD Stage不知道怎么翻译#…使用Python USD API 来实现 Physics 。
以下内容中大部分 Python 代码可以在 Physics Python 演示脚本文件中找到本文仅作为个人学习笔记。 一.设置 USD Stage 和物理场景 Setting up a USD Stage and a Physics Scene USD Stage不知道怎么翻译就是放置USD文件的地方Isaac Sim软件和官方文档全英文就算翻译了官方文档回去使用软件也全是英文...英语菜狗啃的累死我了...
from pxr import Usd, UsdGeom, UsdPhysics, UsdShade, Sdf, Gf, Tf# Set up a USD Stage to define what is the up axis and set up the units,
# which is crucial, as some default simulation parameters are derived
# from the MetersPerUnit value
# 单位0.01m
stage omni.usd.get_context().get_stage()
UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.z)
UsdGeom.SetStageMetersPerUnit(stage, 0.01)# Physics scene definition
scene UsdPhysics.Scene.Define(stage, /physicsScene)# setup gravity
# note that gravity has to respect the selected units, if we are using cm, the gravity has to respect that
# 重力方向下重力大小9.81m/s^2
scene.CreateGravityDirectionAttr().Set(Gf.Vec3f(0.0, 0.0, -1.0))
scene.CreateGravityMagnitudeAttr().Set(981.0) 二.创建碰撞体 Creating Colliders
# Cube path
cubeActorPath /cubeActor# Cube parameters like size, position, orientation
# 这个500测试发现实在太高了第一次做的时候转一圈都没找到Cube自己用设置1050之类的就行
size 25.0
position Gf.Vec3f(0.0, 0.0, 500.0)
orientation Gf.Quatf(1.0)
color Gf.Vec3f(71.0 / 255.0, 105.0 / 255.0, 1.0)# define the cube geom(几何)
cubeGeom UsdGeom.Cube.Define(stage, cubeActorPath)
cubeGeom.CreateSizeAttr(size)
cubeGeom.AddTranslateOp().Set(position)
cubeGeom.AddOrientOp().Set(orientation)
cubeGeom.AddScaleOp().Set(Gf.Vec3f(1.0))
cubeGeom.CreateDisplayColorAttr().Set([color])# get the prim
cubePrim stage.GetPrimAtPath(cubeActorPath)# setup collision, so that the cube does collider
UsdPhysics.CollisionAPI.Apply(cubePrim) 三.不同的碰撞体和渲染表现 Different Collision and Rendering Representations 必须创建两个不同的几何体其中一个用于碰撞另一个用于渲染(这里也可以理解为展示一个作为碰撞箱隐藏一个可以观察)。两个几何体的父 xform 应该设置为刚体使它们能够一起移动。 # Top level actor, contains rigid body
rigidCompoundPath /compoundRigid
rigidXform UsdGeom.Xform.Define(stage, rigidCompoundPath)
rigidPrim stage.GetPrimAtPath(rigidCompoundPath)# Rigid body transform
rigidCompoundPos Gf.Vec3f(0.0, 0.0, 10.0)
rigidXform.AddTranslateOp().Set(rigidCompoundPos)
rigidXform.AddOrientOp().Set(Gf.Quatf(1.0, 0.0, 0.0, 0.0))physicsAPI UsdPhysics.RigidBodyAPI.Apply(rigidPrim)# Collision shape
collisionShape rigidCompoundPath /physicsBoxShapesize 25.0
shapePos Gf.Vec3f(0.0)
shapeQuat Gf.Quatf(1.0)cubeGeom UsdGeom.Cube.Define(stage, collisionShape)
cubePrim stage.GetPrimAtPath(collisionShape)
cubeGeom.CreateSizeAttr(size)
cubeGeom.AddTranslateOp().Set(shapePos)
cubeGeom.AddOrientOp().Set(shapeQuat)# set it as collision
UsdPhysics.CollisionAPI.Apply(cubePrim)# hide it from rendering
cubeGeom.CreatePurposeAttr(UsdGeom.Tokens.guide)# rendering shape
renderSphere rigidCompoundPath /renderingSpheresphereGeom UsdGeom.Sphere.Define(stage, renderSphere)
#sphereGeom.CreateSizeAttr(20.0)
sphereGeom.AddTranslateOp().Set(shapePos)
sphereGeom.AddOrientOp().Set(shapeQuat) 四.物理材料 Physics Materials 每个碰撞器都可以分配不同的物理材料。UsdPhysicsMaterialAPI 可以应用于任何其他材料并且此 API 添加了物理材料属性如静态/动态摩擦。要将材料分配给碰撞器请使用具有“物理”目的的材料绑定。
# define physics material
materialPath /material
mu 1.0
UsdShade.Material.Define(stage, materialPath)
material UsdPhysics.MaterialAPI.Apply(stage.GetPrimAtPath(materialPath))
material.CreateStaticFrictionAttr().Set(mu)
material.CreateDynamicFrictionAttr().Set(mu)
material.CreateRestitutionAttr().Set(0.0)
material.CreateDensityAttr().Set(1000.0)collisionAPI UsdPhysics.CollisionAPI.Get(stage, collisionShape)# add the material to the collider
bindingAPI UsdShade.MaterialBindingAPI.Apply(collisionAPI.GetPrim())
materialPrim material.GetPrim()
material UsdShade.Material(materialPrim)
bindingAPI.Bind(material, UsdShade.Tokens.weakerThanDescendants, physics) 五.使用凸网格进行圆柱和圆锥碰撞 Using Convex Mesh for Cylinder and Cone Collision UsdGeom.Cylinder 和 UsdGeom.Cone prims 的碰撞默认近似为自定义几何体以获得更高的精度。但是您可以通过在碰撞部分下的物理设置中禁用相应的切换来将它们设置为使用凸网格。 注意您还可以使用 Omniverse Create 内置的圆锥和圆柱网格几何体进行凸近似。这允许您细粒度控制是否应使用凸近似或自定义几何体碰撞器模拟单个几何体。 原文 UsdGeom.Cylinder and UsdGeom.Cone prims’ collision is by default approximated as custom geometry for higher precision. However, you can set them to use convex mesh instead by disabling the corresponding toggles in the Physics settings under the collision section. Note: you can also use the Omniverse Create built-in cone and cylinder mesh geometries for convex approximation. This allows you to fine grain control whether individual geometry should be simulated with a convex approximation or custom geometry collider. 我想应该可以对单个物体模块进行精细修改...还没试呢晚上找个时间弄弄看。 六.刚体 Rigid Bodies 刚体在 Physics 架构中表示为应用于 xformable Prim 的 UsdPhysicsRigidBodyAPI 架构。
# Cube path
cubeActorPath /cubeActorRigid# Cube parameters like size, position, orientation
size 25.0
position Gf.Vec3f(0.0, 0.0, 500.0)
orientation Gf.Quatf(1.0)
color Gf.Vec3f(71.0 / 255.0, 105.0 / 255.0, 1.0)# initial velocities
linVelocity Gf.Vec3f(2.0, 1.0, 2.0)
angularVelocity Gf.Vec3f(1.0, 0.0, 0.0)# define the cube geom
cubeGeom UsdGeom.Cube.Define(stage, cubeActorPath)
cubeGeom.CreateSizeAttr(size)
cubeGeom.AddTranslateOp().Set(position)
cubeGeom.AddOrientOp().Set(orientation)
cubeGeom.AddScaleOp().Set(Gf.Vec3f(1.0))
cubeGeom.CreateDisplayColorAttr().Set([color])# get the prim
cubePrim stage.GetPrimAtPath(cubeActorPath)# setup collision, so that the cube does collider
UsdPhysics.CollisionAPI.Apply(cubePrim)# setup physics - rigid body
rigidBodyAPI UsdPhysics.RigidBodyAPI.Apply(cubePrim)# apply initial velocities
rigidBodyAPI.CreateVelocityAttr().Set(linVelocity)
rigidBodyAPI.CreateAngularVelocityAttr().Set(angularVelocity)
在仿真过程中如果要更改刚体的线速度则必须更改 UsdPhysicsRigidBodyAPI 的速度属性
rigidBodyAPI.CreateVelocityAttr().Set(newVelocity) 6.1 刚体质量属性 Rigid Body Mass Properties UsdPhysicsMassAPI 定义了刚体的质量属性。UsdPhysicsMassAPI 可以应用于具有 UsdPhysicsRigidBodyAPI 或 USdPhysicsCollisionAPI 的 Prim。它提供了多种定义刚体质量属性的方法 physics:mass质量属性。注意任何子 Prim 在应用 UsdPhysicsMassAPI 时也可以具有质量属性。在这种情况下优先规则是“父级质量属性覆盖子级的质量属性”。这可能有些反直觉因为质量是一个计算得到的量通常不是累积的。例如如果父级的质量为 10而两个子级中的一个质量为 20允许子级的质量覆盖父级的质量将导致另一个子级的质量为 -10。 physics:density密度属性。在刚体物理学的背景下密度间接地通过公式质量 密度 × 对象的体积来设置质量。在同时指定密度和质量的情况下质量具有优先权。与质量不同子级 Prim 的密度将覆盖父级 Prim 的密度。注意collisionAPI 的密度也可以通过 physicsMaterial 关系来设置。材料密度在密度定义中具有最弱的优先级。注意密度遵循单位。因此对于单位为 1 的水密度为 1000对于单位为 0.01水密度为 0.001。 physics:centerOfMassPrim 本地空间中的质心。 physics:diagonalInertia未旋转状态下对角化惯性张量。 physics:principalAxes惯性张量主轴的方向四元数表示在 Prim 的本地空间中。 解析代码会收集每个碰撞形状的质量属性并使用计算出的惯性张量来计算体的最终质量属性。如果未找到任何 massAPI则使用默认密度 1000 来确定最终的质量。
# Cube path
cubeActorPath /cubeActorRigidMass# Cube parameters like size, position, orientation
size 25.0
position Gf.Vec3f(0.0, 0.0, 500.0)
orientation Gf.Quatf(1.0)
color Gf.Vec3f(71.0 / 255.0, 105.0 / 255.0, 1.0)# define the cube geom
cubeGeom UsdGeom.Cube.Define(stage, cubeActorPath)
cubeGeom.CreateSizeAttr(size)
cubeGeom.AddTranslateOp().Set(position)
cubeGeom.AddOrientOp().Set(orientation)
cubeGeom.AddScaleOp().Set(Gf.Vec3f(1.0))
cubeGeom.CreateDisplayColorAttr().Set([color])# get the prim
cubePrim stage.GetPrimAtPath(cubeActorPath)# setup collision, so that the cube does collider
UsdPhysics.CollisionAPI.Apply(cubePrim)# setup physics - rigid body, so that cube does simulate as a rigid body
UsdPhysics.RigidBodyAPI.Apply(cubePrim)# custom center of mass
massAPI UsdPhysics.MassAPI.Apply(cubePrim)
massAPI.CreateCenterOfMassAttr().Set(Gf.Vec3f(0.0, 0.0, 10.0)) 七.静态体和动态体的区别 Difference Between a Static Body and a Dynamic Body 如果找到具有 UsdPhysicsCollisionAPI 的 Geom Prim则它可以属于静态实体或动态实体。决定的逻辑是这样的 如果层次结构中的 prim 或任何父 Prim 上没有应用 UsdPhysicsRigidBodyAPI则具有 UsdPhysicsCollisionAPI 的几何体将被视为静态实体。 还 如果 physicsrigidBodyEnabled 属性为 true默认值则几何属于动态实体。 否则Geom 属于静态实体。 八.运动体 Kinematic Body 如果需要通过动画或用户定义的目标位置来驱动身体则应将 UsdPhysicsRigidBodyAPI 上的 physicskinematicEnabled 属性设置为 true。 九.CPU 与 GPU 刚体动力学 CPU vs GPU Rigid Body Dynamics 默认情况下在 GPU 上执行刚体仿真。可以在 Physics Debug 窗口中或使用场景属性覆盖管道。 为了使用 CPU 仿真请在 UsdPhysicsScene prim 中添加一个 PhysxSceneAPI 并设置以下属性 physxScenebroadphaseType - MBP physxSceneenableGPUDynamics - 错误 对于 GPU 模拟第一个设置为 GPU第二个设置为 True。 文章转载自: http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.rhnn.cn.gov.cn.rhnn.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.jtqxs.cn.gov.cn.jtqxs.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn http://www.morning.pcshb.cn.gov.cn.pcshb.cn http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn http://www.morning.ntzbr.cn.gov.cn.ntzbr.cn http://www.morning.ztnmc.cn.gov.cn.ztnmc.cn http://www.morning.a3e2r.com.gov.cn.a3e2r.com http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.juju8.cn.gov.cn.juju8.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.yrflh.cn.gov.cn.yrflh.cn http://www.morning.bdqpl.cn.gov.cn.bdqpl.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.kphsp.cn.gov.cn.kphsp.cn http://www.morning.qieistand.com.gov.cn.qieistand.com http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.irqlul.cn.gov.cn.irqlul.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.gkgr.cn.gov.cn.gkgr.cn http://www.morning.jpwmk.cn.gov.cn.jpwmk.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.c7624.cn.gov.cn.c7624.cn http://www.morning.nmpdm.cn.gov.cn.nmpdm.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.kqpq.cn.gov.cn.kqpq.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.trrrm.cn.gov.cn.trrrm.cn http://www.morning.nmnhs.cn.gov.cn.nmnhs.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.ttxnj.cn.gov.cn.ttxnj.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.krgjc.cn.gov.cn.krgjc.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.sskhm.cn.gov.cn.sskhm.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn http://www.morning.frfnb.cn.gov.cn.frfnb.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.gygfx.cn.gov.cn.gygfx.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.hjssh.cn.gov.cn.hjssh.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn