Trying to convert Strands to Curves

Discussions concerning programming of SOFTIMAGE©
Post Reply
Pooby
Posts: 501
Joined: 27 Aug 2010, 22:25

Trying to convert Strands to Curves

Post by Pooby » 22 Nov 2013, 12:18

Hi, I was trying an exersise to convert Strands to Curves in a Python script.
When I try and access a vector array, I cant see the values. Instead I get something like >>PyIDispatch000000000001234
Scalars work fine, so I converted the strand values and set them as individual xyz scalars and read that in the following script; which works fine, but I'd really like to know how to do it directly from the 'Strandposition' attribute
If anyone can help in any way, I'd be very grateful.
Thanks

Code: Select all

xsi = Application
lm = LogMessage
from win32com.client import constants as c

sel = xsi.Selection


if sel.Count == 0:
	lm ('nothing selected')
sel=sel (0)

	
pntIDs = sel.SubComponent.ElementArray
mesh = sel.SubComponent.Parent3DObject
geom = mesh.ActivePrimitive.Geometry
	
for eachpoint in pntIDs:

	attrs = mesh.ActivePrimitive.Geometry.ICEAttributes;
	for attr in attrs:
	
		if attr.Name == "x":
			x = attr.DataArray2D [eachpoint]
		if attr.Name == "y":
			y = attr.DataArray2D [eachpoint]
		if attr.Name == "z":
			z = attr.DataArray2D [eachpoint]

	size = len(x)
	
	curcurve = xsi.SICreateCurve("crvlist", 3, 0)
	xsi.CopyPaste(curcurve, "", mesh, 1)
	
	for each in range (size):
	
		xsi.SIAddPointOnCurveAtEnd(curcurve, x [each], y [each], z [each], False, 0, "")
Mod Edit: I wrapped your code in the code tags so it formats correctly. ET

User avatar
xsisupport
Posts: 713
Joined: 09 Jun 2009, 11:02
Location: Montreal Canada
Contact:

Re: Trying to convert Strands to Curves

Post by xsisupport » 22 Nov 2013, 15:48

StrandPosition is an array of Vector3.
See "Trying to read StrandPosition via Python, getting empty tuples" on the Softimage mailing list.
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

Pooby
Posts: 501
Joined: 27 Aug 2010, 22:25

Re: Trying to convert Strands to Curves

Post by Pooby » 22 Nov 2013, 20:31

I couldn't find a resolution to the issue in that thread. Just a remark that he had solved it.

User avatar
xsisupport
Posts: 713
Joined: 09 Jun 2009, 11:02
Location: Montreal Canada
Contact:

Re: Trying to convert Strands to Curves

Post by xsisupport » 22 Nov 2013, 21:05

Code: Select all

# (Sample code from the XSI SDK docs, slightly modified.)

# Try with:
# /XSI_SAMPLES/Scenes/ICE/Modeling_Columns_From_Strands.scn
# with the pointcloud selected.

# I get:
# AttributeError: 'tuple' object has no attribute 'X'


def dispFix( badDispatch ):
    import win32com.client.dynamic
    # Re-Wraps a bad dispatch into a working one:
    return win32com.client.dynamic.Dispatch(badDispatch)
 


from win32com.client import constants
xsi = Application

attrs = xsi.Selection(0).ActivePrimitive.Geometry.ICEAttributes
interesting = ['StrandPosition']
for attr in attrs:
    if str(attr) not in interesting:
        continue

    if attr.StructureType != constants.siICENodeStructureArray:
        continue
    xsi.LogMessage( "*******************************************************************" )
    xsi.LogMessage( "Name: " + attr.Name )
    xsi.LogMessage( "DataType: " + str(attr.DataType) )
    xsi.LogMessage( "StructType: " + str(attr.StructureType) )
    xsi.LogMessage( "ContextType: " + str(attr.ContextType) )
    xsi.LogMessage( "IsDefined: " + str(attr.IsDefined) )   
    xsi.LogMessage( "IsConstant: " + str(attr.IsConstant) )
    xsi.LogMessage( "Readonly: " + str(attr.IsReadonly) )
    xsi.LogMessage( "AttributeCategory: " + str(attr.AttributeCategory) )
    xsi.LogMessage( "Element count: " + str(attr.ElementCount) )
    if attr.IsDefined == 0:
        continue
    dataType = attr.DataType
    data2D = attr.DataArray2D
    for data in data2D:
        for elem in data:
            elem = dispFix(elem)
            if dataType == constants.siICENodeDataFloat:
                xsi.LogMessage( "float: " + str(elem) )
            elif dataType == constants.siICENodeDataLong: 
                xsi.LogMessage( "long: " + str(elem) )
            elif dataType == constants.siICENodeDataBool: 
                xsi.LogMessage( "bool: " + str(elem) )
            elif dataType == constants.siICENodeDataVector3: 
                xsi.LogMessage( "Vector3: " + str(elem.X) + ":" + str(elem.Y) + ":" + str(elem.Z) )
            elif dataType == constants.siICENodeDataQuaternion: 
                xsi.LogMessage( "Quaternion: " + str(elem.W) + ":" + str(elem.X) + ":" + str(elem.Y) + ":" + str(elem.Z) )
            elif dataType == constants.siICENodeDataRotation: 
                xsi.LogMessage( "Rotation: " + str(elem.RotX) + ":" + str(elem.RotY) + ":" + str(elem.RotZ) )
            elif dataType == constants.siICENodeDataMatrix33: 
                xsi.LogMessage( "Matrix33:" );
                xsi.LogMessage( str(elem.Value(0,0)) + ":" + str(elem.Value(0,1)) + ":" + str(elem.Value(0,2)) )
                xsi.LogMessage( str(elem.Value(1,0)) + ":" + str(elem.Value(1,1)) + ":" + str(elem.Value(1,2)) )
                xsi.LogMessage( str(elem.Value(2,0)) + ":" + str(elem.Value(2,1)) + ":" + str(elem.Value(2,2)) )
            elif dataType == constants.siICENodeDataMatrix44: 
                xsi.LogMessage( "Matrix44:" );
                xsi.LogMessage( str(elem.Value(0,0)) + ":" + str(elem.Value(0,1)) + ":" + str(elem.Value(0,2)) + ":" + str(elem.Value(0,3)))
                xsi.LogMessage( str(elem.Value(1,0)) + ":" + str(elem.Value(1,1)) + ":" + str(elem.Value(1,2)) + ":" + str(elem.Value(1,3)))
                xsi.LogMessage( str(elem.Value(2,0)) + ":" + str(elem.Value(2,1)) + ":" + str(elem.Value(2,2)) + ":" + str(elem.Value(2,3)))
                xsi.LogMessage( str(elem.Value(3,0)) + ":" + str(elem.Value(3,1)) + ":" + str(elem.Value(3,2)) + ":" + str(elem.Value(3,3)))
            elif dataType == constants.siICENodeDataColor4: 
                xsi.LogMessage( "Color:" );
                xsi.LogMessage( str(elem.Red) + ":" + str(elem.Green) + ":" + str(elem.Blue) + ":" + str(elem.Alpha) )
            elif dataType == constants.siICENodeDataString: 
                xsi.LogMessage( "String: " + elem );
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

