Jump to content

Instancing using the data from the external file


Recommended Posts

Hi,
I am looking for the ways to replicate in Houdini point instancing done in some other application.  I will skip here the data importing part because it is clear in my situation how to do that with Python. Let's say I already have a Python dictionary with elements like 'name':'transform', where 'name' is the name of the object to be instanced and 'transform' is a list of 16 floats representing world transform matrix of that instance.

I have figured out so far how to do it at the object level. Here is my Python code for that:

# My existing dictionary containing pairs like 'name':'transform'
my_dict = {'foo': '....',  'bar':'......', ......}

# where each dictionary value is a list of 16 floats

for key in my_dict.keys():
    node = hou.node('/obj').createNode('null')
    node.setName(key)
    m4 = hou.Matrix4(1)
    m4.setTo(my_dict[key])
    node.setParmTransform(m4)

 
This gives me bunch of named nulls with the correct transforms. And I can parent my object under those nulls.

But I need the same at the SOP level. I need a bunch of points with 'name', vector 'scale' and preferably quaternion 'orient' attributes which I can pipe into the the right input of the CopySOP for instancing. Any help on that would be much appreciated.

Link to comment
Share on other sites

o.k. I am a bit closer now to what I am trying to achieve.

Here is the code snippet that when used in Python SOP will add points with attributes coming from my dictionary.

Again, my dictionary consists of  pairs of an object name and a list of 16 floats for world transform for that object. Let's say that dictionary already exists and was imported from some other application.

node = hou.pwd()
geo = node.geometry()

# for test case dictionary consists only of 2 items and transforms are 
# represented by lists of integers instead lists of floats 
my_dict = {'foo':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
           'bar':[5,6,7,8,9,8,7,6,5,4,3,2,1,1,2,3]}
           
name_attr = geo.addAttrib(hou.attribType.Point, 'name', '')
m4_attr = geo.addAttrib(hou.attribType.Point, 'm4', [0]*16)
           
for key in my_dict.keys():
    point = geo.createPoint()
    point.setAttribValue(name_attr,key)
    point.setAttribValue(m4_attr,tuple(my_dict[key]))

The next step for me is to find how to create translate, orient and scale attributes from that 'm4' attribute and prepare those points for instancing using CopySOP

 

Edited by StopTheRain
Link to comment
Share on other sites

Nobody responded, so I guess I will just put here what I finally have came up with.
Here is the final piece of the puzzle. How to use point array attribute of 16 floats representing transform to actually get correct translate, orient and scale attributes on those points ready for instancing with CopySOP.

First small correction to the code to create points and set their attributes. My Python dictionary coming from the other application has lists of 16 floats representing transforms.
That is important for initializing correctly my point attribute which holds that list of 16 floats.

node = hou.pwd()
geo = node.geometry()

# For simplicity dictionary consists only of 2 items
# That is just a sample data

my_dict = {'foo':[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0],
           'bar':[5.0,6.0,7.0,8.0,9.0,8.0,7.0,6.0,5.0,4.0,3.0,2.0,1.0,1.0,2.0,3.0]}
           
name_attr = geo.addAttrib(hou.attribType.Point, 'name', '')
m4_attr = geo.addAttrib(hou.attribType.Point, 'm4', [0.0]*16)
           
for key in my_dict.keys():
    point = geo.createPoint()
    point.setAttribValue(name_attr,key)
    point.setAttribValue(m4_attr,tuple(my_dict[key]))

Now we have points, and we have 'name' attribute on them and 'm4' attribute representing the 4x4 transform matrix.

Here is how I am getting translate, orient and scale attributes.

PointVOP.jpg.612685aadc9f5cbfc5c578e7af6b893b.jpg

First import m4 16 floats 'm4' attribute as Type '16 Floats (matrix)'.  Simply multiplying point position by this matrix gives me the correct translate. In order to get the quaternion 'orient' attribute I convert transform matrix to rotation matrix3 and convert the result to quaternions.  And finally to get the 'scale' attribute I use ExtractTransform VOP that does just what its name says - extracts translation, Euler rotation, scale and shear vectors from the transform.
Now my points have all the necessary attributes needed for instancing with CopySOP.
Thanks for observing my humble efforts!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...