Browsing"Imaging"

Photoshop Automation Project Update

Mar 21, 2011 by     No Comments    Posted under: Characters, DotNet, Imaging, Program Automation, Technical Research

Veteran LR.net readers will know that a few years back I published a research project into using managed code to control Photoshop using COM interop. If you are not sure of the article in question, then you can read it here.

It’s been a popular article, so it is with great pleasure that I am now publishing an update. Now that my baby girl is sleeping through the night I’m actually in a position to think clearly about programming again. The new assembly contains a few bug fixes (namely the save option didn’t work properly, oops) and some new methods. The full list can be seen in the class layout at the end of the article.

New methods –

Channels_AlphatoLayerMask()

Channels_DeleteAlpha()

Convert_ImagetoCMYK()

Convert_ImagetoGrayscale()

Convert_ImagetoIndexedColor()

Convert_ImagetoLabColor()

Convert_ImagetoRGB()

Paths_DeleteAll()

Selection_ActivateBottomLayer()

Selection_ActivateTopLayer()

Selection_CreateNewDocumentFrom()

Selection_FromAlpha()

Selection_SelectLayerContents()

Most of these should be self explanatory. I have chained a few of these new functions into a new automation –

Automation_CutOut()

On my latest batch of characters, I was making 2D cut-out style characters based on shots of real people. These PS files were constructed from a green screen shoot of the band Blue Soup.

Blue Soup

In order to get these assets into 3dsMax in order to rig the characters, each layer would have to be selected, pasted into a new document and saved. Then a work path would have to be created and exported. Not a difficult operation, but with four characters (and an average of twenty layers each) is a reasonably time consuming one. Here’s a screen grab of the action working – If you are thinking that the process takes a while on some layers, it’s because the Photoshop file is composed from 6K RAW images shot on a Canon 5D Mk2, and my old dell laptop.

So there you are, a nice time-saving action that is controlled completely via managed code. I invoked this via my test project in VS2008, but it could easily be fired off via 3dsMax itself.

Full class listing –

And as always, download the assembly below –

download

Using HitchHiker – Building a Matte/Shadow material utility

Dec 21, 2010 by     2 Comments    Posted under: 3dsMax, DotNet, Imaging, Maxscript

This post is to show how you can use Hitchhiker to develop maxscript tools in super quick time. Any auto thumbnailer is going to have about a million uses in production in my humble opinion, as most of the development time on a utility is spend building the UI functionality. Whilst on the subject of dotnet controls, I would like to draw attention to another dotnet thumbnail control out there, written by TD Jason Labbe. It’s got a few things that Hitchhiker doesn’t have like multi select, so might be useful if Hitchhiker hasn’t quite got the functionality you need. Nice job Jason! You can read about it here

The Problem

On our last production, we had to consolidate a lot of complex sets into background renders as we were on tight deadlines. Often, the shot would need some form of contact shadow as well as masking so the obvious choice was to use the mental ray matte material. Depending on the type of shot, HD render times could be cut from 10 minutes per frame to around 45 seconds, with no perceptible loss of detail or quality.

The Solution

I wrote a utility that allowed the user to browse for the rendered backgrounds and quickly set the material up as a matte material, ready to be assigned to the background objects and rendered.

MatteHelper

This utility is a good example of what you can use HitchHiker for. The browsing window allows you to pick the map that you want, and with the new cache mode property on the latest hitchhiker, is very fast. The greatest thing was that I wrote this entire script on my train ride home one day. This was exactly why I wrote HitchHiker, a plug and play solution to speed up my deployment of production scripts.

The material setup is simple,  matte/shadow.reflection (mi) shader is made with an Environment/Background Map (mi) shader. The texturemap is then set to screen and the blur taken down. You have to option to include the alpha so that you can use the render for composition. While the script itself is simple, it’s an example of how you can speed up a common bottleneck with very small programming outlay. Suffice to say, it checks that Mental Ray is the production renderer, as the materials are only compatible with that, but if you wanted to adjust it for use with an alternative material setup, it would be very easy. There is a method called :

CreateMatteShader <texmap> <slotnumber>

This could be changed to whatever you liked.

There is another assembly that is included with this script too, called ImageBot. It is an extended picturebox that allows you to zoom in on an area by dragging a marquee with the the right mouse button. This could also be used in other scripts, for example a rendered frame buffer.

How to use the script

Make a copy of your scene and render all static elements and save the rendered background file. Start MonsterMatte and use HitchHiker to navigate to the rendered file and select it. Press ‘Create Shader’. The matte material will be created and added to the designated material slot.

Apply the material to anything in the scene that isn’t animated and render – watch how fast it goes!

Installation

To install, just download the script below and copy the folders to the root of your max installation. You’ll see the macroscript listed under the ‘LoneRobot’ category and is called MonsterMatte. It’s not really a monster tool, but unfortunately when I was writing this article that monster mash song kept going round my head, damn pesky irritating brain.

Merry Xmas!

download

FloatSpinner – A custom control to bypass 3dsmax value casting issues

James Haywood of Bungie Studios recently pointed out an issue on CGTalk regarding the 3ds maxscript/dotnet conversion functions. Notably it was to do with a decimal from dot net being cast into and integer in 3ds max. This could present a problem if you are using a numericupdown control in max (This is the spinner equivalent in Dot net)

James noticed that any decimal values were automatically being cast into the nearest integer. If you check the MXS help, it does indeed support this fact.

From the list, it seemed that what was needed in order for this to function in 3dsmax properly was a spinner control that returned a dotnet single as it’s event handler rather than the current decimal. This is possible. You may have read the other articles about control inheritance and custom events. The new control will be using the same principles.

Control Inheritance

It’s the one form of inheritance that the treasury isn’t thinking of taxing people on. We inherit the numericupdown control, as it already contains the functionality we need in our control, (like how we use extends in MXS). The numericupdown becomes the base class of the control.In VB you refer to this by typing MyBase. This is similar to the way you can use delegate in a scripted plugin.

Once this is done, we have to override the properties we need to change. In VB, the rough syntax for setting up a property that stores any form of information within the assembly is as follows –

private _QuantumTheory As String
<Category(“Data”), Description(“Something that nobody understands”)> _
Public Property QuantumTheory() As String
Get
Return _QuantumTheory
End Get
Set(ByVal value As String)
_QuantumTheory = value
End Set
End Property

You’ll see that the variable is stored within a private member which is the same, except for an underscore before it. Properties are basically a function that has a get and set block. Get, in this case is called when you type something like control.QuantumTheory. It simply returns whatever is stored within the private variable _QuantumTheory. Set is called when you type something like control.QuantumTheory = “unproven”. This is the basic layout for a property the only one simpler than this is a readonly property, that do not have a set part. However, you can put whatever you like within the set part to perform error checking and options for the user. For example, you might want to check a supplied value isn’t outside the maximum or minimum range of the control, like on a spinner. Without some error checking here, you would get an exception thrown if a user tried to set this property. So you could in fact extend the to property to perform logical comparisons on the value submitted.

Another thing you can do is add information about the property. Before the property declaration, you can provide a category and description of the property. This can help organise things better, and viewing the control in Visual studio will show all categories together and provide information about the property and it’s usage. This isn’t always necessary with properties like value, and backcolor, but some properties might need clarifying. I’ll add that I’m not brilliant at doing this myself – Most of the time if I make a control, it’s just for something I’m doing so I miss this bit out if I’m in a rush. (This has been done on the XML panel download assembly though)

The properties that need to be changed are the ones that previously returned a decimal as it’s return type. You’ll see on the property declaration above that all elements are declared with the AS operator. This determines that the property returns whatever type specified. In the case of the floatspinner, here is the value property –

Shadows Property Value() As Single
Get
Return _Value
End Get
Set(ByVal value As Single)
MyBase.Value = value
_Value = Math.Round(CSng(MyBase.Value), MyBase.DecimalPlaces)
End Set
End Property

It is declared as shadows in VB as you are using a property that exists on the base class that you want to replace. I don’t know if this is the correct or proper way to do this in VB but is the only way I have found so far. The thing to note this time is that the property returns a Single. This will mean that when this property is retrieved in 3dsmax, it will be in the correct data type for max to cast into a float, rather than the decimal to integer conversion it currently performs.

Be a DotNet Pirate – Use an Event Arrrrg

This isn’t the only thing that needs to be done. Once the properties have been overridden, the control needs to provide an event for the data to be given to 3dsmax. I have talked in the past about custom event arguments, and they are a great way to make you controls perform tasks where you get exactly the information you need out of the control, in the format ready to be used without further casting or conversion. This is recapping other posts, but is really important if you want to start to develop your own controls for max. Custom Event arguments are classes themselves, so also have properties. It is these properties that allow you to query the eventargs for the information that you want. So in the standard mouse event args, there are stored properties for that particular mouse movement that allow you to get information about the pointer –

System.Windows.Forms.MouseEventArgs Properties –
e.Button
e.Clicks
e.Delta
e.Location
e.X
e.Y

We are going to write a simple eventarg that returns a value as a single in the form of an event property. This way, we know that 3dsMax will get the correct data structure we need.

Public Class LoneRobotValueChangedEventArgs
Inherits EventArgs
Private _Value As Single
Public Property Value() As Single
Get
Return _Value
End Get
Set(ByVal value As Single)
_Value = value
End Set
End Property
Public Sub New(ByVal Value As Single)
Me.Value = Value
End Sub
End Class

This is pretty much it, all we need to add to the custom control is the connection between the valuechanged event in the numericupdown, and our new event class. We do this by overriding that, and calling our new event class. firstly, we override the event we want to alter –

Public Shadows Event ValueChanged(ByVal sender As Object, ByVal e As LoneRobotValueChangedEventArgs)

We have changed it so that it now accepts a lonerobotValueChangedEventArgs instead of a SystemValueChangedEventArgs

Protected Overrides Sub OnValueChanged(ByVal e As System.EventArgs)
Me.Value = Math.Round(CSng(MyBase.Value), MyBase.DecimalPlaces)
RaiseEvent ValueChanged(Me, New LoneRobotValueChangedEventArgs(Math.Round(CSng(MyBase.Value), MyBase.DecimalPlaces)))
End Sub


You’ll see that when we raise the event, we do so by instantiating a new lonerobotValueChangedEventArgs object. The sender (i.e the control) is the first argument, and a second argument which is a number that is set into the event property “value”. Now that has been performs, you can retrieve this value in max as a single and therefore get the float value that you need.

Download the source for this below. As I mentioned before in the post, I haven’t had a great deal of time to write this properly, perhaps someone could add properties if needed and post the results.

download script

Appendix

I also mentioned in the forum that I had seen a class in the managed services dll called maxspinner.

I couldn’t instantiate this so I asked over at the SDKblog on the Area. This was the response that I got –

Regarding your .NET Questions.

First off, a disclaimer – if a class doesn’t have specific documentation (in this class, it’s excluded) then we don’t officially support its usage.

That said, those classes are public (because they need to be used across Assembly boundaries) and we can’t really prevent anyone from at least trying. 🙂

MaxSpinner is a wrapper class that takes the Spinner custom control that we expose in Win32 and provides a managed interface so that it can be used in an interop solution. This means that we take care of instantiating the class, but the client would need to grab the handle, using MaxNativeControl::HostWindowHandle, and embed it in an interop solution (such as System.Windows.Interop.HwndHost in WPF.)

So there you are, that clears that up. MaxSpinner is not for us mortals.

Follow Up –

DotNet Supremo Yannick Peuch has managed to clear up this issue with a conversion before Max casts to and integer. Thanks for sharing this Yannick!

(
rollout upDownRolloutTest “NumericUpDown Control Test” width:220 height:45
(
dotNetControl upDownCtrl “System.Windows.Forms.NumericUpDown” pos:[10,10] width:50 height:25
dotNetControl btnGetValue “System.Windows.Forms.Button” pos:[90,10] width:100 height:21
on upDownRolloutTest open do
(
upDownCtrl.DecimalPlaces = 1
upDownCtrl.Increment = 0.1
upDownCtrl.Value = 1.0
upDownCtrl.Minimum = 0.0
upDownCtrl.Maximum = 10.0
btnGetValue.Text = “Get Value”
btnGetValue.FlatStyle = (dotNetClass “System.Windows.Forms.FlatStyle”).System
)
on btnGetValue MouseClick do
(
— Get the value auto converted by MAXScript
fValue = upDownCtrl.Value
format “Wrong Value : %n” fValue
— Get the decimal value as a .NET object
dValue = getProperty upDownCtrl #value asDotNetObject:true
— Convert it to single object value then auto converted to float value by MAXScript
fValue = (dotNetClass “System.Decimal”).ToSingle dValue
format “Good Value : %n” fValue
createDialog upDownRolloutTest
)

Automating Photoshop with a DotNet Class Library and COM interop

Recently, there was a thread on CGTalk about whether it was possible to save out multiple render passes into a layered photoshop file from 3dsMax itself. This question had been sitting in the back of my mind ever since and I decided to look into whether it was possible. My first thought was that there was an external image library out there that could do the job. However, finding a good solution meant I didn’t want to have to purchase software for something that was purely research. I already own a great library – AIL, which is based on the freeimage library. Alas, while this can open Photoshop documents, it can not create, structure and save them. However, after a bit of research on the Adobe site, I found that it is indeed possible to do all these things using DotNet.

With Photoshop, Adobe has included an object library assembly. This is a clever assembly that allows you to write custom scripts and automate the Photoshop UI. “Hang on Shirley”, I hear you cry, “We don’t need that – I own a Mac and we’ve got actions for that, I can do everything I need from there! It’s fun, too”.

Well, yes and no. This programmed method still could be the ‘droids you are looking for’.

Actions mean that you obviously need to have it recorded at some point, and be able to trigger the start of the action from within the photoshop application. However, when using maps in 3dsMax there are many occasions when you need to perform a tweak here and there to a map, and the workflow is broken as you have to open it in another program and perform the tasks.

Some other benefits (from the Adobe scripting guide) are –

  • You can add conditional logic, so that the script automatically makes “decisions” based on the currentsituation. For example, you could write a script that decides which color border to add depending onthe size of the selected area in an image: “If the selected area is smaller than 2 x 4 inches, add a greenborder; otherwise add a red border.”
  • A single script can perform actions that involve multiple applications. For example, depending on thescripting language you are using, you could target both Photoshop CS3 and another Adobe CreativeSuite 3 Application, such as Illustrator® CS3, in the same script.
  • You can open, save, and rename files using scripts.
  • You can copy scripts from one computer to another. If you were using an Action and then switchedcomputers, you’d have to recreate the Action.
  • Scripts provide more versatility for automatically opening files. When opening a file in an action, youmust hard code the file location. In a script, you can use variables for file paths.

With this in mind, you are presented with the opportunity that you can automate Photoshop from within the 3dsMax interface. For example, you could have a maxfile that renders multiple passes and calls a post-render script when it finishes, triggering the creation of a psd composite of all the images. You could use it to call a function to overlay an Ambient Occlusion pass on notification of a completed render. You could select an object in the 3dsMax viewport and have Photoshop apply a Gaussian Blur or an artistic effect to the Diffusemap without leaving the 3dsMax Interface. The applications can be as specialised as you want but completely customisable in the same breath.

Creating a Photoshop Assembly for 3dsMax

Because the access to Photoshop is via COM, you need to use an interop assembly in order to use it within a DotNet context. With my fuzzy logic, The interop assembly basically handles the calls between the DotNet assembly and the COM object. I have demonstrated this before, using the COM assembly DSOFile in order to extract the 3DSMax file thumbnail, and there seems to be a small snag that I haven’t been able to resolve as of yet.

Trying to directly use COM interop via the DotNet bridge in 3dsMax can only get you so far. At some point you will (via interop) be accessing a COM object itself. This part I don’t explain fully myself, but as Max uses reflection it reaches a point at which certain objects are created and viewed explicitly as the system.__COMobject type. This is a pain, – one because you can’t cast it to the correct object type in 3dsMax as this is a core function, and two because as a system.__COMobject, you can only access the properties and methods of that type, not the underlying class that the interop assembly is giving you access to. Here’s an example of my failed attempt using just the interop assembly and attempting to set this up via MaxScript – (Listener output is in blue)

dotnet.loadassembly ((getdir#scripts)+"LoneRobotClassLibInterop.Photoshop")
dotNetObject:System.Reflection.Assembly
ps = dotnetobject "Photoshop.ApplicationClass"
dotNetObject:Photoshop.ApplicationClass
showproperties ps
.ActiveDocument : <Photoshop.Document>
.Application : <Photoshop.Application>, read-only
.BackgroundColor : <Photoshop.SolidColor>
.ColorSettings : <System.String>, read-only
.DisplayDialogs : <Photoshop.PsDialogModes>
.Documents : <Photoshop.Documents>, read-only
.Fonts : <Photoshop.TextFonts>, read-only
.ForegroundColor : <Photoshop.SolidColor>
.FreeMemory : <System.Double>, read-only
.Locale : <System.String>, read-only
.MacintoshFileTypes : <System.Object>, read-only
.MeasurementLog : <Photoshop.MeasurementLog>, read-only
.Name : <System.String>, read-only
.Notifiers : <Photoshop.Notifiers>, read-only
.NotifiersEnabled : <System.Boolean>
.Path : <System.String>, read-only
.Preferences : <Photoshop.Preferences>, read-only
.PreferencesFolder : <System.String>, read-only
.RecentFiles : <System.Object>, read-only
.ScriptingBuildDate : <System.String>, read-only
.ScriptingVersion : <System.String>, read-only
.Version : <System.String>, read-only
.Visible : <System.Boolean>
.WinColorSettings : <System.String>, read-only
.WindowsFileTypes : <System.Object>, read-only
ps.documents
dotNetObject:System.__ComObject
-- this should be a Photoshop.Documents class??
show ps.documents
false
showmethods ps.documents
.<System.Runtime.Remoting.ObjRef>CreateObjRef <System.Type>requestedType
.<System.Boolean>Equals <System.Object>obj
.[static]<System.Boolean>Equals <System.Object>objA <System.Object>objB
.<System.Int32>GetHashCode()
.<System.Object>GetLifetimeService()
.<System.Type>GetType()
.<System.Object>InitializeLifetimeService()
.[static]<System.Boolean>ReferenceEquals <System.Object>objA <System.Object>objB
.<System.String>ToString()
-- ie showing the methods of the COM object, not the documents class.

I cant say exactly what is going on, except that Max is returning the COM object’s methods rather than the methods the interop assembly is allowing from the Adobe Object library. Maybe this is something to do with Reflection or the way 3dsMax sees DotNet, I really don’t know. If anyone reading this has any ideas, feel free to correct me. Just using the application class methods, like load() will work, so you can open a photoshop file via max, it’s just that you can’t do an awful lot after that. 🙁

It’s not all bad, as there is no such limitation in the Visual Studio IDE. Which means you can write a wrapper to expose the functions you want to call within max. This is actually a nice way of testing these things, as the VS IDE is a far superior environment to test and debug your classes, and you also get help from intellisense and prompting you when you make all sorts of errors.

Coding a COM Wrapper

The next bit assumes the following criteria, and was done in this environment –

  • A Visual Studio 2008 VB project on Vista x86
  • PhotoShop CS3 (this is the newest and only version I have)
  • 3dsMax 2009

For different versions of Photoshop, there may be slight differences but the principles should essentially be the same. Also note that the code examples are in Visual Basic, but would be almost identical if done in C#. Once the photoshop object is created, the methods and property access would be the same. A wrapper is a class library that provides you with convenient access to the photoshop application, so that you can make general calls from max like photoshop.blur() or photoshop.strokeselection red. It means you can write an assembly with whatever functions you want and deploy them wherever you like.

Creating the Interop Assembly

Open the Visual Studio IDE. Choose File>New Project>Class Library (VB or C#)

Name it something suitable. Not PatSharpesHairPiece or MrCommotasticsPhotoShopofHorrors, something meaningful. Actually that last one isn’t that bad.

Click the show all files button on the Solution Explorer

Right Click the References branch and choose Add Reference (Or from the menu Project>Add Reference)

When the dialog loads, instead of .NET, pick the COM tab. Choose Adobe Photoshop Object Library from the list.

Bingo, you’ve created a reference to photoshop –

Save, and Build your project. Depending on the compile parameters, you will have created an interop assembly in one of the folders. Usually this is the projectfolder>bin>debug folder. If you check in this folder in the solution explorer you will see listed Interop.Photoshop.dll

That’s the first part, Visual Studio automatically creates the COM interop assembly for you. Thanks Visual Studio, and much obliged I am too.

Bring on the Shop

Now a drop of the good stuff. Take a look at how the photoshop object is constructed. I have highlighted the three main aspects to show what we will be using in this tutorial –

Most important is the Application class. This creates an instance of the photoshop application. If photoshop is not open, Instantiating this object will open Photoshop for you. Don’t worry, you can make the UI a background process after this if you don’t want to (or need) see what is going on. All operations on individual documents are through the Documents class, strangely, and individual layers are accessed via the ArtLayers class. Collections index members are from 1 rather than 0 like in a VB array. The first document
created has an index of 1.

If you look up in the Document object in the
Visual Basic Object Browser, you will see that there is no Add() method for the object. However, the Add()
method is available for the Documents object. Similarly, the ArtLayer object does not have an Add()
method; the ArtLayers object does. You have to add these via these collections than the individual object class. Not only that, you need to refer to them via the Application class. So to adjust the brightness and contrast of layer 4 you would have to do it with the following code –

<ApplicationObject>.ActiveDocument.ArtLayers(4).AdjustBrightnessContrast()

In your class library, place an import (or using in C#) statement at the top of the class so you don’t have to keep writing the full address. I have called my class PhotoShopBot, surprisingly.

Imports Photoshop
Public Class PhotoshopBot
End Class

In order for the class to perform the automation you are going to program, It will need an instance of the Photoshop application object. It makes sense to declare this as a private member variable at the start, and instantiate it in the new sub. That way, you only create one photoshop application object, which can then be used if you extend the class library in the future and add more methods. You don’t want to be creating it each time you call a function, that would be wasteful.

Imports Photoshop
Private PSDapp As Application
Public Class PhotoshopBot
Public Sub New()
PSDapp = New Application()
End Sub
End Class

Currently, all this class is doing is instantiating the application object, so in theory creating a dotnetobject in max from the resulting assembly would actually open up the photoshop interface!

Now, define a sub for the actual work you want the class to do. Since the post on CGTalk was discussing whether it was possible to create a PSD file with layers from multiple render passes, so this is what I will setup in this tutorial.

Imports Photoshop
Private PSDapp As Application
Public Class PhotoshopBot
Public Sub New()
PSDapp = New Application()
End Sub
Public Sub BasicImageCompositor(ByVal Images() As String, _
ByVal OutputFileName As String, _
ByVal HideApplication As Boolean, _
ByVal OpenOnCompletion As Boolean)
End Sub
End Class

BasicImageCompositor is going to work by providing an array of filenames. This could be passed from a maxscript using the GetFiles function to create the array, so could be generated dynamically from a folder’s contents. For the purposes of this basic subroutine, the layer order will be done bottom up by the order of the array elements, so make sure the background layer is first and each subsequent layer next. You could give your passes names so that this would automatically arrange them into the correct order after calling sort on the array. The reason it is an array of strings is that the Application object calls load on a string filename, don’t forget windows only opens a small selection of filetypes natively.

The thing to remember is that you need to keep track of the active document before you do anything. This is a property that is set via the Documents collection of the Application object. Let’s look at the entire class –

Imports Photoshop

Public Class PhotoshopBot

Private PSDapp As Application

Public Sub New()
PSDapp = New Application()
End Sub

Public Sub BasicImageCompositor(ByVal Images() As String, _
ByVal OutputFileName As String, _
ByVal HideApplication As Boolean, _
ByVal OpenOnCompletion As Boolean)

' get a fileinfo object to check the folder validity
Dim OutFile As New IO.FileInfo(OutputFileName)

If OutFile.Directory.Exists Then
' hide the photoshop UI
If HideApplication Then PSDapp.Visible = False
' Suppress the Photoshop dialogs (if any)
PSDapp.DisplayDialogs = 3
'   enum 3 =
'   PsDialogModes.psDisplayNoDialogs()
'load the first image and duplicate it into a new image document

If Images.Length > 0 Then
Dim BackGroundLayer As String = Images(0)
PSDapp.Load(BackGroundLayer)
PSDapp.ActiveDocument = PSDapp.Documents.Item(1)
PSDapp.ActiveDocument.Duplicate(OutFile.Name)
'close the original
'close(1) bypasses the save dialog
PSDapp.Documents.Item(1).Close(1)
'set the active document to the duplicate
PSDapp.ActiveDocument = PSDapp.Documents.Item(1)
'loop through the rest of the array, copying and pasting it into the
'duplicated document
For imageindex = LBound(Images) + 1 To UBound(Images)
PSDapp.Load(Images(imageindex))
PSDapp.ActiveDocument = PSDapp.Documents.Item(2)
PSDapp.ActiveDocument.Selection.SelectAll()
PSDapp.ActiveDocument.ArtLayers(1).Copy()
PSDapp.Documents.Item(2).Close(1)
PSDapp.ActiveDocument = PSDapp.Documents.Item(1)
PSDapp.ActiveDocument.Paste()
Next

'save it
PSDapp.ActiveDocument.SaveAs(OutputFileName)
PSDapp.ActiveDocument.Close(1)
' show the ui and load the image, if desired
If HideApplication Then PSDapp.Visible = True
If OpenOnCompletion Then PSDapp.Load(OutputFileName)
End If
Else
' if the folder doesn't exist it will throw this exception
Throw New IO.DirectoryNotFoundException("Directory does not exist")
End If

End Sub

End Class

That’s it, compile the project and copy the two dlls it makes to your max directory or wherever you load your dotnet stuff from. Depending on your project and namespaces used this will be different but mine is photoshop.dll and photoshop.interop.dll

Using this assembly in 3DSMax

Now there is the simple issue of creating an instance of this class and calling the only method it has. I am going to composite the following images –

Background – (Click to save if you want to try this example)

Other layers

As for 3dsMax, here is the script to automate the creation of the PSD file. Don’t forget your paths will be different to mine!

dotnet.loadassembly ((getdir#scripts)+"LoneRobotClassLibInterop.Photoshop")

dotnet.loadassembly ((getdir#scripts)+"LoneRobotClassLibPhotoshop")

oPhotoShop = dotnetobject "LoneRobot.Imaging.PhotoshopBot"

showmethods PshopApp

imagepath = @"C:LoneRobot Script DevelopmentPhotoShopInteroptestimages"

imagearray = #((imagepath + "bg.jpg"),(imagepath + "layer1.png"),(imagepath + "layer2.png"),(imagepath + "layer3.png"),(imagepath + "layer4.png"))

oPhotoShop.BasicImageCompositor imagearray (imagepath +@"testoutput.psd") false true

All things being fair and just, you should have this in Photoshop – Ah crap!

Problem with this is that the copy/paste automation has placed the layers bang in the middle of the composition. This is how photoshop does this, regardless of image, if there is a transparent area around the image it takes the bounding box of the layer and pastes it smack in the middle of the comp. What it needs is a method for placing the layers when they are put in the composition. Looks like we will have to return to our class library and implement this.

Adding layer placement to the class

In a somewhat hacky way, photoshop does allow you to paste a layer into a certain position – If you select a marquee somewhere on the composition, it will use that as a location point to place the pasted layer. It takes the center of the layer and aligns it to the center of the selection marquee. Because of this, we can try to mimic this behavior in our loop.

So firstly, we need to work out how to automate the selection procedure in Photoshop. The Photoshop application object wants a mildly ambiguous Region object to be passed to the select function. This is listed in the Adobe help as

Array(Array(0, 0), Array(0, 100), Array(100,100), Array(100,0))

Now this is VBScript so my best guess is that in VB it is an array of Integer Arrays, with each array being the coordinate of the drawing surface. The other thing to note is if you traced the draw order, it means that it is drawing the selection marquee point-by-point in an anti-clockwise direction. This is a little confusing
to say the least, but since drawing selection marquees are pretty important,
It would be worth finding a way of calling this method with a simpler structure. To create a Photoshop selection region via VB the syntax is slightly different –

 Dim psdArray1() as Object = {x, y}

Dim psdArray2() as Object = {w, y}

Dim psdArray3() as Object = {x, h}

Dim psdArray4() as Object = {x, h}

Dim psdArray() as Object = {psdArray1, psdArray2, psdArray3, psdArray4}

PSDapp.Selection.Select(psdArray)

It seems that casting to anything else than a generic object causes an exception with the COM assembly. The best thing, in order to keep this DotNetty, would be to write a function to return the selection region by providing a Drawing.Rectangle object as an argument. That way it’s much easier to visualise the area that you want to select. Here’s what i came up with –

Public Function SelectionMarquee(ByVal Area As Drawing.Rectangle) As Object

Dim psArray1() As Object = {Area.X, Area.Y}

Dim psArray2() As Object = {Area.X, Area.Y + Area.Width}

Dim psArray3() As Object = {Area.X + Area.Height, Area.Y + Area.Width}

Dim psArray4() As Object = {Area.X + Area.Height, Area.Y}

Dim psArray() As Object = {psArray1, psArray2, psArray3, psArray4}

Return psArray

End Function

My idea is to pass a drawing.point array from max, since you are simply saying “paste this layer into position X,Y”. The function will then construct a rectangle with a height and width of 1 pixel. Then, when pasting the layer, it will be at the XY coordinates specified. Let’s see how that looks –

Public Sub BasicImageCompositor(ByVal Images() As String, _

ByVal Placement() As Drawing.Point, _

ByVal OutputFileName As String, _

ByVal HideApplication As Boolean, _

ByVal OpenOnCompletion As Boolean)

' get a fileinfo object to check the folder validity

Dim OutFile As New IO.FileInfo(OutputFileName)

If OutFile.Directory.Exists Then

' hide the photoshop UI

If HideApplication Then PSDapp.Visible = False

' Suppress the Photoshop dialogs (if any)

PSDapp.DisplayDialogs = 3

'   enum 3 =

'   PsDialogModes.psDisplayNoDialogs()

'load the first image and duplicate it into a new image document

If Images.Length > 0 Then

Dim BackGroundLayer As String = Images(0)

PSDapp.Load(BackGroundLayer)

PSDapp.ActiveDocument = PSDapp.Documents.Item(1)

PSDapp.ActiveDocument.Duplicate(OutFile.Name)

'close the original

'close(1) bypasses the save dialog

PSDapp.Documents.Item(1).Close(1)

'set the active document to the duplicate

PSDapp.ActiveDocument = PSDapp.Documents.Item(1)

'loop through the rest of the array, copying and pasting it into the

'duplicated document

For imageindex = LBound(Images) + 1 To UBound(Images)

PSDapp.Load(Images(imageindex))

PSDapp.ActiveDocument = PSDapp.Documents.Item(2)

PSDapp.ActiveDocument.Selection.SelectAll()

PSDapp.ActiveDocument.ArtLayers(1).Copy()

PSDapp.Documents.Item(2).Close(1)

PSDapp.ActiveDocument = PSDapp.Documents.Item(1)

'Select the point at which to paste the layer

Try

Dim Location As Drawing.Point = CType(Placement(imageindex), Drawing.Point)

Dim Marquee As Object = SelectionMarquee(New Drawing.Rectangle(Location.X, Location.Y, 1, 1))

PSDapp.ActiveDocument.Selection.Select(Marquee)

Catch ex As Exception

End Try

PSDapp.ActiveDocument.Paste()

Next

'save it

PSDapp.ActiveDocument.SaveAs(OutputFileName)

PSDapp.ActiveDocument.Close(1)

' show the ui and load the image, if desired

If HideApplication Then PSDapp.Visible = True

If OpenOnCompletion Then PSDapp.Load(OutputFileName)

End If

Else

' if the folder doesn't exist it will throw this exception

Throw New IO.DirectoryNotFoundException("Directory does not exist")

End If

End Sub

So now you need to change the code you call from 3dsMax to include a placement argument. This is just an array of dotnet point objects.

oPhotoShop = dotnetobject "LoneRobot.Imaging.PhotoshopBot"

imagepath = @"C:LoneRobot Script DevelopmentPhotoShopInteroptestimages"

imagearray = #((imagepath + "bg.jpg"),(imagepath + "layer1.png"),(imagepath + "layer2.png"),(imagepath + "layer3.png"),(imagepath + "layer4.png"))

LocationArray = #((DotNetObject "System.Drawing.Point" 0 0),(DotNetObject "System.Drawing.Point" 411 513),(DotNetObject "System.Drawing.Point" 109 531),(DotNetObject "System.Drawing.Point" 193 444),(DotNetObject "System.Drawing.Point" 314 421))

oPhotoShop.BasicImageCompositor imagearray LocationArray (imagepath +@"testoutput.psd") false true

The first point in the array is (0,0) – This is because it represents the Background layer position, so is disregarded by the loop in the class.

What do we get now?

An automated composition with layer placement, all from within 3dsMax!

Huzzah!

I hope this tutorial has been helpful and you can see the potential of being able to use this within 3DSmax. You really can automate just about everything, without even going near the photoshop interface. I’m quite excited about the possibilities of this, so I will perhaps write more in the future.

Further reading –

http://www.adobe.com/devnet/photoshop/scripting/

The download package has the assemblies and the VB version of the class itself. If you want the test images, just leech them from here. Cheers!

Update!

I have updated the wrapper with some more methods listed below. There are a few things you can now change with this class. A simple showmethods from 3ds max will give you all the info you need to use these functions. The enums are used within a few of the functions, like setting the blend modes. It’s just an easier way to specify these.

I would advise always using the function setactivelayer before any code, as it is a boolean function that returns true on setting the correct layer, and false if it doesn’t exist.

If you’ve got this far then you know what you are doing anyway! Enjoy!

Lastest Update!

I’ve added a whole load of classes exposing the save options for various other photoshop image formats. You can now save out to the following image types with this assembly –

  • .bmp
  • .jpg
  • .tga
  • .png
  • .eps
  • .psd
  • .gif
  • .tiff

Download at the bottom as usual!

New Update!

Just managed to get a handle on programming the Action Descriptor using VB. This is reasonably straight forward, you can use the Adobe Scripting Listener to record the actions. It’s amost the same as VB, as it is VBscript. This has basically allowed me to now integrate pretty much any action or command into the assembly. You could make a highly complex series of commands and wrap it into a fuction and call it straight from 3dsMax. Take a look at the new methods – there are many not exposed by the Adobe Object Library. If you think of a useful action or operation you’d like to see in this assembly, let me know below and i’ll try to integrate it for you.

download script

Generating PDF Documents via MXS and .NET

Apr 14, 2009 by     No Comments    Posted under: 3dsMax, DotNet, Imaging, Maxscript, Program Automation, Technical Research

Recently, I discovered an open source library written in C# called PDFSharp. This came about because I was working on a script that generated a text file with information in at the end. Unfortunately, when it came to refining the presentation, my ASCI skills are seriously limited. For example, my attempt at the LoneRobot logo looked like this –

............o
............|
......... ___
........ /../
......8=/- /=8  - BeepBeepDoWopDoDoopDeep!
....... ---
........| |
........o.o

In the magazine “ASCI Art Lovers Monthly” , this attempt gained a four asterisk rating, although others have subsequently informed me that the **** peppered over the artistic comments page are not related to this.

Because this generated document was to be printed and used as reference, It got me thinking about whether I could generate better printable documents directly from 3dsMax without having to use OpenOffice, Acrobat or any of my presentation skills. Fortunately, PDFSharp is an assembly that allows you to create PDF documents. The way it does this is to mirror a whole set of GDI+/WPF drawing classes and allow you to construct the page programmatically via DotNet. (If you want to look at a few DotNet GDI+ drawing methods for 3dsmax, check out this page)

Regardless of it’s limited appeal within Max, it is an amazing piece of open source code that should most certainly be looked at.

In order to integrate this, I wrote a small MXS Struct to wrap a few of the functions of PDFSharp’s GDI+ drawing methods, since this was all I was interested in using for my purposes. There are many, many more drawing and formatting options available through this assembly, but in the struct the following methods are available –

  • DrawImage (With optional scale multiplier)
  • DrawRectangle (Specify a corner radius to draw a rounded rectangle)
  • DrawEllipse
  • DrawLine (With different line styles)
  • DrawString (With various paragraph alignment options – Left,Right,Justify,Center)

The PDF is drawn on a GDI+ drawing surface, in this case a GDI bitmap the size of an A4 document. There is a basic example within the test code that creates the following PDF file –

Don’t Forget, you’ll need to replace the line where it loads in and draws the image, or the TestDocument function will fail.

You can get PDFSharp from here, and of course download the example below as normal. It’s well commented so you should be able to get an idea of how to use it. The source code of PDFSharp has a pretty comprehensive array of test projects to pick at.

I’m still waiting for our subscription copy of max 2010, so I’ll probably add a version later using the private/public declarations available in this release.

If anyone is interested, the text on the PDF is from the rather beautiful short story ‘The Man Who Planted Trees‘ by Jean Giono.

download script