Pooby
Posts: 501
Joined: 27 Aug 2010, 22:25

Re: Trying to convert Strands to Curves

Post by Pooby » 22 Nov 2013, 21:18

I thought that was the faulty code. It is commented that it doesn't work

I'm really looking for instructions on what to do

User avatar
xsisupport
Posts: 713
Joined: 09 Jun 2009, 11:02
Location: Montreal Canada
Contact:

Re: Trying to convert Strands to Curves

Post by xsisupport » 23 Nov 2013, 00:02

Pooby wrote:I thought that was the faulty code. It is commented that it doesn't work

I'm really looking for instructions on what to do
Did you run that code? I fixed the dispatch issue. That's what the dispFix function is.

Code: Select all

def dispFix( badDispatch ):
    import win32com.client.dynamic
    # Re-Wraps a bad dispatch into a working one:
    return win32com.client.dynamic.Dispatch(badDispatch)
Further down in the snippet:

Code: Select all

    for data in data2D:
        for elem in data:
            elem = dispFix(elem)
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

Pooby
Posts: 501
Joined: 27 Aug 2010, 22:25

Re: Trying to convert Strands to Curves

Post by Pooby » 23 Nov 2013, 03:12

I haven't run the code, no.
I only just started scripting a few weeks ago. It didn't enter my head that I could have stumbled across a bug. I thought the answer would be more along the lines of " just rifle through the array setting vector 3 objects and here is an example " or something like that.
I'll try makin head or tail of this when I am next by a machine. Thanks.

User avatar
xsisupport
Posts: 713
Joined: 09 Jun 2009, 11:02
Location: Montreal Canada
Contact:

Re: Trying to convert Strands to Curves

Post by xsisupport » 23 Nov 2013, 13:10

If you're going to go with Python, then you need to know that some times there'll be dispatch problems with some objects. Most of the times everything works, but now and again you'll hit an object like this where you need to do a dispfix.

The answer was included in that example from the SDK docs, but here's a briefer version where I deleted the other lines:

Code: Select all

from win32com.client import constants
xsi = Application

def dispFix( badDispatch ):
    import win32com.client.dynamic
    # Re-Wraps a bad dispatch into a working one:
    return win32com.client.dynamic.Dispatch(badDispatch)
 

attr = xsi.Selection(0).ActivePrimitive.Geometry.ICEAttributes( "StrandPosition" )
dataType = attr.DataType
data2D = attr.DataArray2D
for data in data2D:
	for elem in data:
		elem = dispFix(elem)
		xsi.LogMessage( "Vector3: " + str(elem.X) + ":" + str(elem.Y) + ":" + str(elem.Z) )
And here's the same thing in JScript:

Code: Select all

a = Selection(0).ActivePrimitive.Geometry.ICEAttributes( "StrandPosition" );
LogMessage( ClassName(a) );

x = a.DataArray2D.toArray();
LogMessage( x.length );
for ( var i = 0; i < x.length; i++ )
{
	y = x[i].toArray();
	LogMessage( "=======================" );
	for ( var j = 0; j < y.length; j++ )
	{
		LogMessage( y[j].X + ", " + y[j].Y + ", " + y[j].Z );
	}
}

Last edited by xsisupport on 23 Nov 2013, 19:39, edited 1 time in total.
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

Pooby
Posts: 501
Joined: 27 Aug 2010, 22:25

Re: Trying to convert Strands to Curves

Post by Pooby » 23 Nov 2013, 17:39

Thats Very helpful. thanks

Pooby
Posts: 501
Joined: 27 Aug 2010, 22:25

Re: Trying to convert Strands to Curves

Post by Pooby » 24 Nov 2013, 10:50

So, will this dispfix function work for all types of occurrences of dispatch problems?

User avatar
xsisupport
Posts: 713
Joined: 09 Jun 2009, 11:02
Location: Montreal Canada
Contact:

Re: Trying to convert Strands to Curves

Post by xsisupport » 24 Nov 2013, 15:11

Almost everything is going to work without having to use something like dispFix. The trick is to recognize when you need to do it; sometimes I confirm with JScript that the object does have a certain method/property, then go back and try with dispFix.
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests